On This Page

MessageBuilder

Class in Builders and Formatting · JavaScript & TypeScript

Assemble a message by chaining methods, then send the plain object returned by build()

Methods change the same builder. Content and settings replace earlier values, while embeds, attachments and stickers append. The builder reads supported properties by name, including inherited properties and getters that do not appear in normal key lists. Values that are not objects and unknown own enumerable fields remain for the message operation to validate. Building does not send or validate a message. In TypeScript, select content, an embed, an attachment or a sticker before calling build. JavaScript can build an empty object, but message operations reject it. The HasBody type parameter tracks whether a body was selected, not whether its content is valid

class MessageBuilder<HasBody extends boolean = false>class MessageBuilder<
  HasBody extends boolean = false
>

Constructor#

Start with no content or settings

TypeScript callers must use a body-selection method before build, not choose HasBody themselves

new MessageBuilder<HasBody extends boolean = false>(
    ..._empty: [HasBody] extends [false]
        ? [false] extends [HasBody] ? [] : [never]
        : [never]
): MessageBuilder<HasBody>new MessageBuilder<
  HasBody extends boolean = false
>(
  ..._empty: [HasBody] extends [false]
    ? [false] extends [HasBody]
        ? []
        : [never]
    : [never]
): MessageBuilder<HasBody>

Parameters

..._empty
[HasBody] extends [false]
    ? [false] extends [HasBody] ? [] : [never]
    : [never][HasBody] extends [false]
  ? [false] extends [HasBody]
      ? []
      : [never]
  : [never]

Properties#

build#

Return a fresh plain MessageInput after selecting content, an embed, an attachment or a sticker

Before a body is selected, TypeScript rejects the call with an error naming selectContentEmbedAttachmentOrStickerBeforeBuild. Sending or replying checks empty text, arrays, attachments and server limits, not this method. JavaScript can call this on an empty builder and receive {}, which message operations reject locally

readonly build: [HasBody] extends [true]
    ? [true] extends [HasBody]
          ? () => MessageInput
          : MissingMessageBody
    : MissingMessageBodyreadonly build: [HasBody] extends [
  true
]
  ? [true] extends [HasBody]
      ? () => MessageInput
      : MissingMessageBody
  : MissingMessageBody

Methods#

addAttachments#

Add one or more attachments in argument order and return this builder

Metadata is copied, but bytes, file and stream sources stay shared just as with attachment

addAttachments(
    ...values: readonly [AttachmentInput, AttachmentInput]
): MessageBuilder<true>addAttachments(
  ...values: readonly [
    AttachmentInput,
    AttachmentInput
  ]
): MessageBuilder<true>

Parameters

Returns
MessageBuilder<true>

addEmbeds#

Add one or more embeds in argument order and return this builder, copying each just as embed does

Message operations check the final count

addEmbeds(
    ...values: readonly [
        EmbedInput | EmbedBuilder,
        EmbedInput | EmbedBuilder
    ]
): MessageBuilder<true>addEmbeds(
  ...values: readonly [
    EmbedInput | EmbedBuilder,
    EmbedInput | EmbedBuilder
  ]
): MessageBuilder<true>

Parameters

Returns
MessageBuilder<true>

addStickers#

Add one or more decimal sticker IDs in argument order and return this builder

This does not look up or validate the stickers

addStickers(
    ...values: readonly [string, string]
): MessageBuilder<true>addStickers(
  ...values:
    readonly [string, string]
): MessageBuilder<true>

Parameters

...values
readonly [string, string]
Returns
MessageBuilder<true>

allowedMentions#

Replace who may be notified by mention text, copy user/role arrays, and preserve other values for message-operation validation

If omitted, message operations keep notifications disabled. This method alone neither inserts mention text nor notifies anyone

allowedMentions(value: AllowedMentions): thisallowedMentions(
  value: AllowedMentions
): this

Parameters

Returns
this

attachment#

Add one attachment, copying its metadata while keeping the original bytes, file or stream source

Return this builder, now buildable in TypeScript. File, stream and byte references are not copied or read here. The builder and its outputs retain those references. Sending does not clear the builder, and direct operations own copying or consuming their inputs

attachment(value: AttachmentInput): MessageBuilder<true>attachment(
  value: AttachmentInput
): MessageBuilder<true>

Parameters

Returns
MessageBuilder<true>

content#

Replace the message text and return this builder, now buildable in TypeScript

Empty or invalid text is still checked by the message operation

content(value: string): MessageBuilder<true>content(value: string): MessageBuilder<
  true
>

Parameters

value
string
Returns
MessageBuilder<true>

embed#

Add one embed and return this builder, now buildable in TypeScript

Copies the embed's nested objects and fields now. Later changes to the supplied embed or EmbedBuilder do not change this message

embed(value: EmbedInput | EmbedBuilder): MessageBuilder<true>embed(
  value: EmbedInput | EmbedBuilder
): MessageBuilder<true>

Parameters

Returns
MessageBuilder<true>

flags#

Replace the numeric message flags and return this builder, without validating the value

Message operations accept only MessageFlags.SuppressEmbeds and MessageFlags.SuppressNotifications and reject other bits before dispatch

flags(value: number): this

Parameters

value
number
Returns
this

reference#

Choose the message to reply to when using messages.send, copy its reference, then return this builder

No message is fetched

reference(value: MessageReference): thisreference(
  value: MessageReference
): this

Parameters

Returns
this

sticker#

Add a decimal sticker ID after existing stickers and return this builder

The ID and final sticker count are checked when sending

sticker(value: string): MessageBuilder<true>sticker(value: string): MessageBuilder<
  true
>

Parameters

value
string
Returns
MessageBuilder<true>