# Discord API

Every handle of the Discord fake: the `discord` fixture, the people and objects a test creates on it, and what they read and wait for. How to test a Discord app or agent, step by step, is in the [Discord guide](https://channelstack.dev/docs/platforms/discord).

Each call is shown in every SDK's language, with its required options (the table under it lists them all). Arguments go first; options are TypeScript's trailing object, Python's keyword arguments and Rust's builder setters. The variable names are the ones the calls return elsewhere on these pages, so `user` is a `DiscordUser` handle.

Besides the errors listed with each method, most methods can fail with [`action_requires`](https://channelstack.dev/docs/reference/api/errors#action-requires), [`cross_workspace_credential`](https://channelstack.dev/docs/reference/api/errors#cross-workspace-credential), [`fake_actor_in_live_world`](https://channelstack.dev/docs/reference/api/errors#fake-actor-in-live-world), [`internal`](https://channelstack.dev/docs/reference/api/errors#internal), [`invalid_argument`](https://channelstack.dev/docs/reference/api/errors#invalid-argument), [`live_misconfigured`](https://channelstack.dev/docs/reference/api/errors#live-misconfigured), [`live_not_allowlisted`](https://channelstack.dev/docs/reference/api/errors#live-not-allowlisted), [`not_found`](https://channelstack.dev/docs/reference/api/errors#not-found), [`session_not_found`](https://channelstack.dev/docs/reference/api/errors#session-not-found), [`unauthorized`](https://channelstack.dev/docs/reference/api/errors#unauthorized), [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): a bad request, a closed session, or live mode refusing or misconfigured.

## DiscordFixture

Returned by [`Session.discord()`](https://channelstack.dev/docs/reference/api/client#discord).

| Field                | Python / Rust         | Type           |
| -------------------- | --------------------- | -------------- |
| `app`                | `app`                 | `DiscordApp`   |
| `apiUrl`             | `api_url`             | `string`       |
| `guild`              | `guild`               | `DiscordGuild` |
| `gatewayConnections` | `gateway_connections` | `number`       |

### createUser()

Create a person in the guild.

```ts title="TypeScript"
const user = await discord.createUser({ name: "…" });
```

```python title="Python"
user = await discord.create_user(name="…")
```

```rust title="Rust"
let user = discord.create_user().name("…").await?;
```

| Parameter | Type      | Required |                                                                          |
| --------- | --------- | -------- | ------------------------------------------------------------------------ |
| `name`    | `string`  | yes      | option                                                                   |
| `id`      | `string`  | no       | option                                                                   |
| `admin`   | `boolean` | no       | option                                                                   |
| `bot`     | `boolean` | no       | option                                                                   |
| `token`   | `string`  | no       | option; read from `CHANNELSTACK_DISCORD_USER_{name}_TOKEN` when left out |

**Returns** [`DiscordUser`](https://channelstack.dev/docs/reference/api/discord#discorduser).

### users()

The people this session created.

```ts title="TypeScript"
const users = await discord.users();
```

```python title="Python"
users = await discord.users()
```

```rust title="Rust"
let users = discord.users().await?;
```

**Returns** [`DiscordUsers`](https://channelstack.dev/docs/reference/api/discord#discordusers).

### createApp()

Make another app in the guild, with its own application, bot user, token and Ed25519 key (decision 0027), or return the one made with this name and settings. Its bot connects to the Gateway with its own token; its interactions go to CHANNELSTACK_DISCORD_APP\_\<NAME>\_WEBHOOK_URL, or over its Gateway connection.

```ts title="TypeScript"
const app = await discord.createApp({ name: "…" });
```

```python title="Python"
app = await discord.create_app(name="…")
```

```rust title="Rust"
let app = discord.create_app().name("…").await?;
```

| Parameter           | Python / Rust        | Type                                                               | Required |        |
| ------------------- | -------------------- | ------------------------------------------------------------------ | -------- | ------ |
| `name`              | `name`               | `string`                                                           | yes      | option |
| `privilegedIntents` | `privileged_intents` | `Array<"GUILD_MEMBERS" \| "GUILD_PRESENCES" \| "MESSAGE_CONTENT">` | no       | option |

**Returns** [`DiscordApp`](https://channelstack.dev/docs/reference/api/discord#discordapp).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

### apps()

The apps this session used: the built-in one first, then each it got from `createApp`, in the order it first did. Another session's are `not_found` here.

```ts title="TypeScript"
const apps = await discord.apps();
```

```python title="Python"
apps = await discord.apps()
```

```rust title="Rust"
let apps = discord.apps().await?;
```

**Returns** [`DiscordApps`](https://channelstack.dev/docs/reference/api/discord#discordapps).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

### createChannel()

Create a text channel. Only the listed participants see it (a private channel); include the app for the bot to hear it.

```ts title="TypeScript"
const channel = await discord.createChannel({ name: "…" });
```

```python title="Python"
channel = await discord.create_channel(name="…")
```

```rust title="Rust"
let channel = discord.create_channel().name("…").await?;
```

| Parameter      | Python / Rust  | Type                               | Required |        |
| -------------- | -------------- | ---------------------------------- | -------- | ------ |
| `name`         | `name`         | `string`                           | no       | option |
| `participants` | `participants` | `Array<DiscordUser \| DiscordApp>` | no       | option |
| `readOnly`     | `read_only`    | `boolean`                          | no       | option |
| `topic`        | `topic`        | `string`                           | no       | option |

**Returns** [`DiscordChannel`](https://channelstack.dev/docs/reference/api/discord#discordchannel).

### channels()

The session's channels, threads and DMs.

```ts title="TypeScript"
const channels = await discord.channels();
```

```python title="Python"
channels = await discord.channels()
```

```rust title="Rust"
let channels = discord.channels().await?;
```

**Returns** [`DiscordChannels`](https://channelstack.dev/docs/reference/api/discord#discordchannels).

### verifyEndpoint()

Discord's check of the app's interactions endpoint, as when it is saved in the developer portal: a PING it must answer with a PONG, and one with a bad signature it must refuse with 401.

```ts title="TypeScript"
const endpointCheck = await discord.verifyEndpoint();
```

```python title="Python"
endpoint_check = await discord.verify_endpoint()
```

```rust title="Rust"
let endpoint_check = discord.verify_endpoint().await?;
```

**Returns** [`DiscordEndpointCheck`](https://channelstack.dev/docs/reference/api/discord#discordendpointcheck).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Discord checks an interactions endpoint when it is saved in the developer portal, which no API does.

### deliveries()

Every Gateway dispatch and interaction channelstack sent the bot for this session, with every attempt.

```ts title="TypeScript"
const deliveries = await discord.deliveries({ type: "…" });
```

```python title="Python"
deliveries = await discord.deliveries(type="…")
```

```rust title="Rust"
let deliveries = discord.deliveries().r#type("…").await?;
```

| Parameter  | Python / Rust     | Type             | Required |        |
| ---------- | ----------------- | ---------------- | -------- | ------ |
| `type`     | `type` · `r#type` | `string`         | no       | option |
| `resource` | `resource`        | `DiscordChannel` | no       | option |
| `app`      | `app`             | `DiscordApp`     | no       | option |

**Returns** [`DiscordDeliveries`](https://channelstack.dev/docs/reference/api/discord#discorddeliveries).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### calls()

The REST calls the bot made about this session's channels, messages and interactions, by route (`POST /channels/{channel.id}/messages`). Calls about nothing a session owns (`GET /gateway/bot`) are in no session.

```ts title="TypeScript"
const apiCalls = await discord.calls({ operation: "…" });
```

```python title="Python"
api_calls = await discord.calls(operation="…")
```

```rust title="Rust"
let api_calls = discord.calls().operation("…").await?;
```

| Parameter   | Type             | Required |        |
| ----------- | ---------------- | -------- | ------ |
| `operation` | `string`         | no       | option |
| `error`     | `true \| string` | no       | option |

**Returns** [`DiscordApiCalls`](https://channelstack.dev/docs/reference/api/discord#discordapicalls).

**Live:** needs proxy mode: Discord shows an ephemeral message to its person alone, and no bot API reads it back; proxy mode sees the agent's own calls. Proxy mode doesn't read the app's calls back live yet ([#24](https://github.com/christian-bromann/channelstack/issues/24)), so for now the call is refused with [`action_requires`](https://channelstack.dev/docs/reference/api/errors#action-requires) and the test is skipped.

### fault()

Inject a fault: rate-limit the bot's calls of a route (removed when this session closes), or drop its Gateway connection just before the next dispatch.

```ts title="TypeScript"
const fault = await discord.fault({ type });
```

```python title="Python"
fault = await discord.fault(type=type)
```

```rust title="Rust"
let fault = discord.fault().r#type(r#type).await?;
```

| Parameter    | Python / Rust     | Type                             | Required |        |
| ------------ | ----------------- | -------------------------------- | -------- | ------ |
| `type`       | `type` · `r#type` | `"rate_limit" \| "drop_gateway"` | yes      | option |
| `route`      | `route`           | `string`                         | no       | option |
| `retryAfter` | `retry_after`     | `Duration`                       | no       | option |
| `times`      | `times`           | `number`                         | no       | option |
| `app`        | `app`             | `DiscordApp`                     | no       | option |

**Returns** [`DiscordFault`](https://channelstack.dev/docs/reference/api/discord#discordfault).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): a fault changes what the bot's calls and its Gateway connection get, and live nothing sits between the bot and Discord: its Gateway doesn't go through the proxy.

### waitForCall()

Waits for the next API call from the agent after the last one this handle returned: it polls `calls()` and never sleeps longer than it must.

```ts title="TypeScript"
const apiCall = await discord.waitForCall({ operation: "…" });
```

```python title="Python"
api_call = await discord.wait_for_call(operation="…")
```

```rust title="Rust"
let api_call = discord.wait_for_call().operation("…").await?;
```

| Parameter   | Type             | Required |             |
| ----------- | ---------------- | -------- | ----------- |
| `operation` | `string`         | no       | filter      |
| `error`     | `true \| string` | no       | filter      |
| `timeout`   | `Duration`       | no       | default 10s |

**Returns** [`DiscordApiCall`](https://channelstack.dev/docs/reference/api/discord#discordapicall). When the time is up, it fails with what it was waiting for.

## DiscordApp

Returned by [`DiscordFixture.createApp()`](https://channelstack.dev/docs/reference/api/discord#createapp).

| Field       | Python / Rust | Type        |
| ----------- | ------------- | ----------- |
| `id`        | `id`          | `string`    |
| `name`      | `name`        | `string`    |
| `kind`      | `kind`        | `"app"`     |
| `channel`   | `channel`     | `"discord"` |
| `botUserId` | `bot_user_id` | `string`    |
| `botToken`  | `bot_token`   | `string`    |
| `publicKey` | `public_key`  | `string`    |
| `guildId`   | `guild_id`    | `string`    |

### say()

An app a test made writes, as its own `POST /channels/{id}/messages` would: in a channel or thread its bot sees, or in a DM with the person in `to`. No bot writes to another bot in a DM (50007).

```ts title="TypeScript"
const message = await app.say("…", { channel });
```

```python title="Python"
message = await app.say("…", channel=channel)
```

```rust title="Rust"
let message = app.say("…").channel(&channel).await?;
```

| Parameter | Type                                                            | Required |            |
| --------- | --------------------------------------------------------------- | -------- | ---------- |
| `text`    | `string \| DiscordMessageInput`                                 | yes      | positional |
| `channel` | `DiscordChannel`                                                | no       | option     |
| `to`      | `DiscordUser \| DiscordApp \| Array<DiscordUser \| DiscordApp>` | no       | option     |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`empty_message`](https://channelstack.dev/docs/reference/api/errors#empty-message), [`cannot_message_user`](https://channelstack.dev/docs/reference/api/errors#cannot-message-user), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions), [`invalid_form_body`](https://channelstack.dev/docs/reference/api/errors#invalid-form-body).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

### reply()

An app a test made replies to a message: Discord's reply, in the message's channel, which quotes it.

```ts title="TypeScript"
const result = await app.reply("…", { message });
```

```python title="Python"
result = await app.reply("…", message=message)
```

```rust title="Rust"
let result = app.reply("…").message(&message).await?;
```

| Parameter | Type                            | Required |            |
| --------- | ------------------------------- | -------- | ---------- |
| `text`    | `string \| DiscordMessageInput` | yes      | positional |
| `message` | `DiscordMessage`                | yes      | option     |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`empty_message`](https://channelstack.dev/docs/reference/api/errors#empty-message), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions), [`invalid_form_body`](https://channelstack.dev/docs/reference/api/errors#invalid-form-body).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

### react()

An app a test made reacts to a message.

```ts title="TypeScript"
const delivery = await app.react("…", { message });
```

```python title="Python"
delivery = await app.react("…", message=message)
```

```rust title="Rust"
let delivery = app.react("…").message(&message).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `emoji`   | `string`         | yes      | positional |
| `message` | `DiscordMessage` | yes      | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

### unreact()

An app a test made takes its reaction back.

```ts title="TypeScript"
const delivery = await app.unreact("…", { message });
```

```python title="Python"
delivery = await app.unreact("…", message=message)
```

```rust title="Rust"
let delivery = app.unreact("…").message(&message).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `emoji`   | `string`         | yes      | positional |
| `message` | `DiscordMessage` | yes      | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): not yet: live, the guild holds only the app channelstack has credentials for.

## DiscordUser

Returned by [`DiscordFixture.createUser()`](https://channelstack.dev/docs/reference/api/discord#createuser).

| Field      | Python / Rust | Type        |
| ---------- | ------------- | ----------- |
| `id`       | `id`          | `string`    |
| `name`     | `name`        | `string`    |
| `kind`     | `kind`        | `"user"`    |
| `channel`  | `channel`     | `"discord"` |
| `username` | `username`    | `string`    |
| `isAdmin`  | `is_admin`    | `boolean`   |
| `isBot`    | `is_bot`      | `boolean`   |

### say()

Write a message in a channel or thread, or in a DM with the person or app in `to`.

```ts title="TypeScript"
const message = await user.say("…", { channel });
```

```python title="Python"
message = await user.say("…", channel=channel)
```

```rust title="Rust"
let message = user.say("…").channel(&channel).await?;
```

| Parameter | Type                                                            | Required |            |
| --------- | --------------------------------------------------------------- | -------- | ---------- |
| `text`    | `string \| DiscordMessageInput`                                 | yes      | positional |
| `channel` | `DiscordChannel`                                                | no       | option     |
| `to`      | `DiscordUser \| DiscordApp \| Array<DiscordUser \| DiscordApp>` | no       | option     |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`empty_message`](https://channelstack.dev/docs/reference/api/errors#empty-message), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions), [`invalid_form_body`](https://channelstack.dev/docs/reference/api/errors#invalid-form-body).

### reply()

Reply to a message: Discord's reply, in the message's channel, which quotes it.

```ts title="TypeScript"
const result = await user.reply("…", { message });
```

```python title="Python"
result = await user.reply("…", message=message)
```

```rust title="Rust"
let result = user.reply("…").message(&message).await?;
```

| Parameter | Type                            | Required |            |
| --------- | ------------------------------- | -------- | ---------- |
| `text`    | `string \| DiscordMessageInput` | yes      | positional |
| `message` | `DiscordMessage`                | yes      | option     |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

**Errors** [`missing_access`](https://channelstack.dev/docs/reference/api/errors#missing-access), [`empty_message`](https://channelstack.dev/docs/reference/api/errors#empty-message), [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions), [`invalid_form_body`](https://channelstack.dev/docs/reference/api/errors#invalid-form-body).

### edit()

Edit one of this person's own messages.

```ts title="TypeScript"
const result = await user.edit("…", { message });
```

```python title="Python"
result = await user.edit("…", message=message)
```

```rust title="Rust"
let result = user.edit("…").message(&message).await?;
```

| Parameter | Type                            | Required |            |
| --------- | ------------------------------- | -------- | ---------- |
| `text`    | `string \| DiscordMessageInput` | yes      | positional |
| `message` | `DiscordMessage`                | yes      | option     |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

**Errors** [`cannot_edit_others_message`](https://channelstack.dev/docs/reference/api/errors#cannot-edit-others-message), [`empty_message`](https://channelstack.dev/docs/reference/api/errors#empty-message), [`invalid_form_body`](https://channelstack.dev/docs/reference/api/errors#invalid-form-body).

### delete()

Delete a message: this person's own, or anyone's for an admin.

```ts title="TypeScript"
await user.delete({ message });
```

```python title="Python"
await user.delete(message=message)
```

```rust title="Rust"
user.delete().message(&message).await?;
```

| Parameter | Type             | Required |        |
| --------- | ---------------- | -------- | ------ |
| `message` | `DiscordMessage` | yes      | option |

**Returns** nothing.

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

### react()

React to a message with an emoji.

```ts title="TypeScript"
const delivery = await user.react("…", { message });
```

```python title="Python"
delivery = await user.react("…", message=message)
```

```rust title="Rust"
let delivery = user.react("…").message(&message).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `emoji`   | `string`         | yes      | positional |
| `message` | `DiscordMessage` | yes      | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

### unreact()

Take this person's reaction off a message.

```ts title="TypeScript"
const delivery = await user.unreact("…", { message });
```

```python title="Python"
delivery = await user.unreact("…", message=message)
```

```rust title="Rust"
let delivery = user.unreact("…").message(&message).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `emoji`   | `string`         | yes      | positional |
| `message` | `DiscordMessage` | yes      | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

### startThread()

Start a thread: from a message, or in a channel.

```ts title="TypeScript"
const channel = await user.startThread("…", { message });
```

```python title="Python"
channel = await user.start_thread("…", message=message)
```

```rust title="Rust"
let channel = user.start_thread("…").message(&message).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `name`    | `string`         | yes      | positional |
| `message` | `DiscordMessage` | no       | option     |
| `channel` | `DiscordChannel` | no       | option     |

**Returns** [`DiscordChannel`](https://channelstack.dev/docs/reference/api/discord#discordchannel).

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions), [`thread_already_created`](https://channelstack.dev/docs/reference/api/errors#thread-already-created).

### openDM()

Open (or reuse) a DM with the app or another person.

```ts title="TypeScript"
const channel = await user.openDM(participant);
```

```python title="Python"
channel = await user.open_dm(participant)
```

```rust title="Rust"
let channel = user.open_dm(&participant).await?;
```

| Parameter     | Type                        | Required |            |
| ------------- | --------------------------- | -------- | ---------- |
| `participant` | `DiscordUser \| DiscordApp` | yes      | positional |

**Returns** [`DiscordChannel`](https://channelstack.dev/docs/reference/api/discord#discordchannel).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): live, a person is a bot of their own, and Discord lets no bot write to another bot in a DM.

### invite()

Let people or the app see a channel. The person must be an admin.

```ts title="TypeScript"
await user.invite(participants, { channel });
```

```python title="Python"
await user.invite(participants, channel=channel)
```

```rust title="Rust"
user.invite(participants).channel(&channel).await?;
```

| Parameter      | Type                                                            | Required |            |
| -------------- | --------------------------------------------------------------- | -------- | ---------- |
| `participants` | `DiscordUser \| DiscordApp \| Array<DiscordUser \| DiscordApp>` | yes      | positional |
| `channel`      | `DiscordChannel`                                                | yes      | option     |

**Returns** nothing.

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

### runCommand()

Run one of an app's slash commands in a channel, e.g. `/deploy web staging` or `/deploy service:web`: the built-in app's, or `app`'s, as a person picks one app's command when several have it.

```ts title="TypeScript"
const delivery = await user.runCommand("…", { channel });
```

```python title="Python"
delivery = await user.run_command("…", channel=channel)
```

```rust title="Rust"
let delivery = user.run_command("…").channel(&channel).await?;
```

| Parameter | Type             | Required |            |
| --------- | ---------------- | -------- | ---------- |
| `command` | `string`         | yes      | positional |
| `channel` | `DiscordChannel` | yes      | option     |
| `app`     | `DiscordApp`     | no       | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery). While refused with [`not_found`](https://channelstack.dev/docs/reference/api/errors#not-found), it tries again until the wait's timeout.

**Errors** [`missing_permissions`](https://channelstack.dev/docs/reference/api/errors#missing-permissions).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): live, a person is a bot of their own, and Discord lets no bot run another app's command or press its button.

### click()

Press a button, found by its label or custom_id: on a message, or on the app's newest message in a channel or thread that has it.

```ts title="TypeScript"
const delivery = await user.click("…", { message });
```

```python title="Python"
delivery = await user.click("…", message=message)
```

```rust title="Rust"
let delivery = user.click("…").message(&message).await?;
```

| Parameter | Type                         | Required |            |
| --------- | ---------------------------- | -------- | ---------- |
| `button`  | `string \| DiscordButtonRef` | yes      | positional |
| `message` | `DiscordMessage`             | no       | option     |
| `channel` | `DiscordChannel`             | no       | option     |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery). While refused with [`not_found`](https://channelstack.dev/docs/reference/api/errors#not-found), it tries again until the wait's timeout.

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): live, a person is a bot of their own, and Discord lets no bot run another app's command or press its button.

### ephemeral()

Messages only this person sees: the app's ephemeral responses, and "This interaction failed".

```ts title="TypeScript"
const ephemeralMessages = await user.ephemeral();
```

```python title="Python"
ephemeral_messages = await user.ephemeral()
```

```rust title="Rust"
let ephemeral_messages = user.ephemeral().await?;
```

**Returns** [`DiscordEphemeralMessages`](https://channelstack.dev/docs/reference/api/discord#discordephemeralmessages). It reads once the app is quiet.

**Live:** needs proxy mode: Discord shows an ephemeral message to its person alone, and no bot API reads it back; proxy mode sees the agent's own calls. Proxy mode doesn't read the app's calls back live yet ([#24](https://github.com/christian-bromann/channelstack/issues/24)), so for now the call is refused with [`action_requires`](https://channelstack.dev/docs/reference/api/errors#action-requires) and the test is skipped.

## DiscordMessage

Returned by [`DiscordApp.say()`](https://channelstack.dev/docs/reference/api/discord#say), [`DiscordApp.reply()`](https://channelstack.dev/docs/reference/api/discord#reply), [`DiscordUser.say()`](https://channelstack.dev/docs/reference/api/discord#say-1), [`DiscordUser.reply()`](https://channelstack.dev/docs/reference/api/discord#reply-1).

| Field        | Python / Rust | Type                           |
| ------------ | ------------- | ------------------------------ |
| `id`         | `id`          | `string`                       |
| `channelId`  | `channel_id`  | `string`                       |
| `channel`    | `channel`     | `DiscordChannel`               |
| `author`     | `author`      | `DiscordUser \| DiscordApp`    |
| `text`       | `text`        | `string`                       |
| `content`    | `content`     | `string`                       |
| `embeds`     | `embeds`      | `unknown[]`                    |
| `components` | `components`  | `unknown[]`                    |
| `postedAt`   | `posted_at`   | `string`                       |
| `edited`     | `edited`      | `boolean`                      |
| `replyTo`    | `reply_to`    | `string \| undefined`          |
| `thread`     | `thread`      | `DiscordChannel \| undefined`  |
| `visibleTo`  | `visible_to`  | `DiscordUser \| undefined`     |
| `delivery`   | `delivery`    | `DiscordDelivery \| undefined` |

### refresh()

Re-read a message: its latest text, edits and thread.

```ts title="TypeScript"
const updated = await message.refresh();
```

```python title="Python"
updated = await message.refresh()
```

```rust title="Rust"
let updated = message.refresh().await?;
```

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage).

### replies()

What answers a message, oldest first: Discord's replies to it, and the messages of the thread started from it.

```ts title="TypeScript"
const replies = await message.replies();
```

```python title="Python"
replies = await message.replies()
```

```rust title="Rust"
let replies = message.replies().await?;
```

**Returns** [`DiscordReplies`](https://channelstack.dev/docs/reference/api/discord#discordreplies).

### transcript()

A message and what answers it, as a person reads them.

```ts title="TypeScript"
const transcript = await message.transcript({ as });
```

```python title="Python"
transcript = await message.transcript(as_=as_)
```

```rust title="Rust"
let transcript = message.transcript().r#as(&r#as).await?;
```

| Parameter | Python / Rust  | Type                        | Required |        |
| --------- | -------------- | --------------------------- | -------- | ------ |
| `as`      | `as_` · `r#as` | `DiscordUser \| DiscordApp` | no       | option |

**Returns** [`DiscordTranscript`](https://channelstack.dev/docs/reference/api/discord#discordtranscript).

### reactions()

The reactions on a message, and who added each.

```ts title="TypeScript"
const reactions = await message.reactions();
```

```python title="Python"
reactions = await message.reactions()
```

```rust title="Rust"
let reactions = message.reactions().await?;
```

**Returns** [`DiscordReactions`](https://channelstack.dev/docs/reference/api/discord#discordreactions).

### deliveryFor()

The first Gateway dispatch this message caused to this app's bot (decision 0027), or nothing when it wasn't told.

```ts title="TypeScript"
const delivery = await message.deliveryFor(app);
```

```python title="Python"
delivery = await message.delivery_for(app)
```

```rust title="Rust"
let delivery = message.delivery_for(&app).await?;
```

| Parameter | Type         | Required |            |
| --------- | ------------ | -------- | ---------- |
| `app`     | `DiscordApp` | yes      | positional |

**Returns** [`DiscordDelivery | undefined`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### deliveries()

Every Gateway dispatch this message caused to the apps' bots.

```ts title="TypeScript"
const messageDeliveries = await message.deliveries();
```

```python title="Python"
message_deliveries = await message.deliveries()
```

```rust title="Rust"
let message_deliveries = message.deliveries().await?;
```

**Returns** [`DiscordMessageDeliveries`](https://channelstack.dev/docs/reference/api/discord#discordmessagedeliveries).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### waitForReply()

Waits for the next reply after the last one this handle returned: it polls `replies()` and never sleeps longer than it must.

```ts title="TypeScript"
const updated = await message.waitForReply({ from: "my-agent" });
```

```python title="Python"
updated = await message.wait_for_reply(from_="my-agent")
```

```rust title="Rust"
let updated = message.wait_for_reply().from("my-agent").await?;
```

| Parameter | Python / Rust    | Type                                  | Required |                     |
| --------- | ---------------- | ------------------------------------- | -------- | ------------------- |
| `from`    | `from_` · `from` | `DiscordUser \| DiscordApp \| string` | no       | a handle, or a name |
| `timeout` | `timeout`        | `Duration`                            | no       | default 10s         |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage). When the time is up, it fails with what it was waiting for.

### latestReply()

The latest reply, once the app is quiet. Waits for at least one: it polls `replies()` and never sleeps longer than it must.

```ts title="TypeScript"
const updated = await message.latestReply({ from: "my-agent" });
```

```python title="Python"
updated = await message.latest_reply(from_="my-agent")
```

```rust title="Rust"
let updated = message.latest_reply().from("my-agent").await?;
```

| Parameter  | Python / Rust    | Type                                  | Required |                     |
| ---------- | ---------------- | ------------------------------------- | -------- | ------------------- |
| `from`     | `from_` · `from` | `DiscordUser \| DiscordApp \| string` | no       | a handle, or a name |
| `timeout`  | `timeout`        | `Duration`                            | no       | default 10s         |
| `quietFor` | `quiet_for`      | `Duration`                            | no       | default 750ms       |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage). When the time is up, it fails with what it was waiting for.

## DiscordChannel

Returned by [`DiscordFixture.createChannel()`](https://channelstack.dev/docs/reference/api/discord#createchannel), [`DiscordUser.startThread()`](https://channelstack.dev/docs/reference/api/discord#startthread), [`DiscordUser.openDM()`](https://channelstack.dev/docs/reference/api/discord#opendm).

| Field       | Python / Rust | Type                                |
| ----------- | ------------- | ----------------------------------- |
| `id`        | `id`          | `string`                            |
| `kind`      | `kind`        | `"text" \| "dm" \| "public_thread"` |
| `channel`   | `channel`     | `"discord"`                         |
| `name`      | `name`        | `string \| undefined`               |
| `parentId`  | `parent_id`   | `string \| undefined`               |
| `starterId` | `starter_id`  | `string \| undefined`               |

### messages()

A channel's or thread's messages, oldest first.

```ts title="TypeScript"
const messages = await channel.messages();
```

```python title="Python"
messages = await channel.messages()
```

```rust title="Rust"
let messages = channel.messages().await?;
```

**Returns** [`DiscordMessages`](https://channelstack.dev/docs/reference/api/discord#discordmessages).

### members()

Who sees a channel: a text channel's participants, a DM's two ends; a thread's are its channel's. Admins see every channel without being listed.

```ts title="TypeScript"
const members = await channel.members();
```

```python title="Python"
members = await channel.members()
```

```rust title="Rust"
let members = channel.members().await?;
```

**Returns** [`DiscordMembers`](https://channelstack.dev/docs/reference/api/discord#discordmembers).

### threads()

The threads in a channel.

```ts title="TypeScript"
const threads = await channel.threads();
```

```python title="Python"
threads = await channel.threads()
```

```rust title="Rust"
let threads = channel.threads().await?;
```

**Returns** [`DiscordThreads`](https://channelstack.dev/docs/reference/api/discord#discordthreads).

### transcript()

A channel or thread as a person reads it, each thread under the message that starts it.

```ts title="TypeScript"
const transcript = await channel.transcript({ as });
```

```python title="Python"
transcript = await channel.transcript(as_=as_)
```

```rust title="Rust"
let transcript = channel.transcript().r#as(&r#as).await?;
```

| Parameter | Python / Rust  | Type                        | Required |        |
| --------- | -------------- | --------------------------- | -------- | ------ |
| `as`      | `as_` · `r#as` | `DiscordUser \| DiscordApp` | no       | option |

**Returns** [`DiscordTranscript`](https://channelstack.dev/docs/reference/api/discord#discordtranscript).

### waitForMessage()

Waits for the next message after the last one this handle returned: it polls `messages()` and never sleeps longer than it must.

```ts title="TypeScript"
const message = await channel.waitForMessage({ from: "my-agent" });
```

```python title="Python"
message = await channel.wait_for_message(from_="my-agent")
```

```rust title="Rust"
let message = channel.wait_for_message().from("my-agent").await?;
```

| Parameter | Python / Rust    | Type                                  | Required |                     |
| --------- | ---------------- | ------------------------------------- | -------- | ------------------- |
| `from`    | `from_` · `from` | `DiscordUser \| DiscordApp \| string` | no       | a handle, or a name |
| `timeout` | `timeout`        | `Duration`                            | no       | default 10s         |

**Returns** [`DiscordMessage`](https://channelstack.dev/docs/reference/api/discord#discordmessage). When the time is up, it fails with what it was waiting for.

## DiscordDelivery

Returned by [`DiscordApp.react()`](https://channelstack.dev/docs/reference/api/discord#react), [`DiscordApp.unreact()`](https://channelstack.dev/docs/reference/api/discord#unreact), [`DiscordUser.react()`](https://channelstack.dev/docs/reference/api/discord#react-1), [`DiscordUser.unreact()`](https://channelstack.dev/docs/reference/api/discord#unreact-1).

| Field      | Python / Rust     | Type                                                                      |
| ---------- | ----------------- | ------------------------------------------------------------------------- |
| `id`       | `id`              | `string`                                                                  |
| `channel`  | `channel`         | `"discord"`                                                               |
| `app`      | `app`             | `string`                                                                  |
| `type`     | `type` · `r#type` | `string`                                                                  |
| `payload`  | `payload`         | `unknown \| undefined`                                                    |
| `attempts` | `attempts`        | `DeliveryAttempt[]`                                                       |
| `status`   | `status`          | `"pending" \| "acknowledged" \| "failed" \| "timed_out" \| "unreachable"` |

### refresh()

Re-read a delivery: its attempts and status.

```ts title="TypeScript"
const updated = await delivery.refresh();
```

```python title="Python"
updated = await delivery.refresh()
```

```rust title="Rust"
let updated = delivery.refresh().await?;
```

**Returns** [`DiscordDelivery`](https://channelstack.dev/docs/reference/api/discord#discorddelivery).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### redeliver()

Dispatch the same event to the bot again, as a replay after a RESUME would.

```ts title="TypeScript"
const deliveryAttempt = await delivery.redeliver();
```

```python title="Python"
delivery_attempt = await delivery.redeliver()
```

```rust title="Rust"
let delivery_attempt = delivery.redeliver().await?;
```

**Returns** [`DeliveryAttempt`](https://channelstack.dev/docs/reference/api/client#deliveryattempt).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Discord dispatches an event once a session, and has no API to send one again.

### resend()

Send an interaction again, forged: signed with a key that isn't the app's, which its endpoint must refuse with 401.

```ts title="TypeScript"
const deliveryAttempt = await delivery.resend({ badSignature });
```

```python title="Python"
delivery_attempt = await delivery.resend(bad_signature=bad_signature)
```

```rust title="Rust"
let delivery_attempt = delivery.resend().bad_signature(bad_signature).await?;
```

| Parameter      | Python / Rust   | Type      | Required |        |
| -------------- | --------------- | --------- | -------- | ------ |
| `badSignature` | `bad_signature` | `boolean` | no       | option |

**Returns** [`DeliveryAttempt`](https://channelstack.dev/docs/reference/api/client#deliveryattempt).

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): Live, the platform delivers to the agent itself and channelstack isn't in the path (delivery chaos is fake-only).

### settled()

Waits for the delivery to settle: it polls `refresh()` and never sleeps longer than it must.

```ts title="TypeScript"
const updated = await delivery.settled();
```

```python title="Python"
updated = await delivery.settled()
```

```rust title="Rust"
let updated = delivery.settled().await?;
```

| Parameter | Type       | Required |             |
| --------- | ---------- | -------- | ----------- |
| `timeout` | `Duration` | no       | default 10s |

**Returns** [`DiscordDelivery`](https://channelstack.dev/docs/reference/api/discord#discorddelivery). When the time is up, it fails with what it was waiting for.

## DiscordFault

Returned by [`DiscordFixture.fault()`](https://channelstack.dev/docs/reference/api/discord#fault).

| Field | Type     |
| ----- | -------- |
| `id`  | `string` |

### undo()

Remove a fault.

```ts title="TypeScript"
await fault.undo();
```

```python title="Python"
await fault.undo()
```

```rust title="Rust"
fault.undo().await?;
```

**Returns** nothing. Disposing the handle calls it.

**Live:** refused with [`unsupported_in_live`](https://channelstack.dev/docs/reference/api/errors#unsupported-in-live): a fault changes what the bot's calls and its Gateway connection get, and live nothing sits between the bot and Discord: its Gateway doesn't go through the proxy.

## Types

Plain values the handles return and take.

### DiscordGuild

| Field  | Type     |
| ------ | -------- |
| `id`   | `string` |
| `name` | `string` |

### DiscordApps

```ts
type DiscordApps = DiscordApp[];
```

### DiscordMessageInput

| Field     | Type                     |
| --------- | ------------------------ |
| `content` | `string \| undefined`    |
| `embeds`  | `unknown[] \| undefined` |

### DiscordUsers

```ts
type DiscordUsers = DiscordUser[];
```

### DiscordChannels

```ts
type DiscordChannels = DiscordChannel[];
```

### DiscordButtonRef

| Field      | Python / Rust | Type                  |
| ---------- | ------------- | --------------------- |
| `label`    | `label`       | `string \| undefined` |
| `customId` | `custom_id`   | `string \| undefined` |

### DiscordEphemeralMessages

```ts
type DiscordEphemeralMessages = DiscordMessage[];
```

### DiscordMessages

```ts
type DiscordMessages = DiscordMessage[];
```

### DiscordMembers

```ts
type DiscordMembers = Array<DiscordUser | DiscordApp>;
```

### DiscordThreads

```ts
type DiscordThreads = DiscordChannel[];
```

### DiscordTranscript

| Field     | Type                       |
| --------- | -------------------------- |
| `entries` | `DiscordTranscriptEntry[]` |
| `text`    | `string`                   |

### DiscordTranscriptEntry

| Field       | Python / Rust | Type                            |
| ----------- | ------------- | ------------------------------- |
| `id`        | `id`          | `string`                        |
| `author`    | `author`      | `DiscordPrincipal`              |
| `at`        | `at`          | `string`                        |
| `text`      | `text`        | `string`                        |
| `controls`  | `controls`    | `string[]`                      |
| `edited`    | `edited`      | `boolean`                       |
| `depth`     | `depth`       | `number`                        |
| `replyTo`   | `reply_to`    | `string \| undefined`           |
| `streaming` | `streaming`   | `"open" \| undefined`           |
| `visibleTo` | `visible_to`  | `DiscordPrincipal \| undefined` |

### DiscordPrincipal

| Field  | Type              |
| ------ | ----------------- |
| `id`   | `string`          |
| `name` | `string`          |
| `kind` | `"user" \| "app"` |

### DiscordReplies

```ts
type DiscordReplies = DiscordMessage[];
```

### DiscordReactions

```ts
type DiscordReactions = DiscordReaction[];
```

### DiscordReaction

| Field   | Type                               |
| ------- | ---------------------------------- |
| `emoji` | `string`                           |
| `by`    | `Array<DiscordUser \| DiscordApp>` |

### DiscordMessageDeliveries

```ts
type DiscordMessageDeliveries = DiscordDelivery[];
```

### DiscordEndpointCheck

| Field        | Type       |
| ------------ | ---------- |
| `valid`      | `boolean`  |
| `problems`   | `string[]` |
| `deliveries` | `string[]` |

### DiscordDeliveries

```ts
type DiscordDeliveries = DiscordDelivery[];
```

### DiscordApiCalls

```ts
type DiscordApiCalls = DiscordApiCall[];
```

### DiscordApiCall

| Field            | Python / Rust     | Type                   |
| ---------------- | ----------------- | ---------------------- |
| `id`             | `id`              | `string`               |
| `channel`        | `channel`         | `"discord"`            |
| `operation`      | `operation`       | `string`               |
| `method`         | `method`          | `string`               |
| `path`           | `path`            | `string`               |
| `at`             | `at`              | `string`               |
| `durationMs`     | `duration_ms`     | `number`               |
| `request`        | `request`         | `unknown \| undefined` |
| `responseStatus` | `response_status` | `number`               |
| `response`       | `response`        | `unknown \| undefined` |
| `error`          | `error`           | `string \| undefined`  |
