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

Discord机器人无法检测Embed问题排查求助

Discord.js机器人无法检测Embed消息的排查与修复

问题根源

你的机器人读取不到Embed,核心原因是缺少获取消息内容的权限配置,同时未考虑部分Bot动态编辑Embed的场景。

具体修复步骤

1. 补充网关意图并开启后台权限

discord.js v14+要求显式声明意图才能获取消息的Embed内容,当前代码缺少关键的MessageContent意图:

  • 在客户端配置中添加GatewayIntentBits.MessageContent
  • 登录Discord开发者后台,进入你的机器人页面,找到Privileged Gateway Intents,开启Message Content Intent开关

2. 监听消息编辑事件(可选)

如果目标Bot是先发送空消息、再编辑添加Embed,仅监听messageCreate会错过Embed内容,需要同时监听messageUpdate事件捕获编辑后的消息。

修正后的完整代码

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent, // 新增:获取消息内容的必要意图
  ]
});

const yourUserID = '你的用户ID';
const specificBotID = '目标Bot的ID';
let cooldown = false;

client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

// 监听初始消息
client.on('messageCreate', handleBotMessage);

// 监听消息编辑(处理动态添加Embed的情况)
client.on('messageUpdate', (oldMessage, newMessage) => {
  if (newMessage.author.id === specificBotID) {
    handleBotMessage(newMessage);
  }
});

// 统一处理消息的函数
function handleBotMessage(message) {
  if (cooldown) return;

  console.log('Received message:', message);

  if (message.embeds.length > 0) {
    const embed = message.embeds[0];
    // 确保颜色转为标准十六进制格式
    const embedColor = embed.hexColor || embed.color.toString(16).padStart(6, '0');

    if (embedColor) {
      message.channel.send(`<@${yourUserID}> 指定Bot发送了消息,Embed色值为 #${embedColor.toUpperCase()}`);
    } else {
      message.channel.send(`<@${yourUserID}> 指定Bot发送了消息,但未设置Embed色值`);
    }
  } else {
    message.channel.send(`<@${yourUserID}> 指定Bot发送了消息,但未检测到Embed`);
  }

  cooldown = true;
  setTimeout(() => {
    cooldown = false;
  }, 2000);
}

// 替换为你的机器人Token
client.login('你的机器人Token');

额外注意事项

  • 确保机器人在目标频道拥有查看频道和发送消息的权限
  • 如果目标Bot通过Webhook发送Embed,message.author.id会是Webhook的ID,需调整为检测Webhook ID而非Bot用户ID

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:32:59