Clients
Last updated
Was this helpful?
Last updated
Was this helpful?
Clients are, in essence, the central point for all your interactions with the API.
Discord authorization tokens are how discord authorizes requests, hence the name. Tokens are changed if a user account changes their password, or if the owner of a bot "regenerates" the token.
If you're trying to use a bot account, you must prefix your token with 'Bot '.
Discord's REST API offers hundreds of endpoints to do everything from sending messages to deleting your account. Anarchy's response to this is the DiscordClient class, which contains methods for calling pretty much any of these endpoints. It's very lightweight because it doesn't need to connect to a websocket, making it the preferred solution for bots that deal with lots of user accounts, such as raidbots, but a bad choice for bots that respond to commands, since it doesn't receive events.
Here's an example of how one might use DiscordClients.
Whenever a token is set, it'll be soft-validated (it's checked whether or not the token is valid, not if it's locked) by requesting /users/@me, getting the local user in the process. The client's own user can be accessed through .User.
The term "REST client" may be used as a synonym for DiscordClient throughout the docs.
On top of the REST API, Discord offers a WebSocket API, which is referred to as the "Gateway". The Gateway allows you to receive events about things like incoming messages, user bans and friend requests, as well as other things like joining and connecting to voice channels and presences. Anarchy's response to this is the DiscordSocketClient, which inherits from DiscordClient, allowing it to send REST requests and use the Gateway. This is pretty much the only sane option for a bot responding to commands, as receiving events about incoming messages is a lot more efficient then requesting messages from all the channels it has access to.
Here's an example of how one might use such clients.
As is shown in the example, DiscordSocketClients also use events to tell you when they've logged in/out. The OnMessageReceived event could be used to listen for commands, however using Anarchy's built-in command handler is much easier.
The term "Gateway client" may be used as a synonym for DiscordSocketClient throughout the docs.
Anarchy allows great customization of the clients, which come in the form of Config classes. The config classes follow the same inheritance logic as the clients, with DiscordSocketConfig adding options on top of the ones DiscordConfig provides.
Configs are passed in the constructor of clients, and cannot be modified after. A client's config can be accessed through .Config, which is a "locked" version of the passed config.