You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js使用Slash命令发送DM嵌入消息失败的问题求助

Discord.js使用Slash命令发送DM嵌入消息失败的问题求助

大家好,我正在测试Discord机器人的ban命令逻辑——为了避免反复ban我的测试小号再解封,我想用给目标用户发送DM嵌入消息的方式模拟ban通知。但目前遇到了两个问题:

  • 当我同时发送文本内容和嵌入消息时,只有文本能成功送达,嵌入消息完全不会显示
  • 如果我只发送嵌入消息,直接触发错误:DiscordAPIError[50006]: Cannot send an empty message

以下是我的代码:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('test')
    .setDescription('Send a private message to a selected user')
    .addUserOption(option =>
      option
        .setName('target')
        .setDescription('The member to send the message to')
        .setRequired(true))
    .addStringOption(option =>
      option
        .setName('reason')
        .setDescription('The reason for sending the message'))
    .setDMPermission(false),

  async execute(interaction) {
    const target = interaction.options.getUser('target');
    if (!target) return interaction.reply({ content: "You must provide a target user with the 'target' option.", ephemeral: true });

    const reason = interaction.options.getString('reason') || 'No reason provided';
    const owner = await interaction.guild.fetchOwner();
    if (!owner) return interaction.reply({ content: "I couldn't fetch the guild owner.", ephemeral: true });

    if (target.user === interaction.client.user) return interaction.reply({ content: "I can't send a message to myself.", ephemeral: true });

    const banEmbed = new EmbedBuilder()
      .setColor(0x0099FF)
      .setTitle('Hello! 😃')
      .setAuthor({ name: `request by ${interaction.user.username}`, iconURL: interaction.guild.iconURL({ format: 'png', dynamic: true }) })
      .setDescription(`You have been banned from ${interaction.guild}.`)
      .addFields(
        { name: '\u200B', value: `If you think there's an error, please contact ${owner.user.username}#${owner.user.discriminator}.` },
        { name: '\u200B', value: `Reason: ${reason}` },
      )
      .setTimestamp()
      .setFooter({ text: 'Have a good day !' });

    try {
      await target.send({ content: "hello", embed: banEmbed });
      interaction.reply({ content: "The message has been sent successfully.", ephemeral: true });
    } catch (error) {
      console.error(error);
      interaction.reply({ content: "I couldn't send the message to the target user.", ephemeral: true });
    }
  },
};

当我去掉content: "hello"只保留嵌入消息时,控制台输出的完整错误信息如下:

DiscordAPIError[50006]: Cannot send an empty message
at SequentialHandler.runRequest (/Users/theophile/Desktop/test bot copie/node_modules/@discordjs/rest/dist/index.js:667:15)
at processTicksAndRejections (/Users/theophile/Desktop/test bot copie/lib/internal/process/task_queues.js:96:5)
at async SequentialHandler.queueRequest (/Users/theophile/Desktop/test bot copie/node_modules/@discordjs/rest/dist/index.js:464:14)
at async REST.request (/Users/theophile/Desktop/test bot copie/node_modules/@discordjs/rest/dist/index.js:910:22)
at async DMChannel.send (/Users/theophile/Desktop/test bot copie/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:176:15)
at async Object.execute (/Users/theophile/Desktop/test bot copie/commands/test.js:41:7)
at async Client.<anonymous> (/Users/theophile/Desktop/test bot copie/index.js:71:3) {requestBody: {…}, rawError: {…}, code: 50006, status: 400, method: 'POST', …}

有没有大佬能帮我看看问题出在哪?怎么才能让机器人正确发送带嵌入的DM消息呢?

备注:内容来源于stack exchange,提问作者FireEmblem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 15:32:46