Code Monkey home page Code Monkey logo

discord-linked-roles's Introduction

Discord Linked Roles OAuth2

A basic wrapper for the Discord Linked Roles OAuth2 API.

Releases

Installation

$ pip install -U linked-roles

FastAPI Example:

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

from linked_roles import LinkedRolesOAuth2, RoleConnection, UserNotFound

app = FastAPI(title='Linked Roles')

client = LinkedRolesOAuth2(
    client_id='client_id',
    client_secret='client_secret',
    redirect_uri='http://localhost:8000/verified-role',
    # token='discord_token',
    scopes=('role_connections.write', 'identify'),
)

@app.on_event('startup')
async def startup():
    await client.start()

@app.on_event('shutdown')
async def shutdown():
    await client.close()

@app.get('/linked-role')
async def linked_roles():
    url = client.get_oauth_url()
    return RedirectResponse(url=url)

@app.get('/verified-role')
async def verified_role(code: str):

    # get token
    token = await client.get_access_token(code)

    # get user
    user = await client.fetch_user(token)

    if user is None:
        raise UserNotFound('User not found')

    role = await user.fetch_role_connection()
    
    if role is None:
        # set role connection
        role = RoleConnection(platform_name='VALORANT', platform_username=str(user))

        # add metadata
        role.add_metadata(key='matches', value=10)
        role.add_metadata(key='winrate', value=20)
        role.add_metadata(key='combat_score', value=30)

        # set role metadata
        await user.edit_role_connection(role)

    return 'Role metadata set successfully. Please check your Discord profile.'

Register Example:

import asyncio

from linked_roles import RoleMetadataType, LinkedRolesOAuth2, RoleMetadataRecord

async def main():

    client = LinkedRolesOAuth2(client_id='client_id', token='discord_token')

    async with client:

        records = (
            RoleMetadataRecord(
                key='matches',
                name='Matches',
                type=2,
            ),
            RoleMetadataRecord(
                key='winrate',
                name='Win Rate',
                type=RoleMetadataType.interger_greater_than_or_equal,  # Union Between int and RoleMetadataType
            ),
            RoleMetadataRecord(
                key='combat_score',
                name='Combat Score',
                description='Combat score this season', # description is optional (default: '...')
                type=RoleMetadataType.interger_greater_than_or_equal,
            )
        )

        records = await client.register_role_metadata(records=records, force=True)
        print(records)

if __name__ == '__main__':
    asyncio.run(main())

Cookie secret can be generated with:

import uuid
>> uuid.uuid4().hex

License

licensed under the MIT license.

discord-linked-roles's People

Contributors

isabe1le avatar staciax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

isabe1le

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.