Repository: discordjs/discord.js
File: packages/discord.js/src/structures/MessagePayload.js (JavaScript)

Code before:

      if (reference.messageId) {
        message_reference = {
          message_id: reference.messageId,
          channel_id: reference.channelId,
          guild_id: reference.guildId,
          type: reference.type,
          fail_if_not_exists: reference.failIfNotExists ?? this.target.client.options.failIfNotExists,
        };
      }
    }

    const attachments = this.options.files?.map((file, index) => ({
      id: index.toString(),
      description: file.description,
      title: file.title,
      waveform: file.waveform,
      duration_secs: file.duration,
    }));

    // Only passable during edits
    if (Array.isArray(this.options.attachments)) {
      this.options.attachments = this.options.attachments
        // Note how we don't check for file body encodable, since we aren't expecting file data here
        .map(attachment => (isJSONEncodable(attachment) ? attachment.toJSON() : attachment))
        .concat(attachments ?? []);
    } else {
      this.options.attachments = attachments;
    }

    let poll;
    if (this.options.poll) {
      poll = isJSONEncodable(this.options.poll)
        ? this.options.poll.toJSON()
        : {
            question: {
              text: this.options.poll.question.text,
            },
            answers: this.options.poll.answers.map(answer => ({
              poll_media: { text: answer.text, emoji: resolvePartialEmoji(answer.emoji) },
            })),
            duration: this.options.poll.duration,
            allow_multiselect: this.options.poll.allowMultiselect,
            layout_type: this.options.poll.layoutType,
          };
    }

    this.body = {
      content,
      tts,
      nonce,

Review comment (bug):
I feel like now that we overwrite `this.options.attachments` either way it should probably not be overwriting the options at all and instead be a const we assign or make `attachments` a let and modify in the `if` case.

Feels wrong to use `this.options.attachments` in the returned payload when it never will be the actual passed value at that point.

Code after:

      if (reference.messageId) {
        message_reference = {
          message_id: reference.messageId,
          channel_id: reference.channelId,
          guild_id: reference.guildId,
          type: reference.type,
          fail_if_not_exists: reference.failIfNotExists ?? this.target.client.options.failIfNotExists,
        };
      }
    }

    const attachments = this.options.files?.map((file, index) => ({
      id: index.toString(),
      description: file.description,
      title: file.title,
      waveform: file.waveform,
      duration_secs: file.duration,
    }));

    // Only passable during edits
    if (Array.isArray(this.options.attachments)) {
      attachments.push(
        // Note how we don't check for file body encodable, since we aren't expecting file data here
        ...this.options.attachments.map(attachment => (isJSONEncodable(attachment) ? attachment.toJSON() : attachment)),
      );
    }

    let poll;
    if (this.options.poll) {
      poll = isJSONEncodable(this.options.poll)
        ? this.options.poll.toJSON()
        : {
            question: {
              text: this.options.poll.question.text,
            },
            answers: this.options.poll.answers.map(answer => ({
              poll_media: { text: answer.text, emoji: resolvePartialEmoji(answer.emoji) },
            })),
            duration: this.options.poll.duration,
            allow_multiselect: this.options.poll.allowMultiselect,
            layout_type: this.options.poll.layoutType,
          };
    }

    this.body = {
      content,
      tts,
      nonce,
      enforce_nonce,
      embeds: this.options.embeds?.map(embed =>