Skip to main content
The Client class is the central object in every alterself application. When you instantiate it, you get a single object that manages your Discord gateway WebSocket connection, sends and receives HTTP API requests, maintains an in-memory state cache of guilds, channels, users, and messages, routes raw gateway payloads through the event bus, and dispatches prefix commands to your registered handlers. You interact with Discord almost entirely through this one object.

Constructor Parameters

Create a client by passing your token and any optional configuration:
Your token is a user account credential, not a bot token. Never commit it to source control. Load it from an environment variable or a secrets manager.

Static vs Dynamic Prefix

You can supply a plain string prefix, or a coroutine (or regular function) that computes the prefix per message. Dynamic prefixes let you use different prefixes in different guilds, for different users, or based on any other runtime condition.
Dynamic prefix callables can be async or plain synchronous functions. alterself awaits them automatically when they are coroutines.

Running the Client

alterself gives you two ways to start the client: a blocking helper for simple scripts, and async entry points for when you need fine-grained lifecycle control.
1

Blocking run (recommended for simple bots)

Call bot.run() at the bottom of your script. It creates an event loop, connects to the gateway, and blocks until the process is interrupted.
2

Async start and close

Use await bot.start() and await bot.close() inside your own async entry point when you need to run other coroutines alongside the bot.
3

Waiting until ready

After calling await bot.start(), the client is connected but may not have finished processing the initial READY payload and populating the cache. Await bot.wait_until_ready() before accessing guilds, channels, or bot.me.
Raises asyncio.TimeoutError if the bot does not become ready within the given number of seconds.

Properties

Once the client is running and ready, the following read-only properties are available:

Changing Presence

You can update your user’s online status and activity at any time after READY using bot.change_presence().
Presence updates are sent over the gateway. Discord rate-limits presence updates to a few per minute, so avoid calling change_presence() in a tight loop.

Cache Access

alterself keeps an internal state cache that is populated and updated as gateway events arrive. You can read from it directly when you need an object that was not passed to your handler:
All getters return None when the object is not present in the cache. Objects may be absent if they arrived before the cache was ready, or if they fall outside the configured cache limits.
The underscore prefix on _cache signals that it is an internal implementation detail. Its API may change between minor versions. For most use cases, prefer accessing objects through event handler arguments or context attributes.

Direct HTTP Access

For Discord API calls that are not wrapped by a high-level method, you can use the underlying HTTP client directly:
bot.http is an instance of alterself.http.HTTPClient. It handles authorization headers, rate-limit buckets, and automatic retries on 429 responses. You can also call bot.http.request(endpoint) with a raw Endpoint object for any Discord API call not covered by a named helper. For a full list of available methods, see the HTTP Methods reference.