Anarchy
Search…
Clients
Clients are, in essence, the central point for all your interactions with the API.

Authorization tokens

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 '.

REST clients

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.
1
// DiscordConfig.RetryOnRateLimit is true by default, this is just an example
2
DiscordClient client = new DiscordClient("tokens can be set like this", new DiscordConfig() { RetryOnRateLimit = true });
3
client.Token = "or like this";
4
5
GuildInvite inv = client.JoinGuild("fortnite");
6
inv.Guild.Leave();
Copied!
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.

Gateway clients

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.
1
static void Main(string[] args)
2
{
3
DiscordSocketClient client = new DiscordSocketClient(new DiscordSocketConfig()
4
{
5
// fun fact: intents are required for bots as of v8, which Anarchy uses by default.
6
Intents = DiscordGatewayIntent.Guilds | DiscordGatewayIntent.GuildMessages
7
});
8
client.OnLoggedIn += client_OnLoggedIn;
9
client.OnMessageReceived += client_OnMessageReceived;
10
client.Login("your token here");
11
12
// The -1 will make the thread sleep until the program is closed.
13
Thread.Sleep(-1);
14
}
15
16
private void client_OnLoggedIn(DiscordSocketClient client, LoginEventArgs args)
17
{
18
client.SetActivity(new StreamActivityProperties()
19
{
20
Name = "Powered by Anarchy",
21
Url = "https://www.twitch.tv/ilinked" // the url must be a twitch channel
22
});
23
}
24
25
private void client_OnMessageReceived(DiscordSocketClient client, MessageEventArgs args)
26
{
27
if (args.Message.Mentions.Any(u => u.Id == client.User.Id) || args.Message.MentionedEveryone)
28
args.Message.Channel.SendMessage("shut up, retard");
29
}
Copied!
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.

Configuration

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.
Last modified 1yr ago