Client

ACord’s built in client enables rapid development, but this doesn’t mean you cant create your own!

Example

from acord import Client, Message
from typing import Any

class MyClient(Client):
    async def on_message(self, message: Message) -> Any:
        if message.content == ".ping":
            return message.reply(content="Pong!")

if __name__ == "__main__":
    client = MyClient(token="My token")
    client.run()

Client

class acord.Client(*, token: typing.Optional[str] = None, intents: typing.Optional[typing.Union[acord.bases.flags.intents.Intents, int]] = 0, loop: typing.Optional[asyncio.events.AbstractEventLoop] = <_UnixSelectorEventLoop running=False closed=False debug=False>, encoding: typing.Optional[str] = 'JSON', compress: typing.Optional[bool] = False)

Client for interacting with the discord API

Parameters
  • loop (AbstractEventLoop) – An existing loop to run the client off of

  • token (str) – Your API Token which can be generated at the developer portal

  • intents (Union[Intents, int]) – Intents to be passed through when connecting to gateway, defaults to 0

  • encoding (str) – Any of ETF and JSON are allowed to be chosen, controls data recieved by discord, defaults to False.

  • compress (bool) – Whether to read compressed stream when receiving requests, defaults to False

loop

Loop client uses

Type

AbstractEventLoop

token

Token set when initialising class

Type

str

intents

Intents set when intialising class

Type

Union[Intents, int]

encoding

Encoding set when initialising class

Type

str

compress

Whether to read compressed stream when receiving requests

Type

bool

session_id

Session ID recieved when connecting to gateway

Type

str

gateway_version

Selected gateway version, available after connecting to gateway. In form v[0-9].

Type

str

user

Client user object

Type

User

INTERNAL_STORAGE

Cache of gateway objects, recomended to fetch using built in methods, e.g. Client.get_user().

Type

dict

bind_token(token: str) None

Bind a token to the client, prevents new tokens from being set

async change_presence(presence: acord.bases.presence.Presence) None

This function is a coroutine.

Changes client presence

Parameters

presence (Presence) – New presence for client, You may want to checkout the guide for presences. Which can be found here.

async create_stage_instance(*, reason: Optional[str] = None, **data) acord.models.channels.stage.Stage

This function is a coroutine.

Creates a stage instance

Parameters
  • channel_id (Snowflake) – ID of channel to create stage instance, channel type must be ChannelTypes.GUILD_STAGE_VOICE

  • topic (str) – The topic of the Stage instance (1-120 characters)

  • privacy_level (StagePrivacyLevel) – The privacy level of the Stage instance (default GUILD_ONLY)

dispatch(event_name: str, *args, **kwargs) None

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]

Fetches channel from API and caches it

async fetch_guild(guild_id: int, *, with_counts: bool = False) Optional[acord.models.guild.Guild]

Fetches guild from API and caches it.

Note

If with_counts is set to True, it will allow fields approximate_presence_count, approximate_member_count to be used.

async fetch_message(channel_id: int, message_id: int) Optional[acord.models.message.Message]

Fetches message from API and caches it

async fetch_user(user_id: int) Optional[acord.models.user.User]

Fetches user from API and caches it

get_channel(channel_id: int) Optional[acord.models.channels.base.Channel]

Returns the channel stored in the internal cache, may be outdated

get_guild(guild_id: int) Optional[acord.models.guild.Guild]

Returns the guild stored in the internal cache, may be outdated

get_message(channel_id: int, message_id: int) Optional[acord.models.message.Message]

Returns the message stored in the internal cache, may be outdated

get_user(user_id: int) Optional[acord.models.user.User]

Returns the user stored in the internal cache, may be outdated

async gof_channel(channel_id: int) Optional[Any]

Attempts to get a channel, if not found fetches and adds to cache. Raises NotFound if cannot be fetched

on(name: str, *, once: bool = False)

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)
Parameters
  • name (str) – Name of event, consider checking out all events

  • once (bool) – Whether the event should be ran once before being removed.

  • check (Callable[…, bool]) – Check to be ran before dispatching event

on_error(event_method)

This function is a coroutine.

Built in base error handler for events

async resume() None

This function is a coroutine.

Resumes a closed gateway connection, should only be called internally.

run(token: Optional[str] = None, *, reconnect: bool = True, resumed: bool = False)

Runs client, loop blocking.

Parameters
  • token (str) – Token to be passed through, if binded both Client.token and are overwritten. Else, this token will be used to connect to gateway, if fails falls back onto Client.token.

  • reconnect (bool) – Whether to reconnect it first connection fails, defaults to True.

wait_for(event: str, *, check: Optional[Callable[[...], bool]] = None, timeout: Optional[int] = None) Tuple[Any]

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}")
Parameters
  • event (str) – Gateway event to wait for.

  • check (Callable[…, bool]) – Validate the gateway event recieved

  • timeout (int) – Time to wait for event to be recieved