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

如何让Discord.js v14机器人对自身嵌入消息添加反应?

解决Discord.js v14机器人给自身嵌入消息加反应的问题

问题核心:你当前调用的message.react()是对用户发送的命令消息添加反应,而非机器人刚发送的嵌入消息。在Discord.js v14中,channel.send()返回一个Promise,必须先获取该Promise解析后的机器人消息实例,才能对其添加反应。

方法一:使用Async/Await(推荐,代码更清晰)

确保你的命令处理函数被声明为async,然后按以下方式修改代码:

// 注意函数要加async,比如:async (message) => { ... }
if (command === 'embedMessage') {
    await message.delete(); // 删除用户的命令消息
    // 保存机器人发送的嵌入消息对象
    const sentEmbedMessage = await message.channel.send({
      embeds: [ new EmbedBuilder()
        .setDescription('Description')
        .setColor("Green")
      ],
    });
    // 对机器人自身的嵌入消息添加反应
    await sentEmbedMessage.react('😄');
}

方法二:使用.then链式调用

如果不想用async/await,也可以通过Promise链式调用实现:

if (command === 'embedMessage') {
    message.delete().then(() => {
        message.channel.send({
          embeds: [ new EmbedBuilder()
            .setDescription('Description')
            .setColor("Green")
          ],
        }).then(sentEmbedMessage => {
            sentEmbedMessage.react('😄');
        });
    });
}

不管是Discord.js旧版本还是v14,核心逻辑都是要拿到机器人发送的消息实例——v14只是要求更严格地处理异步操作的Promise,确保你在消息发送完成后再执行添加反应的操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 00:33:24