# Moderation

> Kicking and banning people, the ban list, timeouts, nicknames, giving and taking roles, and deleting messages in bulk. The permissions each route checks, the reason for the audit log, and the errors.

## Before you read

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

- **The permission** is the one in each route, held by the bot in the server (or in the channel,
  for a bulk delete). A timed-out bot has lost it.
- **The hierarchy.** The bot can only act on people whose highest role is **below** its own
  highest role. Being level isn’t enough. Nobody can act on the server’s owner, and a bot can’t
  act on itself through these routes.
- **The bot’s own role** is placed at the bottom when it’s installed. Until someone in the server
  moves it up, the bot can’t kick, ban, time out or give roles to anyone with a role.
- **Roles.** A bot only gives roles below its own highest role, and **never a role with a
  permission the bot doesn’t have**, even one below its own. Another bot’s role can’t be given or
  taken.

These routes need the bot to be in the server: for any other server they answer `404` with
`10004`. Only people who are **in the server** can be kicked, banned, timed out or changed: for
anyone else they answer `404` with `10007`.

### The reason in the audit log

Send `X-Audit-Log-Reason` with the reason, URL-encoded (libraries do it for you when you pass a
`reason`). Klang keeps up to 512 characters, and stores it in the server’s audit log for kicks,
bans, lifted bans, timeouts and role changes. A ban’s reason is also the one its ban shows.
Deleting messages isn’t written to the audit log, so a bulk delete ignores it.

```http
DELETE /api/v10/guilds/1571460870625443840/members/1571460871472693248
Authorization: Bot <token>
X-Audit-Log-Reason: Left%20the%20rules
```

## Kick someone

```http
DELETE /guilds/{guild.id}/members/{user.id}
```

Takes the person out of the server and answers `204`. They can come back with an invite. Kicking
a bot removes it from the server, with its role.

- **Needs**: `KICK_MEMBERS`, and to be above them.

| Status | Code    | When                                                                 |
| ------ | ------- | -------------------------------------------------------------------- |
| `403`  | `50013` | Without the permission, or they’re the owner, the bot, or not below. |
| `404`  | `10007` | They aren’t in the server.                                           |
| `404`  | `10004` | The bot isn’t in it, or there’s no such server.                      |

## Change a member

```http
PATCH /guilds/{guild.id}/members/{user.id}
```

Changes what’s sent and leaves the rest. Answers `200` with the
[member](https://docs.klang.gg/bots/reference/objects/#member) as it ended up, and everyone with the intent gets a
[`GUILD_MEMBER_UPDATE`](https://docs.klang.gg/bots/reference/gateway-events/#guild_member_update).

| Field                          | Type               | Needs              | Notes                                                                                                                                   |
| ------------------------------ | ------------------ | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
| `nick`                         | string or `null`   | `MANAGE_NICKNAMES` | Up to 32 characters, one line. `null` or `""` removes it. The server’s AutoMod word rules apply.                                        |
| `roles`                        | array of role IDs  | `MANAGE_ROLES`     | The whole list. Each role added or removed is checked as if given or taken alone. The server’s ID (`@everyone`) is ignored.             |
| `communication_disabled_until` | ISO 8601 or `null` | `MODERATE_MEMBERS` | Times them out until then, **28 days at most**. `null`, or a moment already past, lifts the timeout. Nobody times out an administrator. |
| `flags`                        | integer            | —                  | Accepted and ignored.                                                                                                                   |
| `mute`, `deaf`, `channel_id`   | —                  | —                  | Not available yet: sending `mute` or `deaf` as `true`, or `channel_id` at all, answers `50035`.                                         |

The changes apply in that order: nickname, roles, timeout. The timeout’s permission is checked
before anything changes.

- **Rate limit**: a nickname counts against `nickname`, a timeout against `timeout`.

| Status | Code    | When                                                                      |
| ------ | ------- | ------------------------------------------------------------------------- |
| `400`  | `50035` | A field isn’t valid, a timeout longer than 28 days, or voice fields.      |
| `400`  | `50035` | A role in `roles` is another bot’s role.                                  |
| `403`  | `50013` | A permission missing, someone not below the bot, or a role it can’t give. |
| `404`  | `10011` | A role in `roles` isn’t one of the server’s.                              |
| `404`  | `10007` | They aren’t in the server.                                                |

`PATCH /guilds/1571460870625443840/members/1571460871472693248` with
`X-Audit-Log-Reason: Cooling%20off`:

```json
{ "communication_disabled_until": "2026-11-15T09:54:32.426Z" }
```

```json
{
  "nick": null,
  "avatar": null,
  "banner": null,
  "roles": [],
  "joined_at": "2026-11-15T09:44:28.750Z",
  "premium_since": null,
  "deaf": false,
  "mute": false,
  "flags": 0,
  "pending": false,
  "communication_disabled_until": "2026-11-15T09:54:32.426Z",
  "user": {
    "id": "1571460871472693248",
    "username": "leomwhneaqd8s0",
    "discriminator": "0",
    "global_name": "Leo",
    "avatar": null,
    "banner": null,
    "accent_color": null,
    "public_flags": 0
  }
}
```

A timeout longer than 28 days:

```json
{
  "code": 50035,
  "message": "The request body isn’t valid.",
  "errors": {
    "communication_disabled_until": {
      "_errors": [{ "code": "INVALID", "message": "A timeout lasts 28 days at most." }]
    }
  }
}
```

## Change the bot’s nickname

```http
PATCH /guilds/{guild.id}/members/@me
```

The same as above for the bot itself, with `nick` only. Any other field answers `50035`.

- **Needs**: `CHANGE_NICKNAME`.
- **Rate limit**: `nickname`.

## Give or take a role

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

Gives the role, or takes it away, and answers `204`. Giving a role they already have, or taking
one they don’t, also answers `204`.

- **Needs**: `MANAGE_ROLES`, and the role below the bot’s highest role. To give it, the bot also
  needs every permission the role carries.

| Status | Code    | When                                                                              |
| ------ | ------- | --------------------------------------------------------------------------------- |
| `400`  | `50035` | `@everyone`, or another bot’s role: nobody else can have it.                      |
| `403`  | `50013` | Without `MANAGE_ROLES`, a role not below the bot’s, or one with more permissions. |
| `404`  | `10011` | The role isn’t one of the server’s.                                               |
| `404`  | `10007` | They aren’t in the server.                                                        |

## Ban someone

```http
PUT /guilds/{guild.id}/bans/{user.id}
```

Takes the person out of the server and keeps them out, and answers `204`. Banning a bot removes
it from the server, with its role. Bans from the API last until someone lifts them.

- **Needs**: `BAN_MEMBERS`, and to be above them.

| Field                    | Type                 | Notes                                      |
| ------------------------ | -------------------- | ------------------------------------------ |
| `delete_message_seconds` | integer, 0 to 604800 | Accepted, but **no messages are deleted**. |
| `delete_message_days`    | integer, 0 to 7      | The older field. The same.                 |

The body can be empty. The reason goes in `X-Audit-Log-Reason`.

| Status | Code    | When                                                                 |
| ------ | ------- | -------------------------------------------------------------------- |
| `400`  | `50035` | One of the fields is out of range.                                   |
| `403`  | `50013` | Without the permission, or they’re the owner, the bot, or not below. |
| `404`  | `10007` | They aren’t in the server: only members can be banned.               |

## Lift a ban

```http
DELETE /guilds/{guild.id}/bans/{user.id}
```

Lifts the ban and answers `204`. The person can join again with an invite.

- **Needs**: `BAN_MEMBERS`.

| Status | Code    | When                    |
| ------ | ------- | ----------------------- |
| `403`  | `50013` | Without the permission. |
| `404`  | `10026` | That user isn’t banned. |

```json
{ "code": 10026, "message": "Unknown Ban" }
```

## The bans

```http
GET /guilds/{guild.id}/bans
```

A page of [bans](https://docs.klang.gg/bots/reference/moderation/#one-ban), in ascending order of user ID. Bans that people in the app set to end
don’t appear once they’ve ended.

- **Needs**: `BAN_MEMBERS`.
- **Rate limit**: `read-bans`, 10 every 10 seconds per bot, whichever server.

| Query    | Type               | Notes                                                             |
| -------- | ------------------ | ----------------------------------------------------------------- |
| `limit`  | integer, 1 to 1000 | 1000 by default.                                                  |
| `after`  | ID                 | A user ID: only bans of users with a bigger one. To page forward. |
| `before` | ID                 | A user ID: the bans right below it, still in ascending order.     |

`GET /guilds/1571460870625443840/bans?limit=2`:

```json
[
  {
    "reason": "Spam links",
    "user": {
      "id": "1571460874375151616",
      "username": "ivymwhneb9d2cq",
      "discriminator": "0",
      "global_name": "Ivy",
      "avatar": null,
      "banner": null,
      "accent_color": null,
      "public_flags": 0
    }
  },
  {
    "reason": null,
    "user": {
      "id": "1571460877181140992",
      "username": "maxmwhnebryimm",
      "discriminator": "0",
      "global_name": "Max",
      "avatar": null,
      "banner": null,
      "accent_color": null,
      "public_flags": 0
    }
  }
]
```

## One ban

```http
GET /guilds/{guild.id}/bans/{user.id}
```

One user’s ban: `reason` (or `null`) and the `user`.

- **Needs**: `BAN_MEMBERS`.
- **Rate limit**: `read-users`, 20 every 5 seconds per bot.

| Status | Code    | When                    |
| ------ | ------- | ----------------------- |
| `403`  | `50013` | Without the permission. |
| `404`  | `10026` | That user isn’t banned. |

## Delete messages in bulk

```http
POST /channels/{channel.id}/messages/bulk-delete
```

Deletes between 2 and 100 messages of the channel at once and answers `204`. Everyone in the
channel gets one
[`MESSAGE_DELETE_BULK`](https://docs.klang.gg/bots/reference/gateway-events/#message_delete_bulk) with the IDs that were
deleted. IDs that aren’t a message of this channel, or that were already deleted, are skipped
without an error. Each message leaves the same trace in the app as deleting it alone.

```json
{
  "messages": ["1571460891315945472", "1571460891357888512", "1571460891416608768"]
}
```

- **Needs**: `MANAGE_MESSAGES` in the channel, also for the bot’s own messages. So it never works
  in a direct message.
- **Age**: every message must be **less than 14 days old**. The age is read from the ID the way
  your library reads it, so its own check (`bulkDelete(…, true)` in discord.js, for example)
  agrees with Klang’s.
- **Rate limit**: `bulk-delete`, 5 every 10 seconds per bot.

| Status | Code    | When                                                     |
| ------ | ------- | -------------------------------------------------------- |
| `400`  | `50035` | Fewer than 2 IDs or more than 100.                       |
| `400`  | `50034` | One of them is 14 days old or older. Nothing is deleted. |
| `403`  | `50013` | Without `MANAGE_MESSAGES` in the channel.                |
| `404`  | `10003` | No such channel, or the bot can’t see it.                |

```json
{ "code": 50034, "message": "Only messages from the last 14 days can be deleted in bulk." }
```
