API Reference#
The following section outlines the API of disco_oauth2.
Note
This module uses the Python logging module to log diagnostic and errors in an output independent way. If the logging module is not configured, these logs will not be output anywhere.
Client#
- class disco_oauth2.Client(*, client_id, client_secret, redirect_uri, scopes=None, connector=None, proxy=None, proxy_auth=None, http_trace=None)#
Represents a client that offers interaction to the discord OAuth2 API.
- Parameters:
client_id (
int) – The OAuth2 application client id.client_secret (
str) – The OAuth2 application client secret.redirect_uri (
str) – The OAuth2 redirect url. This url must configurated from uri’s at discord developer portal.scopes (Optional[List[
str]]) – List of OAuth2 scopes. DefaultNone.connector (Optional[
aiohttp.BaseConnector]) – A connector for aiohttp client API. aiohttp documentation. DefaultNone.proxy (Optional[
str]) – The proxy URL. DefaultNone.proxy_auth (Optional[
aiohttp.BasicAuth]) –Object that represents proxy HTTP Basic Authorization. aiohttp documentation. Default
None.http_trace (Optional[
aiohttp.TraceConfig]) –The trace configuration to use for tracking HTTP requests the library does using
aiohttp. aiohttp documentation. DefaultNone.
- async exchange_code(code)#
Exchanges the code you receive from the OAuth2 redirect.
- Parameters:
code (
str) – The code from the querystring.- Return type:
- async refresh_token(refresh_token)#
Refreshes an access token. This can be useful to anticipate the expiration and refresh the token.
- Parameters:
refresh_toke (
str) – The user’s refresh token.- Return type:
- async revoke_token(token)#
Revokes a token, token can be either an access token or an refresh token. This can be useful to invalidate the previous token.
Note
Invalid tokens do not cause an error response.
- Parameters:
token (
str) – The refresh or access token that will get revoked.
- async fetch_user(access_token)#
Fetch user’s information from discord api using user’s access token.
You must have the scope identify to use this. To access user’s email you must have the scope email.
- Parameters:
access_token (
AccessToken) – The user’s access token.- Return type:
- get_oauth_url(*, prompt=..., state=..., response_type='code', disable_guild_select=..., guild_id=..., permissions=..., scopes=..., redirect_uri=...)#
Returns the OAuth2 URL to authorize this application.
- Parameters:
prompt (
str) – Controls how existing authorizations are handled, either consent or none. You must have scopes set to use this.state (
str) – A unique cryptographically secure hash. discord documentationresponse_type (Literal["code", "token"]) – The response type, either code or token. The token is for client-side web applications only. Defaults
code.disable_guild_select (
bool) – Disallows the user from changing the guild for the bot invite, either true or false. You must have the scope bot to use this.guild_id (
bool) – The guild id to pre-fill at authorization url. You must have the scope bot to use this.permissions (
int) – The permissions flags for the bot invite. You must have the scope bot to use this.scopes (Iterable[
str]) –An optional valid list of scopes. Defaults to
self.scopes.New in version 1.3.
redirect_uri (
str) –An optional valid redirect URI. Defaults
self.redirect_uriNew in version 1.3.
- Return type:
The OAuth2 URL with all the received parameters.
- get_partial_user(*, access_token)#
Returns a partial user.
This is useful if you have a access_token but don’t want to do an API call to fetch it again.
Note that this partial user class will not let you use methods that requires user information.
- Parameters:
access_token (
AccessToken) – The user’s access token.- Return type:
PartialUser
- async close()#
Closes and cleanup operations on the client.
AccessToken#
- class disco_oauth2.AccessToken#
Represents a access token response information provided by discord.
Discord Models#
User#
- class disco_oauth2.User#
Represents your Discord user that contains information from the OAuth2 API.
- x == y
Compare the ids of the two users and verify that they are the same.
- x != y
Compare the ids of the two users and verify that they are the different.
- hash(x)
Return the user’s hash.
- str(x)
Returns the user’s id with discriminator.
- async refresh()#
Refreshes the access token for the user.
- Return type:
- async add_guild(guild, bot_token, *, nick=None, roles=None, mute=None, deaf=None)#
Adds a user to the guild.
You must have the scope guilds.join to use this.
- Parameters:
guild (
Guild) – Guild that the user will join.bot_token (
str) – A bot token that is in the target guild. Discord requires a bot that you have a bot that is in the target guild you want to add the user.nick (Optional[
str]) – Value to set user’s nickname to.roles (Optional[List[
int]]) – A list of role ids that will be added to the member.mute (Optional[
bool]) – Whether the user is muted in voice channelsdeaf (Optional[
bool]) – Whether the user is deafened in voice channels
- async fetch_guilds()#
Fetch a list of partial guilds that the user is a member of.
You must have the scope guilds to use this.
- Return type:
List[
Guild]
- async fetch_connections()#
Fetch a list of connections of the user.
You must have the scope connections to use this.
- Return type:
List[
Connection]
- async fetch_member(guild_id)#
Fetch the user’s object member from guild ID.
You must have the scope guilds.members.read to use this.
- property accent_colour#
Returns the user’s accent colour, if applicable.
Note
This property will return the color in hex format.
- Type:
Optional[
str]
- property created_at#
Returns the date of when the user account was created in UTC.
- Type:
- property default_avatar#
Returns the default avatar for a given user. This is calculated by the user’s discriminator.
- Type:
- property display_avatar#
Returns the user’s display avatar.
This method is similar to avatar_url, but if the user doesn’t have an avatar it will give the default avatar.
- Type:
- property flags#
Returns the resolved flags of the user.
- Type:
- property public_flags#
Returns the resolved public flags of the user.
- Type:
Guild#
- class disco_oauth2.Guild#
Represents a Discord partial guild.
This is referred to as a “server” in the official Discord UI.
- x == y
Checks if two guilds are equal.
- x != y
Checks if two guilds are not equal.
- hash(x)
Returns the guild’s hash.
- str(x)
Returns the guild’s name.
- permissions#
An Permissions class that represents the permissions of the user in the guild.
- Type:
Optional[
Permissions]
- property permissions#
Returns the resolved permissions that the oauth2 user has in this guild.
- Type:
Member#
- class disco_oauth2.Member#
Represents a Discord member to a
Guild.- x == y
Compare the ids of the two members and verify that they are the same.
- x != y
Compare the ids of the two members and verify that they are the different.
- hash(x)
Return the member’s hash.
- str(x)
Returns the member’s id with discriminator.
- roles#
The list of role ids that the member has
- Type:
SnowflakeList
- joined_at#
An aware datetime object that specifies the date and time in UTC that the member joined the guild.
- Type:
An aware datetime object that specifies the date andtime in UTC that the member “Nitro boost” the guild. This could be
None.- Type:
Optional[
datetime.datetime]
- timed_out_until#
An aware datetime object that specifies the date and time in UTC that the user’s time out will expire. This is
Nonewhen the user is not timed out.- Type:
Optional[
datetime.datetime]
- property permissions#
Returns the resolved permissions that the oauth2 user has in this guild.
- Type:
Connection#
- class disco_oauth2.Connection#
Represents connection object that the user has attached.
- show_activity#
If activities related to this connection will be shown in presence updates
- Type:
- visibility#
Visibility of this connection
- Type:
Visibility
Integrations#
- class disco_oauth2.ServerIntegration#
Represents discord server integrations.
- type#
The integration type (twitch, youtube, discord, …).
- Type:
IntegrationType
- enable_emoticons#
If emoticons should be synced for this integration (twitch only currently).
- Type:
Optional[
bool]
- expire_behavior#
The behavior of expiring subscribers (remove_role or kick).
- Type:
Optional[
ExpireBehavior]
- account#
Integration account information.
- Type:
- synced_at#
When this integration was last synced.
- Type:
Optional[
datetime.datetime]
- application#
The bot/OAuth2 application for discord integrations, if available.
- Type:
Optional[
IntegrationApp]
- class disco_oauth2.IntegrationAccount#
Represents integration account.
- class disco_oauth2.ExpireBehavior#
An enumeration.
- class disco_oauth2.IntegrationApp#
Represents the integration application provided by Discord.
- class disco_oauth2.ExpireBehavior#
An enumeration.
Flags#
Permissions#
- class disco_oauth2.Permissions(value)#
-
- administrator#
Returns
Trueif a user is an administrator. This role overrides all other permissions. This also bypasses all channel-specific overrides.- Type:
- manage_channels#
Returns
Trueif a user can edit, delete, or create channels in the guild. This also corresponds to the “Manage Channel” channel-specific override.- Type:
- read_messages#
Returns
Trueif a user can read messages from all or specific text channels.- Type:
- send_messages#
Returns
Trueif a user can send messages from all or specific text channels.- Type:
- send_tts_messages#
Returns
Trueif a user can send TTS messages from all or specific text channels.- Type:
- embed_links#
Returns
Trueif a user’s messages will automatically be embedded by Discord.- Type:
- read_message_history#
Returns
Trueif a user can read a text channel’s previous messages.- Type:
- mention_everyone#
Returns
Trueif a user’s @everyone or @here will mention everyone in the text channel.- Type:
- manage_roles#
Returns
Trueif a user can create or edit roles less than their role’s position. This also corresponds to the “Manage Permissions” channel-specific override.- Type: