API Reference#
The following section covers the API of Discode.
Clients#
These are the clients, that the library provides.
Client#
The base client to use to connect to the api and receive events.
- defadd_listener
- asyncclose
- asyncdispatch
- defrun
- asyncrun_task
- asyncwait_for
- asyncwait_until_ready
- class discode.Client(token, *, intents=None, api_version=10, gateway_version=9, shard_count=None, gateway_timeout=5.0)#
Represents the Client that connects to the Discord Gateway and HTTP Api.
- Parameters
token (
str) – The token to use to communicate with the gateway and REST APIs.intents (
Intents) – The intents to use while connecting to the gateway. Defaults toIntents.all().loop (
asyncio.AbstractEventLoop) – The event loop the client should use for asynchronous operations.
- intents#
The intents the client is using.
- Type
Intents
- loop#
The event loop used by the client for asynchronous operations.
- @on_event(event)#
Decorator to register event listeners to the client. The function decorated must be a coroutine. This decorator uses
Client.add_listener()to register the listener.
- property user#
The user object belonnging to the client.
- Type
- property channels#
A list of all the channels cached by the client.
- Type
List[Union[
TextChannel,DMChannel]]
- property dm_channels#
A list of all the direct message channels cached by the client.
- Type
List[
DMChannel]
- property session#
The client session used by the http client for making requests to the Discord API.
- Type
aiohttp.ClientSession
- property shards#
All the shards connected to the gateway.
- Type
List[
Shard]
- async run_task(*args, reconnect=True, compress=True, **kwargs)#
This method is a coroutine. It is used to start the client, i.e., connect to gateway API and the REST API. It prepares the client completely.
- Returns
The client itself.
- Return type
- run(*args, **kwargs)#
Synchronous alternative to
Client.run_task(). Usesasyncio.run()internally to run the bot.Warning
Code written after this function is called will not get executed till the bot stops.
- Returns
The client itself.
- Return type
- async close()#
Closes the client, the http client, and the gateway connection.
- add_listener(coro, event)#
This function registers a listener to the client.
- Parameters
coro (
Coro) – A callable coroutine.event (
str) – The event to which the listener should be registered to. This event must be a valid event documented underGatewayEvent.
- Returns
The coroutine passed in as a parameter.
- Return type
Coro- Raises
TypeError – The coro passed in parameters isn’t a coroutine.
- async dispatch(event, *args, **kwargs)#
- async wait_for(event, check, *, timeout=30)#
Waits for a dispatch event from the websocket.
This method can be used to wait for a user to send a message that matches the passed check.
- Parameters
event (
str) – The event to wait for. Must be a valid event documented underGatewayEvent.check (Union[Callable[True], Coro]) – The check that should be run on the event. Can either be a normal function or a coroutine, with valid parameters specified under
GatewayEvent. Must returnTrueif the check is successful.
- Returns
The future created to wait for the event.
- Return type
- Raises
asyncio.TimeoutError – The timeout has finished.
Event Reference#
Discode allows users to listen for dispatch events received thru the gateway. All events must be registered in the following format:
@client.on_event(discode.GatewayEvent.READY)
async def on_ready():
print(client.user, "is ready"!)
See GatewayEvent for more information on dispatch events.
- class discode.GatewayEvent#
This class has all available gateway events, and information on how to register a listener for each of the events. An event maybe registered like this:
@client.event(GatewayEvent.{EVENT_NAME}) async def example_listener(*args, **kwargs): ...
- DISPATCH = 'dispatch'#
Dispatch is called whenever the websocket receives an event. Listeners waiting for dispatch must have 1 parameter- ‘payload’
- READY = 'ready'#
Dispatched when the client is completely ready. Listeners waiting for this event must not have any parameters.
- SHARD_READY = 'shard_ready'#
Dispatched when a shard is ready. This event takes 1 parameter- ‘shard_id’
- GUILD_CREATE = 'guild_create'#
Dispatched when the client joins a new guild, or the client receives data on a guild it is already in. This event takes 1 parameter- ‘guild’
- GUILD_UPDATE = 'guild_update'#
Dispatched when a guild that the client is already in gets updated. This event takes two parameters- ‘before’ & ‘after’
- GUILD_DELETE = 'guild_delete'#
Dispatched when the client gets removed from a guild, or the guild itself gets deleted by the owner. This event takes 1 parameter- ‘guild’
- MESSAGE_CREATE = 'message_create'#
Dispatched when a user / bot sends a message. This event takes 1 parameter- ‘message’
- MESSAGE_UPDATE = 'message_update'#
Dispatched when a user/bot edits a message. This event takes 2 parameters- ‘before’ & ‘after’
- MESSAGE_EDIT = 'message_update'#
Enumerations#
This section describes in detail all the available enumerations Discode Provides.
Utilities#
Discode provides a wide range of utilities for making developing Discord bots, and Discord related projects for developers. These are some of the documented utilities.
- discode.utils.invite_url(client_id, *, permissions=..., scopes=..., redirect_uri=...)#
Generates an invite url based on the given parameters for the client.
- Returns
The invite url generated.
- Return type
- discode.utils.get_event_loop()#
Alternative to
asyncio.get_event_loop(). Creates a new loop or fetches an existing event loop and returns it. AvoidsDeprecationWarningwarning to be thrown while called in Python v3.10 and higher versions.- Returns
The existing or newly created loop.
- Return type
- async discode.utils.run_async(func, *args, **kwargs)#
Run a function asynchronously and wait for it to complete.
- Returns
The return value of the function.
- Return type
Any
- discode.utils.escape_markdown(text)#
Get rid of Discord markdown highlighting from given text.
- @discode.utils.async_function#
Decorator to make synchronous function asynchronous. Useful with modules like Pillow, which have a synchronous backend but are extremely useful in image manipulation.
Usage of this decorator is equivalant to:
# inside coroutine: await get_event_loop().run_in_executor( None, functools.partial(sync_function, *args, **kwargs) )
Example
@discode.utils.async_function def sync_function(msg: discode.Message): do_synchronous_operations_with_message(msg) @client.on_event("message_create") async def on_message(msg): await sync_function(msg)
- @discode.utils.deprecated(instead=None, from_version='2.0.0b2')#
Deprecate a function with this decorator.
Example
@utils.deprecated(instead = "new_function") def deprecated_function(*args, **kwargs)): ... # somewhere else in code: depreceated_function(...) # Now this warns the user not to use the function and instead use an alternative method to do so..
Models#
Asset#
Warning
This class is not to be instanized manually.
- class discode.Asset(connection, *, url, key, animated=False)#
Represents a Discord Asset. An asset can be either of the following: user avatar, user banner, guild icon, guild banner.
ClientUser#
Warning
This class is not to be instanized manually.
- asyncsend
DMChannel#
Warning
This class is not to be instanized manually.
- asyncsend
Emoji#
Warning
This class is not to be instanized manually.
- class discode.Emoji(connection, payload)#
Guild#
Warning
This class is not to be instanized manually.
- defget_member
- class discode.Guild(connection, payload)#
Represents a Discord Guild.
- get_member(member_id)#
Get a
Memberfrom the cache who matches the member_id parameter.- Returns
The member that was retrieved from the cache
- Return type
- property channels#
All the channels attached to the guilds cached by the client.
- Type
List[
TextChannel]
- property text_channels#
All the text channels cached by the guild.
- Type
List[
TextChannel]
Member#
Warning
This class is not to be instanized manually.
Message#
Warning
This class is not to be instanized manually.
- class discode.Message(connection, payload)#
MessageReference#
Warning
This class is not to be instanized manually.
- class discode.MessageReference(connection, payload)#
Role#
Warning
This class is not to be instanized manually.
- class discode.Role(connection, payload)#
Represents a Discord role.
TextChannel#
Warning
This class is not to be instanized manually.
User#
Warning
This class is not to be instanized manually.
- asyncsend
Dataclasses#
Embed#
File#
- class discode.File(fp, *, filename=None, spoiler=False)#