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

Discord.js实现机器人为自身发送的Embed消息添加勾选反应

解决Discord机器人发送Embed后自动添加✅反应的问题

要实现机器人发送Embed消息后自动添加✅反应,核心是获取发送后的消息实例并调用其react()方法。以下是修改后的代码及关键说明:

修改后的完整代码

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isChatInputCommand()) return;

  // Features Command
  if (interaction.commandName === 'host') {
    const embed = new EmbedBuilder()
      .setTitle('New lobby, queue up in 1 minute!')
      .setColor('#FF0000')
      .setFooter({ text: 'discord.gg/XzNV6bxf7R' })
      .addFields(
        { name: 'VPN: Canada', value: '‎ ', inline: false },
        { name: 'React with ✅ If you will be there', value: '‎ ', inline: false },
        { name: 'Gamemode: Solo Showdown', value: '‎ ', inline: false },
        { name: 'Pin:', value: '‎ ', inline: false }
      )
      .setImage('bababababababababa');

    try {
      // 发送消息并获取返回的消息对象
      const sentMessage = await interaction.reply({ embeds: [embed], fetchReply: true });
      // 为消息添加✅反应
      await sentMessage.react('✅');
    } catch (error) {
      console.error('发送消息或添加反应时出错:', error);
    }
  }
});

关键修改点说明

  • 添加async/await支持:将事件处理函数标记为async,允许内部使用await等待异步操作完成。
  • 启用fetchReply选项:在interaction.reply()的参数中加入fetchReply: true,这是获取发送后消息对象的必要条件(默认不返回消息实例)。
  • 调用react()方法:通过获取到的sentMessage对象调用react('✅'),实现自动添加指定表情反应。
  • 错误捕获:用try/catch包裹操作,处理可能出现的异常(如机器人无添加反应的权限、消息发送失败等)。

替代方案(使用.then()链式调用)

如果你更习惯Promise链式写法,也可以这样实现:

interaction.reply({ embeds: [embed], fetchReply: true })
  .then(sentMessage => sentMessage.react('✅'))
  .catch(error => console.error('操作失败:', error));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 04:07:21