# Users and the app

> The routes about the bot itself. Where its gateway is, its user, other people it shares a server with, its app and its interactions URL, the servers it’s in, leaving one, and opening a direct message.

## Gateway address

```http
GET /gateway
```

Where bots connect. It’s the only route that works without a token.

- **Rate limit**: the global limit, per IP address.

```json
{
  "url": "wss://bot-gateway.klang.gg"
}
```

## Gateway for a bot

```http
GET /gateway/bot
```

The same address, with how many shards to use and how many sessions the bot can still start
today.

- **Rate limit**: the global limit.

| Field                                 | Type    | Notes                                                            |
| ------------------------------------- | ------- | ---------------------------------------------------------------- |
| `url`                                 | string  | The bot gateway’s address.                                       |
| `shards`                              | integer | Always `1`.                                                      |
| `session_start_limit.total`           | integer | **Identify** frames a bot can send per UTC day: `1000`.          |
| `session_start_limit.remaining`       | integer | How many are left today.                                         |
| `session_start_limit.reset_after`     | integer | Milliseconds until the count starts again, at midnight UTC.      |
| `session_start_limit.max_concurrency` | integer | Always `1`: one **Identify** at a time, every 5 seconds at most. |

```json
{
  "url": "wss://bot-gateway.klang.gg",
  "shards": 1,
  "session_start_limit": {
    "total": 1000,
    "remaining": 999,
    "reset_after": 77684409,
    "max_concurrency": 1
  }
}
```

## The bot’s user

```http
GET /users/@me
```

The bot’s own [user](https://docs.klang.gg/bots/reference/objects/#user), with the fields only the bot reads about
itself: `flags`, `verified` (always `true`), `mfa_enabled` (always `false`), `email` (always
`null`) and `locale` (always `en-US`).

- **Rate limit**: the global limit.

```json
{
  "id": "1571350332965146624",
  "username": "lighthouse",
  "discriminator": "0",
  "global_name": "Lighthouse",
  "avatar": null,
  "banner": null,
  "accent_color": null,
  "public_flags": 0,
  "bot": true,
  "flags": 0,
  "verified": true,
  "mfa_enabled": false,
  "email": null,
  "locale": "en-US"
}
```

## Another user

```http
GET /users/{user.id}
```

A person or a bot, with what their profile shows to everyone. Klang only answers for users the
bot **shares a server or a direct message with**; anyone else is an unknown user, whether they
exist or not. See [Looking up users](https://docs.klang.gg/bots/differences/#looking-up-users).

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

| Status | Code    | When                                                    |
| ------ | ------- | ------------------------------------------------------- |
| `404`  | `10013` | The user doesn’t exist, or shares nothing with the bot. |

```json
{
  "id": "1571350327143452672",
  "username": "leo",
  "discriminator": "0",
  "global_name": "Leo",
  "avatar": null,
  "banner": null,
  "accent_color": null,
  "public_flags": 0
}
```

## The bot’s app

```http
GET /applications/@me
GET /oauth2/applications/@me
```

Both answer the same: the bot’s [application](https://docs.klang.gg/bots/reference/objects/#application), with its
owner and how many servers the bot is in. Libraries read it when they start, for the owner’s ID
and the app’s `flags`.

- **Rate limit**: the global limit.

```json
{
  "id": "1571350332965146624",
  "name": "Lighthouse",
  "icon": null,
  "description": "Posts the tide tables every morning.",
  "summary": "",
  "bot_public": false,
  "bot_require_code_grant": false,
  "bot": {
    "id": "1571350332965146624",
    "username": "lighthouse",
    "discriminator": "0",
    "global_name": "Lighthouse",
    "avatar": null,
    "banner": null,
    "accent_color": null,
    "public_flags": 0,
    "bot": true
  },
  "owner": {
    "id": "1571350324312297472",
    "username": "nora",
    "discriminator": "0",
    "global_name": "Nora",
    "avatar": null,
    "banner": null,
    "accent_color": null,
    "public_flags": 0
  },
  "verify_key": "682781a2f47cfbc91c26970c848641a3a7e2773c0d0d4e9d1153f75c19eb797a",
  "team": null,
  "flags": 557056,
  "flags_new": "557056",
  "redirect_uris": [],
  "interactions_endpoint_url": null,
  "approximate_guild_count": 1
}
```

Here `flags` is `557056`: `GATEWAY_GUILD_MEMBERS_LIMITED` (`1 << 15`) and
`GATEWAY_MESSAGE_CONTENT_LIMITED` (`1 << 19`), the two privileged intents switched on in the
portal. `verify_key` is the public key that checks [HTTP interactions](https://docs.klang.gg/bots/http-interactions/).

## Set the interactions URL

```http
PATCH /applications/@me
```

Sets or removes the app’s [interactions endpoint URL](https://docs.klang.gg/bots/http-interactions/), the same as the
portal’s General tab. A URL is saved only if it passes
[the check](https://docs.klang.gg/bots/http-interactions/#the-check-when-you-save): a signed `PING` has to get `200`
with `{"type": 1}`, and a badly signed one `401`. Answers with the bot’s
[application](https://docs.klang.gg/bots/reference/objects/#application).

It’s the only field Klang changes here: the description, the icon and the rest are edited in the
portal, and sending any of them is an error, so your bot never thinks it changed something that
didn’t.

- **Rate limit**: the global limit and, when there’s a URL, `interactions-url-check`, 10 every
  10 minutes per app (shared with the portal).

| Field                       | Type           | Notes                                                                                  |
| --------------------------- | -------------- | -------------------------------------------------------------------------------------- |
| `interactions_endpoint_url` | string or null | Up to 512 characters. `null` or `""` goes back to `INTERACTION_CREATE` on the gateway. |

| Status | Code    | When                                                                                                                                                   |
| ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `400`  | `50035` | The URL didn’t pass the check: the error is on `interactions_endpoint_url`, with a message that says which part failed. Or the body has another field. |

```json
{
  "interactions_endpoint_url": "https://tides.example.workers.dev/"
}
```

## The bot’s servers

```http
GET /users/@me/guilds
```

The servers the bot is in, as short [server](https://docs.klang.gg/bots/reference/objects/#server) objects, in
ascending order of ID.

- **Rate limit**: `read-guild`, 10 every 10 seconds per bot.

| Query         | Type              | Notes                                                                                 |
| ------------- | ----------------- | ------------------------------------------------------------------------------------- |
| `before`      | ID                | Only servers with a smaller ID: the closest ones to it.                               |
| `after`       | ID                | Only servers with a bigger ID. If both are sent, `after` counts.                      |
| `limit`       | integer, 1 to 200 | 200 by default.                                                                       |
| `with_counts` | `true` or `false` | Adds `approximate_member_count` and `approximate_presence_count` (people online now). |

Each item has `id`, `name`, `icon`, `banner`, `owner` (always `false`: a bot never owns a
server), `permissions` (what the bot can do in the server, from its roles, before any channel’s
overwrites) and `features`.

```json
[
  {
    "id": "1571350333212610560",
    "name": "Harbor",
    "icon": null,
    "banner": null,
    "owner": false,
    "permissions": "563261442739777",
    "features": ["PIN_PERMISSION_MIGRATION_COMPLETE"],
    "approximate_member_count": 3,
    "approximate_presence_count": 1
  }
]
```

## Leave a server

```http
DELETE /users/@me/guilds/{guild.id}
```

The bot leaves the server, as if someone with **Manage server** had removed it: its role goes
with it, and the server’s audit log shows the bot as the one who removed it. Answers `204`.
The bot gets a [`GUILD_DELETE`](https://docs.klang.gg/bots/reference/gateway-events/#guild_delete). To come back,
someone has to add it again.

- **Rate limit**: the global limit.

| Status | Code    | When                 |
| ------ | ------- | -------------------- |
| `404`  | `10004` | The bot isn’t in it. |

## Open a direct message

```http
POST /users/@me/channels
```

Opens the direct message between the bot and a person, or returns the one they already have.
Answers with the [DM channel](https://docs.klang.gg/bots/reference/objects/#direct-message-channel); send messages to
its `id`. Opening one doesn’t send a `CHANNEL_CREATE`.

The person has to share a server with the bot and allow it in their privacy settings. See
[Direct messages](https://docs.klang.gg/bots/differences/#direct-messages).

- **Rate limit**: `dm-create`, only when a new one is opened. Getting one that already exists
  is free.

| Field          | Type | Notes                             |
| -------------- | ---- | --------------------------------- |
| `recipient_id` | ID   | Required. The person to write to. |

| Status | Code    | When                                                                                                                                                              |
| ------ | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `403`  | `50007` | No server in common, their settings don’t allow it, a block, the other user is a bot, or there’s no such user. Always the same code, so the reason stays private. |
| `400`  | `0`     | The bot tried to open one with itself.                                                                                                                            |

```json
{
  "recipient_id": "1571350327143452672"
}
```

```json
{
  "id": "1571350402322157569",
  "recipients": [
    {
      "id": "1571350327143452672",
      "username": "leo",
      "discriminator": "0",
      "global_name": "Leo",
      "avatar": null,
      "banner": null,
      "accent_color": null,
      "public_flags": 0
    }
  ],
  "last_message_id": null,
  "flags": 0,
  "type": 1,
  "name": null
}
```
