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

如何在discord.js中向指定频道发送带按钮的Embed消息?

Discord.js 发送Embed+组件报错解决

问题描述

尝试向指定频道(ID:1068005047373414481)发送带有ActionRow按钮组件的Embed消息,该频道并非交互触发的频道。使用 interaction.guild.channels.cache.get(id).send(embed); 方法发送时失败,始终抛出 DiscordAPIError[50006]: Cannot send an empty message 错误,同时不确定能否像 interaction.reply({ embeds: embed, components: [row] }); 那样为消息添加组件。

测试代码

client.once('ready', () => {
    console.log('Ready!')
    client.user.setActivity("all you rascals", {
        type: ActivityType.Watching,
    });
    var testing = new EmbedBuilder()
    .setTitle('test')

    client.guilds.cache.get(guild).channels.cache.get(channel).send(testing)
})

报错信息

C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293
        throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[50006]: Cannot send an empty message
    at SequentialHandler.runRequest (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
    at async REST.request (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22)
    at async TextChannel.send (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:15) {
  rawError: { message: 'Cannot send an empty message', code: 50006 },
  code: 50006,
  status: 400,
  method: 'POST',
  url: 'https://discord.com/api/v10/channels/1068005047373414481/messages',
  requestBody: {
    files: [],
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    }
  }
}

错误原因

Discord.js v14及以上版本中,TextChannel.send() 方法要求传入配置对象,而非直接传入Embed实例。测试代码中直接传Embed导致API请求的 embeds 字段为 undefined,被判定为“空消息”。

修复方案

1. 修正Embed发送格式

将 send(testing) 改为 send({ embeds: [testing] }),同时确保 guild 和 channel 变量替换为实际的服务器ID和频道ID:

client.once('ready', () => {
    console.log('Ready!')
    client.user.setActivity("all you rascals", {
        type: ActivityType.Watching,
    });
    const testing = new EmbedBuilder()
        .setTitle('test')
    
    // 替换为实际的服务器ID和频道ID
    const targetChannel = client.guilds.cache.get('你的服务器ID').channels.cache.get('1068005047373414481');
    targetChannel.send({ embeds: [testing] });
})

2. 添加ActionRow组件

完全可以像 interaction.reply 一样给消息添加组件,只需在配置对象中加入 components 字段,示例如下:

client.once('ready', () => {
    console.log('Ready!')
    client.user.setActivity("all you rascals", {
        type: ActivityType.Watching,
    });
    const testing = new EmbedBuilder()
        .setTitle('test');
    
    // 创建ActionRow和按钮
    const testButton = new ButtonBuilder()
        .setLabel('测试按钮')
        .setStyle(ButtonStyle.Primary)
        .setCustomId('test_button');
    
    const row = new ActionRowBuilder().addComponents(testButton);
    
    const targetChannel = client.guilds.cache.get('你的服务器ID').channels.cache.get('1068005047373414481');
    targetChannel.send({ 
        embeds: [testing],
        components: [row] 
    });
})

注意事项

  • 确保机器人拥有目标频道的发送消息和嵌入链接权限
  • 若使用缓存获取频道失败,可改用 client.channels.fetch('频道ID') 方法(缓存可能未加载目标频道)

内容的提问来源于stack exchange,提问作者TheC0mpu1er

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:05:47