Client

The gateway client

class cordy.client.Client(token: StrOrToken, *, intents: Intents | None = None, sharder_cls: type[BaseSharder] = <class 'cordy.gateway.Sharder'>, num_shards: int | None = None, shard_ids: Sequence[int] | None = None)[source]

The discord API Gateway client.

token

The gateway authorization token.

Type

Token

intents

The gateway intents used by the client

Type

Intent

emitter

The client event emitter. You don’t need to interact with this if you do not want custom events

Type

Emitter

publisher

The event publisher.

Type

Publisher

shard_ids

The shard ids to launch. If None then sharder automatically shards the client

Type

set, (set[int] | None)

num_shard

The total number of shards the client has, this number includes shards which are not under this client.

Type

int, (int | None)

sharder

The gateway sharder for the client.

Type

BaseSharder, (BaseSharder[Shard])

Parameters
  • token (str, Token, (str | Token)) – The token for the gateway. Is a string is provided then a bot token is built. This is a required argument. All following arguments are keyword only.

  • intents (Intent) – This is an optional parameter. The gateway intents used by the client. If None, then value returned by default() is used. by default None.

  • sharder_cls (type[BaseSharder]) – The sharder type to use. Must subclass the BaseSharder protocol. If None then Sharder is used. By default None

  • num_shards (int) – The total number of shards. If None Client.sharder provided value will be used. By default None

  • shard_ids (Sequence[int]) – A sequence of shard ids that this client should launch. If not None then num_shards parameter must be provided. If None then shards are generated from num_shards or Client.sharder provided value will be used.

property shards: list[Shard]

All the shards under this client

Type

list[Shard]

listen(name: str | None = None) Callable[[CoroFn], CoroFn][source]

This method is used as a decorator. Add the decorated function as a listener for the specified event

Parameters

name (str) – The name of the event. If None then the name of the function is used, by default None.

Returns

The decorator.

Return type

Callable

add_listener(func: CoroFn, name: str | None = None) None[source]

Add a listener for the given event.

Parameters
  • func (Callable[..., Coroutine]) – The coroutine function to add as a listener. This is a required argument.

  • name (str) – The name of the event. If None then the name of the function is used, by default None.

remove_listener(func: CoroFn, name: str | None = None) None[source]

Remove a registered listener. If the listener or event is not found, then does nothing.

Parameters
  • func (Callable[..., Coroutine]) – The coroutine function which needs to be unsubscribed. This is a required argument.

  • name (str) – The name of the event. If None then the name of the function is used, by default None.

async wait_for(name: str, timeout: int | None = None, check: CheckFn | None = None) tuple[Any, ...][source]

Wait for an event to occur.

Parameters
  • name (str) – The name of the event to wait for.

  • timeout (int) – The time to wait for the event to occur, if None then wait indefinetly, by default None

  • check (Callable[…, bool]) – The check function, a subroutine returning a boolean, the provided arguments are the same as the returned tuple. If the check function returns True then the same data that was given to the check function will be returned, or else continue waiting for the event. If None then return the first event data, by default None

Returns

The data associated with the event. This is the same as the arguments received by listeners for the event.

Return type

tuple[Any, …]

async setup() None[source]

Initialise client with the current running event loop. This is implicity called when the gateway is launched, otherwise this needs to be called explicitly with a running loop.

async connect() None[source]

Launch all shards and connect to gateway.

async disconnect(*, code: int = 4000, message: str = '') None[source]

Disconnect all shards from the gateway

Parameters
  • code (int) – Optional status code to close the gateway connection, by default 4000

  • message (str) – A message to close the gateway connection, by default an empty string.

async reconnect() None[source]

Reconnect all shards to the gateway

async close() None[source]

Close the client permanently.