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

discord.js从v12升级至v13后扫描embed内容报错如何解决

适配discord.js v13的修改说明

  • 事件名调整:v13版本已将message事件重命名为messageCreate,原有监听逻辑不会触发
  • 新增意图要求:读取消息内容需要新增GUILD_MESSAGE_CONTENT意图,同时需要在Discord开发者后台的机器人设置页面开启「Message Content Intent」权限
  • 发送格式调整:v13发送Embed需要将实例放入embeds数组参数中,不能直接传递Embed实例
  • 逻辑错误修复:原有代码混用敏感词索引和Embed数组索引,会触发读取undefined属性的报错,调整为分层遍历:先遍历所有Embed,再遍历所有敏感词做匹配
  • 语法错误修复:补全client.login参数缺失的单引号,修正敏感词匹配逻辑(不能直接将数组传入includes方法)
  • 匹配范围优化:新增Embed描述、字段内容的敏感词检测,避免漏检
  • 新增机器人消息过滤:避免机器人自身发送的消息触发检测逻辑导致循环调用
const { Client, Intents, MessageEmbed } = require('discord.js');

const client = new Client({ 
  intents: [
    Intents.FLAGS.GUILDS, 
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MESSAGE_CONTENT
  ] 
});
let prefix = ";";
// 统一转小写方便匹配
let badwords = ["ip logger", "grabify", "dlscord.com"]; 

client.on('ready', () => {
    console.log('Ready!');
});

client.on("messageCreate", async message => {
    // 忽略机器人自己发的消息,避免循环触发
    if (message.author.bot) return;

    const detectedEmbed = new MessageEmbed()
    .setColor('#0099ff')
    .setTitle('Detected')
    .setAuthor("apphx the ass grabber")
    .setDescription('IP Logger Detected')
    .setThumbnail('https://kinsta.com/wp-content/uploads/2017/09/external-ip-address-1-1024x512.png')

    // 先检测普通消息内容
    const contentLower = message.content.toLowerCase();
    const hasBadWordInContent = badwords.some(word => contentLower.includes(word));
    if (hasBadWordInContent) {
        await message.delete().catch(() => {});
        return message.channel.send({ embeds: [detectedEmbed] });
    }

    // 检测所有Embed内容
    for (const embed of message.embeds) {
        // 拼接Embed所有需要检测的内容
        const embedContent = [
            embed.title,
            embed.description,
            ...embed.fields.map(f => `${f.name} ${f.value}`)
        ].filter(Boolean).join(' ').toLowerCase();

        // 检测是否包含敏感词
        const hasBadWordInEmbed = badwords.some(word => embedContent.includes(word));
        if (hasBadWordInEmbed) {
            await message.delete().catch(() => {});
            return message.channel.send({ embeds: [detectedEmbed] });
        }
    }
})

client.login('verysecrettoken');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 00:18:05