> ## Documentation Index
> Fetch the complete documentation index at: https://www.chapterjs.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Message

> A message sent in a channel: what it contains, and how to reply, edit, react, pin and start a thread from it

A `Message` is a message sent in a channel. You get one from the [events about messages](https://www.chapterjs.org/events/messages.md), from what `send()` and `reply()` return, and from `fetchMessage()` and `fetchMessages()` on a channel.

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
import type { Message } from 'chapterjs';

async function onPing(message: Message) {
  if (!message.isFromUser) return; // ignore bots, webhooks and system messages
  if (message.content !== '!ping') return;

  const reply = await message.reply('Pong!');
  await reply.react('🏓');
}
```

> [!NOTE]
>
> How to send, edit and read messages is in [Sending messages](https://www.chapterjs.org/messages.md); everything `reply()` and `edit()` accept is in [Message options](https://www.chapterjs.org/reference/message-options.md).

> [!WARNING]
>
> `content`, `embeds` and `attachments` are empty for the messages of other people unless the **Message Content Intent** is enabled for your bot. Messages that mention the bot, and private messages, always have their content.

## Properties

**id** `Snowflake`

The unique id Discord gave it.

**createdAt** `Date`

When it was created: a Discord id contains its creation date.

**author** `User`

Who wrote the message.

**mentions** `readonly User[]`

The users mentioned in the message.

**channelId** `Snowflake`

The id of the channel of the message.

**guildId** `Snowflake | null`

The id of the server; `null` for a private message.

**channel** `TextBasedChannel | null`

The channel of the message, when it is known.

**guild** `Guild | null`

The server of the message; `null` for a private message.

**member** `GuildMember | null`

The author as a member of the server, when it is known.

**content** `string`

The text of the message. Empty when the bot is not allowed to read it: reading the messages of others needs the Message Content intent.

**type** `MessageType`

The kind of message, as a value of [`MessageType`](https://www.chapterjs.org/reference/enums#messagetype) (a regular one, a reply, a join notice...).

**editedAt** `Date | null`

When the message was last edited; `null` when it never was.

**pinned** `boolean`

Whether the message is pinned.

**tts** `boolean`

Whether the message was read aloud.

**embeds** `Embed[]`

The embeds of the message. Each entry is an [`Embed`](https://www.chapterjs.org/reference/message-options#embed).

**attachments** `Attachment[]`

The files attached to the message. Each entry is an [`Attachment`](https://www.chapterjs.org/reference/data#attachment).

**reactions** `Reaction[]`

The reactions on the message, one entry per emoji. Each entry is a [`Reaction`](https://www.chapterjs.org/reference/data#reaction).

**mentionsEveryone** `boolean`

Whether the message mentions @everyone or @here.

**mentionedRoleIds** `readonly Snowflake[]`

The ids of the roles mentioned in the message.

**flags** `MessageFlagName[]`

The flags of the message, by name (`Ephemeral`, `SuppressEmbeds`...).

**webhookId** `Snowflake | null`

The id of the webhook that sent the message, when one did.

**repliedMessageId** `Snowflake | null`

The id of the message this one replies to, when it is a reply.

**isFromUser** `boolean`

Whether a person wrote it: not a bot, a webhook or Discord itself.

**url** `string`

The link that opens the message in Discord.

## Helpers

These read what the bot already knows: they never send a request.

### toJSON()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
toJSON(): object
```

The data of Discord as a plain object, with camelCase names.

## Reading from Discord

### fetch()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
fetch(): Promise<Message>
```

Asks Discord for the latest version of the message.

[Discord documentation](https://docs.discord.com/developers/resources/message#get-channel-message)

### fetchReactionUsers()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
fetchReactionUsers(emoji: EmojiInput, options?: { limit?: number; after?: Snowflake }): Promise<User[]>
```

The users who reacted with an emoji (100 at most per call).

[Discord documentation](https://docs.discord.com/developers/resources/message#get-reactions)

## Actions

### reply()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
reply(message: MessageInput): Promise<Message>
```

Answers the message, in the same channel. What it accepts is a [`MessageInput`](https://www.chapterjs.org/reference/message-options#messageinput).

[Discord documentation](https://docs.discord.com/developers/resources/message#create-message)

### edit()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
edit(message: string | MessageEditOptions): Promise<Message>
```

Changes the message. Only the content, embeds, components and files of a message the bot wrote can change. The options are a [`MessageEditOptions`](https://www.chapterjs.org/reference/message-options#messageeditoptions).

[Discord documentation](https://docs.discord.com/developers/resources/message#edit-message)

### delete()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
delete(reason?: string): Promise<void>
```

Deletes the message.

[Discord documentation](https://docs.discord.com/developers/resources/message#delete-message)

### react()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
react(emoji: EmojiInput): Promise<void>
```

Adds a reaction of the bot to the message.

[Discord documentation](https://docs.discord.com/developers/resources/message#create-reaction)

### removeReaction()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
removeReaction(emoji: EmojiInput, user?: Snowflake | { id: Snowflake }): Promise<void>
```

Removes a reaction: of the bot, or of the given user.

[Discord documentation](https://docs.discord.com/developers/resources/message#delete-user-reaction)

### clearReactions()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
clearReactions(emoji?: EmojiInput): Promise<void>
```

Removes every reaction of the message, or every reaction of one emoji.

[Discord documentation](https://docs.discord.com/developers/resources/message#delete-all-reactions)

### pin()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
pin(reason?: string): Promise<void>
```

Pins the message in its channel.

[Discord documentation](https://docs.discord.com/developers/resources/message#pin-message)

### unpin()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
unpin(reason?: string): Promise<void>
```

Unpins the message.

[Discord documentation](https://docs.discord.com/developers/resources/message#unpin-message)

### crosspost()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
crosspost(): Promise<Message>
```

Publishes a message of an announcement channel to the servers that follow it.

[Discord documentation](https://docs.discord.com/developers/resources/message#crosspost-message)

### startThread()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
startThread(
  options: { name: string; autoArchiveDuration?: number; rateLimitPerUser?: number | null },
  reason?: string
): Promise<ThreadChannel>
```

Starts a thread from the message.

**name** `string` *required*

The name of the thread, 1 to 100 characters.

**autoArchiveDuration** `number`

After how many minutes without activity the thread leaves the channel list: 60, 1440, 4320 or 10080.

**rateLimitPerUser** `number | null`

How many seconds a member waits between two messages (0 to 21600).

[Discord documentation](https://docs.discord.com/developers/resources/channel#start-thread-from-message)

## Waiting

They wait for what comes next on the message, while your bot runs: a restart ends the wait. How to use them is in the [guide](https://www.chapterjs.org/waiting.md).

### waitForClick()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
waitForClick(options?: WaitForClickOptions): Promise<Click | null>
```

Waits for a click on a button or a menu of this message: of a person (`from`), anyone by default. Resolves with the click, to answer with `click.interaction`, or `null` when `within` (one minute by default) passed first. A click taken here does not run the `run` of the button; a button that is only waited for needs none. A restart of the bot ends the wait.

### waitForReaction()

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
waitForReaction(options?: WaitForReactionOptions): Promise<WaitedReaction | null>
```

Waits for a reaction added to this message: of a person (`from`), anyone's but a bot's by default, with an emoji (`emoji`), any by default. Resolves with it, or `null` when `within` (one minute by default) passed first. A restart of the bot ends the wait.

## Types

### GuildMessage

A message of a server: its server is known. It is what the events about messages give for what happens in a server.

**guildId** `Snowflake`

The id of the server.

**guild** `Guild`

The server of the message.

**channel** `GuildTextBasedChannel`

The channel of the message.

### MemberMessage

A message written in a server by one of its members: neither a private message nor the message of a webhook. It is what the events about messages give by default. It has everything of `GuildMessage`, and:

**member** `GuildMember`

The author as a member of the server.

### PrivateMessage

A private message, for a file that receives both kinds (`{ where: 'both' }`): there is no server, which is how it is told apart from a `GuildMessage`.

**guildId** `null`

No server: it is a private message.

**guild** `null`

No server: it is a private message.

**member** `null`

No member: the author is `author`.

**channel** `DMChannel | null`

The private conversation, when the bot knows it.

### DmMessage

A private message, for a file that only receives those (`{ where: 'dm' }`): nothing about a server exists on it. `guildId`, `guild` and `member` are not even offered by your editor.

**channel** `DMChannel | null`

The private conversation, when the bot knows it.

You only need these names to type a function of your own:

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
import type { MemberMessage } from 'chapterjs';

export function xpFor(message: MemberMessage): number {
  return message.member.roles.length + message.content.length;
}
```

### EmojiInput

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
type EmojiInput = string | { id?: Snowflake | null; name?: string | null }
```

An emoji to react with: a standard one (`"👍"`), or a custom one. The methods that take one accept:

* a standard emoji: `'👍'`
* a custom emoji as Discord writes it: `'<:name:123456789012345678>'` (or `'<a:name:…>'` when animated)
* a custom emoji as an object: `{ id: '123456789012345678', name: 'name' }`, or a [`GuildEmoji`](https://www.chapterjs.org/reference/emoji)

An empty text is refused before anything is sent:

```text theme={"theme":{"light":"github-light","dark":"vesper"}}
The emoji is empty: pass one like "👍".
```

### MessageFlagName

```ts theme={"theme":{"light":"github-light","dark":"vesper"}}
type MessageFlagName = keyof typeof MessageFlags
```

The name of a flag of a message: a key of [`MessageFlags`](https://www.chapterjs.org/reference/enums#messageflags). It is what `flags` lists.

### WaitForClickOptions

What `waitForClick()` takes.

**from** `PersonInput`

Only a click of this person. Anyone by default. A [`PersonInput`](https://www.chapterjs.org/reference/channels#personinput).

**within** `Duration`

How long to wait: `'30s'`, `'2m'`. One minute by default.

### Click

A click on a button or a menu of a message, as `waitForClick()` gives it.

**interaction** `ComponentInteraction`

The click itself: answer it as in the `run` of a button, with `update()`, `reply()` or `deferUpdate()`. Without an answer within 2 seconds, the bot changes nothing on screen for you (`deferUpdate()`).

**user** `User`

Who clicked.

**member** `GuildMember | null`

Who clicked, as a member of the server; `null` in a private message.

**name** `string`

The name of the button or menu: the name of its export.

**data** `Readonly\<Record\<string, string | number | boolean\>\>`

The data the button or menu carries.

**values** `readonly string[]`

What was picked in a menu: the values of a menu of texts, the ids of the people, roles or channels of the others. Empty for a button.

**is** `(component: ButtonDeclaration\<never\> | SelectDeclaration\<never\>) =\> boolean`

Whether it is this button or this menu.

### WaitForReactionOptions

What `waitForReaction()` takes.

**from** `PersonInput`

Only a reaction of this person. Anyone but a bot by default.

**emoji** `string`

Only this emoji: a standard one (`'👍'`), or a custom one by its name, its id or as Discord writes it (`'<:party:123>'`).

**within** `Duration`

How long to wait: `'30s'`, `'2m'`. One minute by default.

### WaitedReaction

A reaction added to a message, as `waitForReaction()` gives it.

**emoji** `ReactionEmoji`

The emoji.

**user** `User`

Who reacted.

**member** `GuildMember | null`

Who reacted, as a member of the server; `null` in a private message.

### Elsewhere

* [`Attachment`](https://www.chapterjs.org/reference/data#attachment) and [`Reaction`](https://www.chapterjs.org/reference/data#reaction): the entries of `attachments` and `reactions`.
* [`Embed`](https://www.chapterjs.org/reference/message-options#embed): the entries of `embeds`, and what you send.
* [`MessageInput`](https://www.chapterjs.org/reference/message-options#messageinput) and [`MessageEditOptions`](https://www.chapterjs.org/reference/message-options#messageeditoptions): what `reply()` and `edit()` take.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.