# Managing a server

> Creating, changing, ordering and deleting channels, their permission overwrites and roles, creating and revoking invites, and reading the audit log. What each route checks, the reason for the audit log, and the errors.

## Before you read

A bot manages a server with the same rules as a person in the app. For every route on this page:

- **The permission** is the one each route names, held by the bot in the server or, where it
  says so, in the channel. A timed-out bot has lost it.
- **Nothing it doesn’t have.** A role or an overwrite can only allow or deny permissions the bot
  has itself where it writes them. When an overwrite already exists, only the bits that change
  count: denying one more permission doesn’t ask again for the ones it already denied.
- **The role hierarchy.** The bot only changes, moves or deletes roles **below** its own highest
  role, and only writes overwrites for roles below it, for people below it, for `@everyone` and
  for itself. Being level isn’t enough. A role nobody can touch through the API: any bot’s own
  role (`managed: true`), the bot’s included.
- **`@everyone`** is the server’s ID, in roles and in overwrites, as you read it everywhere.
- **Discord’s bits** go in and come out the same, also those Klang doesn’t have, such as
  `SEND_TTS_MESSAGES`: Klang keeps them as they were sent, and they never allow anything. See
  [Permissions](https://docs.klang.gg/bots/differences/#permissions).

These routes need the bot to be in the server: for any other server they answer `404` with
`10004`, and a channel it can’t see is `404` with `10003`.

### The reason in the audit log

Send `X-Audit-Log-Reason`, URL-encoded (libraries do it when you pass a `reason`). Klang keeps up
to 512 characters in the server’s audit log for every change on this page, and for changes to
threads.

```http
POST /api/v10/guilds/1575146937681682432/channels
Authorization: Bot <token>
X-Audit-Log-Reason: Setting%20up%20the%20team
```

### What people see

Changes to channels, overwrites and roles reach everyone in the server at once: each person’s
view of it is rebuilt, with the channels they can see now. Bots get them as `CHANNEL_CREATE`,
`CHANNEL_UPDATE`, `CHANNEL_DELETE`, `GUILD_ROLE_CREATE`, `GUILD_ROLE_UPDATE` and
`GUILD_ROLE_DELETE`. Two changes to the same channel a moment apart can arrive as one event with
the final state. Invites have no events yet.

## Create a channel

```http
POST /guilds/{guild.id}/channels
```

Creates a text channel, a voice channel or a category, and answers `201` with the
[channel](https://docs.klang.gg/bots/reference/objects/#server-channel).

- **Needs**: `MANAGE_CHANNELS`. With `permission_overwrites`, also `MANAGE_ROLES` where the channel
  is born (its category, or the server), and each overwrite follows the rules above. They’re all
  checked first: if one isn’t allowed, the channel isn’t created.
- **Rate limit**: `channel-create`, per server, shared with the people in the app.

| Field                   | Type                                                                 | Notes                                                                                           |
| ----------------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `name`                  | string                                                               | 1 to 100 characters. A text channel’s goes to lower case with dashes for spaces, as in the app. |
| `type`                  | integer                                                              | `0` text (the default), `2` voice or `4` category. Anything else is a `50035`.                  |
| `topic`                 | string or `null`                                                     | Up to 1,024 characters. Text channels only.                                                     |
| `parent_id`             | category ID or `null`                                                | A category of the same server.                                                                  |
| `position`              | integer                                                              | Where it goes among the others. Without it, it goes last.                                       |
| `rate_limit_per_user`   | integer                                                              | Slowmode, 0 to 21,600 seconds.                                                                  |
| `user_limit`            | integer                                                              | Voice only. 0 to 99; 0 is no limit.                                                             |
| `nsfw`                  | boolean                                                              | Age-restricted.                                                                                 |
| `permission_overwrites` | array of [overwrites](https://docs.klang.gg/bots/reference/objects/#permission-overwrite) | Up to 500. `type` is `0` (role) or `1` (member); `allow` and `deny` are strings of digits.      |

`bitrate`, `video_quality_mode`, `default_auto_archive_duration` and `flags` are accepted and
ignored: Klang has no such settings. `rtc_region` and the forum fields only take `null`.

A bot that hides a channel from `@everyone` stops seeing it too, unless something gives it back:
add an overwrite for the bot itself, with its user ID and `type` `1`.

```json
{
  "name": "Staff Room",
  "topic": "Only for the team",
  "permission_overwrites": [
    { "id": "1575146937681682432", "type": 0, "deny": "1024" },
    { "id": "1575146937547464704", "type": 1, "allow": "1024" }
  ]
}
```

```json
{
  "id": "1575149467932102656",
  "guild_id": "1575146937681682432",
  "name": "staff-room",
  "position": 9,
  "permission_overwrites": [
    { "id": "1575146937547464704", "type": 1, "allow": "1024", "deny": "0" },
    { "id": "1575146937681682432", "type": 0, "allow": "0", "deny": "1024" }
  ],
  "nsfw": false,
  "flags": 0,
  "parent_id": null,
  "last_message_id": null,
  "rate_limit_per_user": 0,
  "type": 0,
  "topic": "Only for the team",
  "default_auto_archive_duration": 10080
}
```

| Status | Code    | When                                                                                     |
| ------ | ------- | ---------------------------------------------------------------------------------------- |
| `400`  | `50035` | A field isn’t valid, a type Klang doesn’t have, or the server’s plan has no room.        |
| `403`  | `50013` | Without the permission, or an overwrite allows or denies something the bot doesn’t have. |
| `404`  | `10003` | The category isn’t one of the server’s.                                                  |
| `404`  | `10004` | The bot isn’t in the server.                                                             |

## Change a channel

```http
PATCH /channels/{channel.id}
```

Changes what’s sent and answers `200` with the channel as it ended up. For a thread, see
[Change a thread](https://docs.klang.gg/bots/reference/threads/#change-a-thread).

- **Needs**: `MANAGE_CHANNELS` in the channel. Moving it to another category changes the
  permissions it inherits, so it also needs `MANAGE_ROLES` in the channel and in the category.
  `permission_overwrites` needs `MANAGE_ROLES`.
- **Rate limit**: `manage-channels`.

It takes the fields of [Create a channel](https://docs.klang.gg/bots/reference/management/#create-a-channel) except `type`, which can only be
the one it has (Klang doesn’t turn one kind of channel into another). `permission_overwrites`
replaces all of the channel’s own overwrites: they’re checked together before anything changes.
A `null` `topic` or `parent_id` removes it.

```json
{ "name": "team", "rate_limit_per_user": 10 }
```

| Status | Code    | When                                                                    |
| ------ | ------- | ----------------------------------------------------------------------- |
| `400`  | `50024` | It’s the bot’s direct message.                                          |
| `400`  | `50035` | A field isn’t valid, or `type` isn’t the channel’s.                     |
| `403`  | `50013` | Without the permission, or an overwrite asks for more than the bot has. |
| `404`  | `10003` | No such channel, or the bot can’t see it.                               |

## Delete a channel

```http
DELETE /channels/{channel.id}
```

Deletes it and answers `200` with the channel as it was. A category leaves its channels outside
any category, with the permissions they inherited from it. Deleting a text or voice channel
deletes its threads too.

- **Needs**: `MANAGE_CHANNELS` in the channel (a thread: `MANAGE_THREADS`).
- **Rate limit**: `manage-channels` (a thread: `thread-manage`).

## Order the channels

```http
PATCH /guilds/{guild.id}/channels
```

A list of `{ id, position, parent_id, lock_permissions }`, as many as you need, and answers
`204`.

- **Needs**: `MANAGE_CHANNELS`, and `MANAGE_ROLES` in the channel and the category for each one
  that changes category.
- **Rate limit**: `manage-channels`.

| Field              | Type                  | Notes                                                                                                                     |
| ------------------ | --------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `id`               | channel ID            | A channel of the server. Each one once.                                                                                   |
| `position`         | integer or `null`     | `null` or absent keeps its position.                                                                                      |
| `parent_id`        | category ID or `null` | Absent keeps its category; `null` takes it out.                                                                           |
| `lock_permissions` | boolean               | `true` removes the channel’s own overwrites, so it takes its category’s as they are. Needs `MANAGE_ROLES` in the channel. |

## Set an overwrite

```http
PUT /channels/{channel.id}/permissions/{overwrite.id}
```

Creates or replaces the overwrite of a role or a member in a channel or a category, and answers
`204`. Threads have no overwrites of their own: they take their channel’s.

- **Needs**: `MANAGE_ROLES` in the channel, and the rules above.
- **Rate limit**: `manage-channels`.

| Field   | Type    | Notes                                       |
| ------- | ------- | ------------------------------------------- |
| `type`  | integer | `0` a role (or `@everyone`), `1` a member.  |
| `allow` | string  | Discord’s bits, as digits. Absent is `"0"`. |
| `deny`  | string  | The same.                                   |

```json
{ "type": 0, "allow": "1024" }
```

## Remove an overwrite

```http
DELETE /channels/{channel.id}/permissions/{overwrite.id}
```

Answers `204`. Removing an overwrite that denied something is giving it back, so it follows the
same rules as setting one. An overwrite that isn’t there is `404` with `10009`.

## Create a role

```http
POST /guilds/{guild.id}/roles
```

Creates a role **at the bottom**, right above `@everyone`, and answers `200` with the
[role](https://docs.klang.gg/bots/reference/objects/#role). Move it with [Order the roles](https://docs.klang.gg/bots/reference/management/#order-the-roles).

- **Needs**: `MANAGE_ROLES`, and only permissions the bot has.
- **Rate limit**: `manage-roles`.

| Field         | Type    | Notes                                                                                   |
| ------------- | ------- | --------------------------------------------------------------------------------------- |
| `name`        | string  | Up to 100 characters. `new role` if absent.                                             |
| `permissions` | string  | Discord’s bits, as digits. `"0"` if absent.                                             |
| `color`       | integer | A colour as `0xRRGGBB`. In `colors`, only `primary_color`: Klang roles have one colour. |
| `hoist`       | boolean | Shown apart in the member list.                                                         |
| `mentionable` | boolean | Anyone can mention it.                                                                  |

`icon` and `unicode_emoji` only take `null`: Klang roles have no icons.

```json
{ "name": "Helpers", "color": 3381759, "permissions": "3072", "mentionable": true }
```

```json
{
  "id": "1575149468162789376",
  "name": "Helpers",
  "color": 3381759,
  "colors": { "primary_color": 3381759, "secondary_color": null, "tertiary_color": null },
  "hoist": false,
  "icon": null,
  "unicode_emoji": null,
  "position": 1,
  "permissions": "3072",
  "managed": false,
  "mentionable": true,
  "flags": 0
}
```

## Change a role

```http
PATCH /guilds/{guild.id}/roles/{role.id}
```

The same fields, only what’s sent, and answers `200` with the role. `@everyone`, by the server’s
ID, only changes its permissions and the rest but its name and colour.

## Delete a role

```http
DELETE /guilds/{guild.id}/roles/{role.id}
```

Answers `204`. `@everyone` can’t be deleted, and neither can a bot’s role: removing the bot
removes it.

| Status | Code    | When                                                                                                           |
| ------ | ------- | -------------------------------------------------------------------------------------------------------------- |
| `400`  | `50035` | Changing `@everyone`’s name or colour, or a field isn’t valid.                                                 |
| `403`  | `50013` | Without `MANAGE_ROLES`, a role not below the bot’s, a bot’s role, `@everyone` (deleting), or more permissions. |
| `404`  | `10011` | No such role in this server.                                                                                   |

## Order the roles

```http
PATCH /guilds/{guild.id}/roles
```

A list of `{ id, position }`, and answers `200` with every role. Libraries send the whole list,
each role with its place counted from the bottom (`@everyone` is `0`), and Klang reads it that
way: only the roles whose place really changes are moved, and those must be below the bot’s
highest role and stay below it.

- **Needs**: `MANAGE_ROLES`.
- **Rate limit**: `manage-roles`.

## Create an invite

```http
POST /channels/{channel.id}/invites
```

Answers `200` with the [invite](https://docs.klang.gg/bots/reference/objects/#invite) and its metadata.

- **Needs**: `CREATE_INSTANT_INVITE` in the channel. Not in a thread.
- **Rate limit**: `invite-create`, per server, shared with the people in the app.

| Field       | Type    | Notes                                                              |
| ----------- | ------- | ------------------------------------------------------------------ |
| `max_age`   | integer | Seconds until it expires, up to 604,800. 0 never. A day if absent. |
| `max_uses`  | integer | 0 to 100. 0 has no limit.                                          |
| `unique`    | boolean | Accepted: every invite Klang makes is new.                         |
| `temporary` | boolean | Only `false`: Klang has no temporary membership.                   |

Invites for a stream or an activity (`target_type`) don’t exist in Klang.

```json
{
  "code": "tfkkacb3",
  "type": 0,
  "guild": {
    "id": "1575146937681682432",
    "name": "Aceptación U19",
    "icon": null,
    "splash": null,
    "banner": null,
    "description": null,
    "features": [],
    "verification_level": 0,
    "vanity_url_code": null,
    "nsfw_level": 0,
    "premium_subscription_count": 0
  },
  "channel": { "id": "1575146937681682434", "name": "general", "type": 0 },
  "expires_at": "2026-11-25T15:01:38.143Z",
  "inviter": { "id": "1575146937547464704", "username": "mayordomo_mww6ml2u", "bot": true },
  "uses": 0,
  "max_uses": 10,
  "max_age": 3600,
  "temporary": false,
  "created_at": "2026-11-25T14:01:38.143Z"
}
```

## The invites

```http
GET /channels/{channel.id}/invites
GET /guilds/{guild.id}/invites
```

The live invites, newest first, with their metadata: a channel’s with `MANAGE_CHANNELS` in it, the
server’s with `MANAGE_GUILD`.

- **Rate limit**: `read-invites`.

## An invite by its code

```http
GET /invites/{invite.code}
```

Where a code leads, for anyone who has it, without its metadata. With `with_counts=true`, also
`approximate_member_count` and `approximate_presence_count`. A server’s own invite link works too;
it leads to the server’s welcome channel, or to no channel (`null`). A code that expired, was
used up or isn’t one is `404` with `10006`.

- **Rate limit**: `invite-preview`.

## Revoke an invite

```http
DELETE /invites/{invite.code}
```

Answers `200` with the invite as it was. Needs `MANAGE_GUILD`, or being the bot that created it.

## The audit log

```http
GET /guilds/{guild.id}/audit-logs
```

The server’s audit log, newest first, as Discord’s entries.

- **Needs**: `VIEW_AUDIT_LOG`.
- **Rate limit**: `read-audit-log`.

| Query         | Notes                                        |
| ------------- | -------------------------------------------- |
| `user_id`     | Only what that person or bot did.            |
| `action_type` | Only one kind of entry, by Discord’s number. |
| `before`      | Entries before this entry ID.                |
| `after`       | Entries after this entry ID.                 |
| `limit`       | 1 to 100. 50 if absent.                      |

Klang writes these kinds of entries, with these numbers:

| Number   | What                                                                 |
| -------- | -------------------------------------------------------------------- |
| 1        | The server changed: its name, picture, owner or verification level.  |
| 10-12    | A channel was created, changed or deleted.                           |
| 14, 15   | An overwrite was set (created or changed: both are `14`) or removed. |
| 20       | Someone was kicked, or a bot was removed from the server.            |
| 22, 23   | A ban, and a ban lifted.                                             |
| 24       | A member changed: nickname, timeout, or muted or deafened in voice.  |
| 25       | A member’s roles changed (`$add` and `$remove`, one role per entry). |
| 26, 27   | Moved or disconnected from voice.                                    |
| 28       | A bot was added.                                                     |
| 30-32    | A role was created, changed or deleted.                              |
| 40, 42   | An invite was created or revoked.                                    |
| 50-52    | A webhook was created, changed or deleted.                           |
| 60-62    | An emoji was added, renamed or deleted.                              |
| 90-92    | A sticker was added, changed or deleted.                             |
| 111, 112 | A thread was changed (name, lock) or deleted.                        |
| 140-142  | An AutoMod rule was created, changed or deleted.                     |

What Klang records and Discord has no number for isn’t listed: tickets, role panels, a voice
channel’s status, pausing and resuming joins, the raid alert, a server made from a template, and
messages the AutoMod blocked. `changes` carries what Klang keeps of each change, under Discord’s
keys, with IDs and permissions as bots see them; some entries have none. The reason, if there was
one, is in `reason`. `users` has whoever the entries name.

Each new entry also reaches bots with `GUILD_MODERATION` and **View audit log** as a
[`GUILD_AUDIT_LOG_ENTRY_CREATE`](https://docs.klang.gg/bots/reference/gateway-events/#guild_audit_log_entry_create).

```json
{
  "audit_log_entries": [
    {
      "id": "1575149468473167872",
      "user_id": "1575146937547464704",
      "target_id": null,
      "action_type": 40,
      "changes": [
        { "key": "code", "new_value": "tfkkacb3" },
        { "key": "channel_id", "new_value": "1575146937681682434" },
        { "key": "max_uses", "new_value": 10 },
        { "key": "max_age", "new_value": 3600 },
        { "key": "temporary", "new_value": false }
      ]
    },
    {
      "id": "1575149468305395712",
      "user_id": "1575146937547464704",
      "target_id": "1575149467932102656",
      "action_type": 14,
      "options": { "id": "1575149468162789376", "type": "0", "role_name": "Helpers" },
      "changes": [
        { "key": "allow", "new_value": "66560" },
        { "key": "deny", "new_value": "0" }
      ]
    }
  ],
  "users": [{ "id": "1575146937547464704", "username": "mayordomo_mww6ml2u", "bot": true }],
  "application_commands": [],
  "auto_moderation_rules": [],
  "guild_scheduled_events": [],
  "integrations": [],
  "threads": [],
  "webhooks": []
}
```

An overwrite’s bits in the audit log are what Klang keeps, with the permissions made up from them
(here `READ_MESSAGE_HISTORY`, from **View channel**); the bits Klang doesn’t have stay on the
overwrite, not in the log.
