# Direct messages

> A bot talks privately with one member: what it needs, how to send, what the member controls, and the limits against spam.

A bot can talk to a member privately, in a **direct message** (DM): a channel only the bot and that
member can see. Not the owner, not administrators. The host relays it like any channel, so it is as
private as the rest of the community: it never leaves the host, and no third party gets it.

A DM is always between one bot and one member; members don't DM each other through the host.

## What the bot needs

- The **`dms` capability** in its manifest, granted by the owner. The operator can forbid it for the
  whole host with `[bots] allow_dms = false`. Members see "can send direct messages" on the bot
  card.
- `events = ["dms"]` (or `*`) to hear DMs opening and closing, and `messages` to hear what members
  write in them. A DM to the bot always arrives whole, even without `message_content`.

```toml title="zaun-bot.toml"
events = ["dms", "messages"]

[capabilities]
dms = true
```

## Sending

**TypeScript:**

```ts
// From any event listener:
const { channelId, messageId } = ctx.dm(memberId, 'Hi! Your report was received.');

// From a command, to the person who used it:
bot.messages.dm(interaction.member.memberId, { content: 'Here are your details', embeds: [details] });
interaction.reply({ content: 'Check your direct messages.', ephemeral: true });

// Lower level:
const dm = bot.dms.open(memberId);   // the DM channel, opened if needed
bot.dms.listOpen();                  // the bot's open DMs
bot.dms.close(dm.channelId);
```

A refusal (the member turned DMs from bots off) throws `ZaunError` with `code: 'FORBIDDEN'` and
`detail: 'dms_closed'`.

**Rust:**

```rust
// From any event handler:
let (channel_id, message_id) = ctx.dm(&member_id, "Hi! Your report was received.")?;

// From a command handler, to the person who used it:
let me = ctx.member.member_id.clone();
ctx.dm(&me, Reply::from("Here are your details").embed(details))?;
ctx.reply_ephemeral("Check your direct messages.")?;
```

The lower-level calls are in `zaun_bot::dms`: `open(member_id)`, `close(channel_id)`,
`list_open()`. A refusal is a `HostError` with code `FORBIDDEN` and reason `dms_closed`.

`dm` opens the DM when there is none, then sends. Replies, reactions, embeds, components and modals
all work in a DM, and so do the bot's own slash commands (only that bot's commands work there).
Message events in a DM are marked (`message.isDM` in TypeScript, `is_dm` in Rust).

## The member decides

- A member can turn **DMs from bots off**, per community. Then a bot can't open a new DM with them:
  the call fails with `FORBIDDEN` and reason `dms_closed`. Handle it; it isn't an error in your
  bot.
- A member can **close** a DM. Until they open it again (or write in it), the bot can't write into
  it (`dms_closed`).
- Members can open a DM with a bot themselves, if the bot has `dms`.

In apps, members do this with [zaun-client](/docs/guide/client/direct-messages/): `member.dm()`,
`channel.closeDm()` and `community.setDmsFromBots(false)`.

## Limits against spam

- A bot opens at most **10 new DMs per minute**.
- Into a DM the member has never written in, a bot writes at most **5 messages per hour**. Once the
  member writes (or uses a command or button there), the limit lifts.
- Beyond either: `RATE_LIMITED` with category `dms` and a retry time.

Answers to interactions are never limited this way: the member asked.

## Resulting code

See the [welcome DM example](/docs/examples/welcome-dm/): it greets new members by DM, answers what they
write back, and offers `/dm-me`.
