# Guilds

## Guilds vs. servers

I often see people confusing guilds with servers, so i thought i'd clear things up real quick.

[A "guild" is the technical term for what's known as a "server" in the UI](https://discord.com/developers/docs/resources/guild#guild-resource). What i call "servers" throughout the docs are actual servers that u can connect to, such as the media servers used to speak and livestream in voice channels.

## Different guild objects

Discord will often only provide parts of a guild object. Anarchy therefore has various guild classes that vary in information to fit what's given to us. Here's a list of all the different classes:

* MinimalGuild: The class with the least possible information. The only property is .Id, but that's enough to call most of the endpoints.
* PartialGuild: Returned only by the .GetGuilds() method. Only unique feature of this class is that all your permissions are present.
* DiscordGuild: Closest to you'll get to a full guild through the REST API.
* UnavailableGuild: Only used in the OnLeftGuild event. The .Removed member can be used to check whether the user was actually removed, or if the server is having an outage.
* SocketGuild: These are only provided through the gateway. These are provided to you through events, as well as .GetCachedGuilds() and .GetCachedGuild() if you have caching enabled (duh).

{% content-ref url="guilds/minimalguild" %}
[minimalguild](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/minimalguild)
{% endcontent-ref %}

{% content-ref url="guilds/partialguild" %}
[partialguild](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/partialguild)
{% endcontent-ref %}

{% content-ref url="guilds/discordguild" %}
[discordguild](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/discordguild)
{% endcontent-ref %}

{% content-ref url="guilds/unavailableguild" %}
[unavailableguild](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/unavailableguild)
{% endcontent-ref %}

{% content-ref url="guilds/socketguild" %}
[socketguild](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/socketguild)
{% endcontent-ref %}

## Permissions

Permissions are a crucial part of Discord guilds. They're what decides what a member can and cannot do. Behind the scenes, permissions are a number comprised of multiple values OR'd. The DiscordPermission maps the values, so just doing the bitwise operations yourself works, although there are some extension methods available to make your life easier.

#### Manual bitwise operations

```csharp
DiscordPermission perms = DiscordPermission.None; // create empty set
perms |= DiscordPermission.KickMembers; // add a permission
perms &= ~DiscordPermission.KickMembers; // remove the permission
perms.HasFlag(DiscordPermission.KickMembers); // returns false
```

#### Using the helper methods

```csharp
DiscordPermission perms = DiscordPermission.None
                          .Add(DiscordPermission.KickMembers)
                          .Remove(DiscordPermission.KickMembers);

perms.Has(DiscordPermission.KickMembers); // returns false
```

Multiple permissions can, in multiple examples, be checked for / added / removed in a single call.

### Permission overwrites

Discord allows overwriting of members/roles in the scope of a singular (or more because of synchronized permissions) channel. A GuildChannel's permission overwrites are located at .PermissionOverwrites.

GuildMember.GetPermissions() has an overload accepting an `IEnumerable<DiscordPermissionOverwrite>`. Example:

```csharp
member.GetPermissions().Has(DiscordPermission.SendMessages); // returns true
member.GetPermissions(channel.PermissionOverwrites)... // returns false
```

In this example, the user is allowed to send messages according to the roles, but an overwrite, well... overwrites that.

{% content-ref url="guilds/discordpermission" %}
[discordpermission](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/discordpermission)
{% endcontent-ref %}

{% content-ref url="channels/guild/discordpermissionoverwrite" %}
[discordpermissionoverwrite](https://ilinked1337.gitbook.io/anarchy/documentation/channels/guild/discordpermissionoverwrite)
{% endcontent-ref %}

{% content-ref url="channels" %}
[channels](https://ilinked1337.gitbook.io/anarchy/documentation/channels)
{% endcontent-ref %}

## Members

Discord have recently removed the ability for user accounts to request members the same way bot accounts do. Anarchy has methods for both, so make sure to use the right one!

Bots can use the .GetGuildMembers() method, whilst users can only use .GetGuildChannelMembers(). The method for users works like Discord's Member List, meaning that it only gets members that have access to it. Both methods return an `IReadOnlyList<GuildMember>` and are available for gateway clients only.

{% content-ref url="guilds/members/guildmember" %}
[guildmember](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/members/guildmember)
{% endcontent-ref %}

{% content-ref url="users" %}
[users](https://ilinked1337.gitbook.io/anarchy/documentation/users)
{% endcontent-ref %}

## Webhooks

There are 2 types of webhooks: incoming webhooks, which send messages and are authenticated using a token, and channel followers that crosspost messages from news channels. Anarchy has extensive functionality for both of them.

#### Requesting a webhook

```csharp
ulong hookId = 42069999;
string token = "jUEugVMozmyy4lnU1tlPXKVFjWnSYZeAmABcttxy5KW5vYJyA_YDCmjtR2WJuKaul2RV";

// Gets a webhook by it's ID and token.
// If you have the ManageWebhooks permission on it's parent guild, you don't have to specify the token.
DiscordWebhook hook = client.GetWebhook(hookId, token);
Console.WriteLine(hook.Name);

// You can also do this if you don't want to create a client and have it's token.
// This constructor only exists in DiscordDefaultWebhook because channel followers don't have tokens.
DiscordDefaultWebhook hook = new DiscordDefaultWebhook(hookId, token);
```

### Incoming webhooks

Incoming webhooks are the classic webhooks we all know and love. They simply send messages as you command.

#### Sending messages

```csharp
DiscordDefaultWebhook hook = (DiscordDefaultWebhook)client.GetWebhook(42069999);

// All parameters except the first are optional
hook.SendMessage("test", null, new DiscordWebhookProfile()
{
    Username = "joe mama",
    AvatarUrl = "https://anarchyteam.dev/favicon.ico"
});
```

### Channel followers

Channel followers, or crossposters, are webhooks that send messages from a news channel to the specified channel, hence the term "crossposting".

In the following examples, the 'sourceChannel' will be a TextChannel with a .Type of News.

#### Creating crossposters

```csharp
sourceChannel.Follow(42069425); // pass the ID of your own channel here
```

#### Crossposting/publishing messages

If you have permissions to manage the news channel, you can "publish" messages, which will forward them to all the channel followers for the channel.

```csharp
foreach (DiscordMessage message in sourceChannel.GetMessages(new MessageFilters() { Limit = 50 }))
    message.Crosspost();
```

{% content-ref url="guilds/webhooks/discordwebhook" %}
[discordwebhook](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/webhooks/discordwebhook)
{% endcontent-ref %}

{% content-ref url="guilds/webhooks/discorddefaultwebhook" %}
[discorddefaultwebhook](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/webhooks/discorddefaultwebhook)
{% endcontent-ref %}

{% content-ref url="guilds/webhooks/discordcrosspostwebhook" %}
[discordcrosspostwebhook](https://ilinked1337.gitbook.io/anarchy/documentation/guilds/webhooks/discordcrosspostwebhook)
{% endcontent-ref %}

{% content-ref url="channels" %}
[channels](https://ilinked1337.gitbook.io/anarchy/documentation/channels)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ilinked1337.gitbook.io/anarchy/documentation/guilds.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
