# 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 '](https://discord.com/developers/docs/reference#authentication-example-bot-token-authorization-header).

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

```csharp
// DiscordConfig.RetryOnRateLimit is true by default, this is just an example
DiscordClient client = new DiscordClient("tokens can be set like this", new DiscordConfig() { RetryOnRateLimit = true });
client.Token = "or like this";

GuildInvite inv = client.JoinGuild("fortnite");
inv.Guild.Leave();
```

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.

{% content-ref url="clients/discordclient" %}
[discordclient](clients/discordclient)
{% endcontent-ref %}

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

```csharp
static void Main(string[] args)
{
    DiscordSocketClient client = new DiscordSocketClient(new DiscordSocketConfig() 
    {
        // fun fact: intents are required for bots as of v8, which Anarchy uses by default.
        Intents = DiscordGatewayIntent.Guilds | DiscordGatewayIntent.GuildMessages
    });
    client.OnLoggedIn += client_OnLoggedIn;
    client.OnMessageReceived += client_OnMessageReceived;
    client.Login("your token here");
    
    // The -1 will make the thread sleep until the program is closed.
    Thread.Sleep(-1);
}

private void client_OnLoggedIn(DiscordSocketClient client, LoginEventArgs args)
{
    client.SetActivity(new StreamActivityProperties()
    {
        Name = "Powered by Anarchy",
        Url = "https://www.twitch.tv/ilinked" // the url must be a twitch channel
    });
}

private void client_OnMessageReceived(DiscordSocketClient client, MessageEventArgs args)
{
    if (args.Message.Mentions.Any(u => u.Id == client.User.Id) || args.Message.MentionedEveryone)
        args.Message.Channel.SendMessage("shut up, retard");
}
```

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.

{% content-ref url="clients/discordsocketclient" %}
[discordsocketclient](clients/discordsocketclient)
{% endcontent-ref %}

{% content-ref url="users" %}
[users](users)
{% endcontent-ref %}

{% content-ref url="channels" %}
[channels](channels)
{% endcontent-ref %}

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

{% content-ref url="clients/configuration/discordconfig" %}
[discordconfig](clients/configuration/discordconfig)
{% endcontent-ref %}

{% content-ref url="clients/configuration/discordsocketconfig" %}
[discordsocketconfig](clients/configuration/discordsocketconfig)
{% endcontent-ref %}
