Skip to main content

A basic wrapper for the Discord Linked Roles OAuth2 API.

Project description

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

import config
from linked_roles import LinkedRolesOAuth2, OAuth2Scopes, RolePlatform, UserNotFound

app = FastAPI(title='Linked Roles OAuth2')

client = LinkedRolesOAuth2(
    client_id=config.DISCORD_CLIENT_ID,
    client_secret=config.DISCORD_CLIENT_SECRET,
    redirect_uri=config.DISCORD_REDIRECT_URI,
    # token=config.DISCORD_TOKEN, # Optinal for Resgister
    scopes=(OAuth2Scopes.role_connection_write, OAuth2Scopes.identify),
    state=config.COOKIE_SECRET,
)

@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 tokens
    tokens = await client.get_oauth2_tokens(code)

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

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

    # set role platform
    platform = RolePlatform(name='VALORANT', username=str(user))

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

    # set role metadata
    await user.edit_role_metadata(platform=platform)

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

Register Example:

import asyncio

import config
from linked_roles import AppRoleConnectionMetadataRecordType as RoleMetadataType, LinkedRolesOAuth2, RoleMetadataRecord


async def main():

    client = LinkedRolesOAuth2(client_id=config.DISCORD_CLIENT_ID, token=config.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)
        print(records)


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

Config Example:

DISCORD_TOKEN = '<your bot token>'
DISCORD_CLIENT_ID = '<your client id>'
DISCORD_CLIENT_SECRET = '<your client secret>'
DISCORD_REDIRECT_URI = 'http://localhost:8000/verified-role'  # example redirect uri
COOKIE_SECRET = '<your cookie secret>'

# cookie secret can be generated with:
import uuid
>> uuid.uuid4().hex

More Examples:

TODO:

  • Add more examples
  • Add documentation
  • Add database support (postgresql, sqlite, etc.) ?
  • add more class error

Code Style Inspiration

License

licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linked-roles-1.2.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linked_roles-1.2.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file linked-roles-1.2.0.tar.gz.

File metadata

  • Download URL: linked-roles-1.2.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for linked-roles-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c76792c4de0f3e54cbe17485310a5723167369417a0ae69099c42817fe5d96b8
MD5 dcd5ef242b9e917838bd8188c71e3c3f
BLAKE2b-256 880af70458da64e01099ff65df58863769dcf14c29d33f247174a4dfa5c60424

See more details on using hashes here.

File details

Details for the file linked_roles-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: linked_roles-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for linked_roles-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41581b01ee278de5d1b46b24b30fbf07ab89b3830bd1220a45fa2c6cbf4af2de
MD5 d7a913e67fb5c7fe9630898c64ef5ce9
BLAKE2b-256 ea87072f961a87d2eb64426613c0f51ae20fbf04fe19e772d04d0a2bea78e00b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page