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¶
- defbind_token
- asyncchange_presence
- asynccreate_stage_instance
- defdispatch
- asyncfetch_channel
- asyncfetch_guild
- asyncfetch_message
- asyncfetch_user
- defget_channel
- defget_guild
- defget_message
- defget_user
- asyncgof_channel
- defon
- asyncon_error
- asyncresume
- defrun
- asyncwait_for
- 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 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 to0encoding (
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 toFalse
- loop¶
Loop client uses
- Type
- gateway_version¶
Selected gateway version, available after connecting to gateway. In form
v[0-9].- Type
- INTERNAL_STORAGE¶
Cache of gateway objects, recomended to fetch using built in methods, e.g.
Client.get_user().- Type
- async change_presence(presence: acord.bases.presence.Presence) None¶
This function is a coroutine.
Changes client presence
- 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 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)
- 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 fieldsapproximate_presence_count,approximate_member_countto 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
NotFoundif 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)
- 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 bothClient.tokenand are overwritten. Else, this token will be used to connect to gateway, if fails falls back ontoClient.token.reconnect (
bool) – Whether to reconnect it first connection fails, defaults toTrue.
- 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}")