Client¶
ACord’s built in client enables rapid development, but this doesn’t mean you can’t create your own!
Example¶
from acord import Client, Message, Intents
from typing import Any
class MyClient(Client):
async def on_message_create(self, message: Message) -> Any:
if message.content == ".ping":
return message.reply(content="Pong!")
if __name__ == "__main__":
client = MyClient(token="My token", intents=Intents.ALL)
client.run()
Client¶
- asyncbulk_update_global_app_commands
- asyncbulk_update_guild_app_commands
- asynccreate_application_command
- asynccreate_stage_instance
- asyncdisconnect
- defdispatch
- asyncfetch_channel
- asyncfetch_glob_app_command
- asyncfetch_glob_app_commands
- asyncfetch_guild
- asyncfetch_message
- asyncfetch_user
- defget_channel
- defget_guild
- defget_message
- defget_shard
- defget_user
- defon
- asyncon_error
- defregister_application_command
- defrun
- asyncshard_handler
- asyncwait_for
- class acord.Client(*, token: typing.Optional[str] = None, dispatch_on_recv: bool = False, intents: typing.Optional[typing.Union[acord.bases.flags.intents.Intents, int]] = 0, presence: typing.Optional[acord.bases.presence.Presence] = None, loop: typing.Optional[asyncio.events.AbstractEventLoop] = <_UnixSelectorEventLoop running=False closed=False debug=False>, encoding: typing.Optional[str] = 'JSON', compress: typing.Optional[bool] = False, cache: acord.client.caches.cache.Cache = DefaultCache(sections={'messages': {}, 'users': <WeakValueDictionary>, 'guilds': {}, 'channels': {}, 'stage_instances': {}}), gateway_ratelimiter: acord.client.ratelimiter.GatewayRatelimiter = DefaultGatewayRatelimiter(max_requests=(117, 60), current_requests={}, refreshed_at=1647170145.1627147, tasks={}, shards={}))[source]¶
Client for interacting with the discord API
- Parameters
loop (
AbstractEventLoop) – An existing loop to run the client off oftoken (
str) – Your API Token which can be generated at the developer portalintents (Union[
Intents,int]) – Intents to be passed through when connecting to gateway, defaults to0presence (
Presence) – Presence to be sent in the identity packetencoding (
str) – Any ofETFandJSONare allowed to be chosen, controls data recieved by discord, defaults toFalse.compress (
bool) – Whether to read compressed stream when receiving requests, defaults toFalsedispatch_on_recv (
bool) – Whether on_socket_recv should be dispatchedcache (
Cache) –Cache for the client to use
New in version 0.2.3a0.
gateway_ratelimiter (
GatewayRatelimiter) –Gateway ratelimiter for client to use
New in version 0.2.3a0.
- loop¶
Loop client uses
- Type
- presence¶
Presence to be sent in the identity packet
- Type
Presence
- gateway_version¶
Selected gateway version, available after connecting to gateway. In form
v[0-9].- Type
- user¶
Client user object
- Type
User
- application_commands¶
Mapping of registered application commands
- Type
Dict[str, List[
UDAppCommand]]
- cache¶
Cache of gateway objects, recommended to fetch using built in methods, e.g.
Client.get_user().New in version 0.2.3a0.
- Type
Cache
- gateway_ratelimiter¶
Gateway ratelimiter for client to use, we recommend not playing around with this as it may lead to unexpected errors
New in version 0.2.3a0.
- Type
GatewayRatelimiter
- max_concurrency¶
Number of identity requests client is allowed per 5 seconds
New in version 0.2.3a0.
Changed in version 0.2.3a0: Name changed from MAX_CONC to max_concurrency
- Type
- num_shards¶
Number of shards client is using, if None it means client has not been ran yet.
Note
This value can be overwritten but only before client is ran. We dont recommend you do but oh well.
New in version 0.2.3a0.
- Type
Optional[
int]
- guilds¶
List of guilds client has access to
- Type
List[
Guilds]
- rest¶
An instance of the Rest API object
- Type
RestApi
- async bulk_update_global_app_commands(commands: List[acord.ext.application_commands.base.UDAppCommand]) None[source]¶
This function is a coroutine.
Updates global application commands in bulk
- Parameters
commands (List[
UDAppCommand]) – List of application commands to update
- async bulk_update_guild_app_commands(guild_id: acord.models.Snowflake, commands: List[acord.ext.application_commands.base.UDAppCommand]) None[source]¶
This function is a coroutine.
Updates application commands for a guild in bulk
- Parameters
guild_id (
Snowflake) – ID of target guildcommands (List[
UDAppCommand]) – List of application commands to update
- async create_application_command(command: acord.ext.application_commands.base.UDAppCommand, *, guild_ids: Optional[List[int]] = None, extend: bool = True) Union[acord.ext.application_commands.base.ApplicationCommand, List[acord.ext.application_commands.base.ApplicationCommand]][source]¶
This function is a coroutine.
Creates an application command from a
UDAppCommandclass.Note
It can take up to an hour for discord to process the command!
:param same as
Client.register_application_command():
- async create_stage_instance(*, reason: Optional[str] = None, **data) acord.models.channels.stage.StageInstance[source]¶
This function is a coroutine.
Creates a stage instance
- Parameters
channel_id (
Snowflake) – ID of channel to create stage instance, channel type must beChannelTypes.GUILD_STAGE_VOICEtopic (
str) – The topic of the Stage instance (1-120 characters)privacy_level (
StagePrivacyLevel) – The privacy level of the Stage instance (default GUILD_ONLY)
- async disconnect()[source]¶
This function is a coroutine.
Disconnects client from discord, ends:
Client spawned sessions
Shards
Voice Connections
- dispatch(event_name: str, *args, **kwargs) None[source]¶
Dispatch a registered event
- Parameters
event_name (
str) – Name of event*args – Additional args or kwargs to be passed through
**kwargs – Additional args or kwargs to be passed through
- async fetch_channel(channel_id: int) Optional[acord.models.channels.base.Channel][source]¶
Fetches channel from API and caches it
- async fetch_glob_app_command(command_id: acord.models.Snowflake) acord.ext.application_commands.base.ApplicationCommand[source]¶
This function is a coroutine.
Fetches a global application command registered by the client
- Parameters
command_id (
Snowflake) – ID of command to fetch
- async fetch_glob_app_commands() AsyncIterator[acord.ext.application_commands.base.ApplicationCommand][source]¶
This function is a coroutine.
Fetches all global application commands registered by the client
- async fetch_guild(guild_id: int, *, with_counts: bool = False) Optional[acord.models.guild.Guild][source]¶
Fetches guild from API and caches it.
Note
If with_counts is set to
True, it will allow fieldsapproximate_presence_count,approximate_member_countto be used.
- async fetch_message(channel_id: int, message_id: int) Optional[acord.models.message.Message][source]¶
Fetches message from API and caches it
- async fetch_user(user_id: int) Optional[acord.models.user.User][source]¶
Fetches user from API and caches it
- get_channel(channel_id: int) Optional[acord.models.channels.base.Channel][source]¶
Returns the channel stored in the internal cache, may be outdated
- get_guild(guild_id: int) Optional[acord.models.guild.Guild][source]¶
Returns the guild stored in the internal cache, may be outdated
- get_message(channel_id: int, message_id: int) Optional[acord.models.message.Message][source]¶
Returns the message stored in the internal cache, may be outdated
- get_shard(guild_id: acord.models.Snowflake) Optional[acord.client.shard.Shard][source]¶
Gets a shard from
Client.shardsusing a guild id- Parameters
guild_id (
Snowflake) – Guild ID to use
- get_user(user_id: int) Optional[acord.models.user.User][source]¶
Returns the user stored in the internal cache, may be outdated
- on(name: str, *, once: bool = False) Optional[Callable[[...], Coroutine]][source]¶
Register an event to be dispatched on call.
This is a decorator, if you do not want to use the decorator consider trying:
from acord import Client from xyz import some_event_handler client = Client(...) client.on("message")(some_event_handler)
- on_error(event_method, task: Optional[_asyncio.Task] = None, err=None)[source]¶
This function is a coroutine.
Built in base error handler for events
- register_application_command(command: acord.ext.application_commands.base.UDAppCommand, *, guild_ids: Optional[List[int]] = None, extend: bool = True) None[source]¶
Registers application command internally before client is ran, after client is ran this method is redundant. Consider using
Client.create_application_command().- Parameters
command (
UDAppCommand) –Note
UDAppCommandrepresents any class which inherits it, this includes SlashBase.Command to register internally, to be dispatched.
guild_ids (Union[List[
int], None]) –Additional guild IDs to restrict command to,
- if value is set to:
None: Reads from class (Default option)[](Empty List): Makes it global
Note
If final value is false, command will be registered globally
extend (
bool) – Whether to extend current guild ids from the command class
- run(token: Optional[str] = None, reconnect: bool = True, update_app_commands: bool = True, exclude_app_cmds: set = {}, asyncio_debug: bool = False)[source]¶
Runs client, loop blocking.
- Parameters
token (
str) – Token to log into discord with, ifClient.tokenis not None it will be used as a fallback, just in case this token failsreconnect (
bool) – Whether to reconnect it first connection fails, defaults toTrue.update_add_commands (
bool) – Whether to update app commands, in bulk.exclude_app_cmds (
set) – A set of app names to stop being updated/createdasyncio_debug (
bool) – Whether to enable debugging on the current loop.
- async shard_handler(*ready_scripts) None[source]¶
This function is a coroutine.
Default shard handler, doesn’t do much except intiate shards and waits till all have been disconnected.
- wait_for(event: str, *, check: Optional[Callable[[...], bool]] = None, timeout: Optional[int] = None) Callable[[...], Coroutine][source]¶
This function is a coroutine.
Wait for a specific gateway event to occur.
Examples
# Simple Greeting data = Client.wait_for( "message", check=lambda message: message.content == "Hello", timeout=30.0 ) message = data[0] return message.reply(content=f"Hello, {message.author}")