Discord v14消息记录器无法运行求助:无报错且未发送日志到频道
问题排查与修复方案
1. 事件名称错误
Discord.js v14 已将原message事件更名为messageCreate,你的代码仍使用旧事件名,导致无法触发消息监听逻辑。
2. 频道获取方式错误
client.channels.cache.get()是通过频道ID匹配频道,但你传入的是频道名称"logs",永远无法定位到目标频道。需改用find()方法按名称+类型精准查找:
const channel = client.channels.cache.find(ch => ch.name === channelName && ch.type === ChannelType.GuildText);
添加ChannelType.GuildText可避免匹配到语音频道、分类频道等非文本频道。
3. 缺失必要的Intents(权限)
Discord v14 强制要求声明机器人所需权限才能接收消息事件。创建客户端时必须启用以下intents:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent // 必须开启才能读取消息内容 ] });
同时要在Discord开发者门户的机器人设置页,手动开启Message Content Intent开关。
4. 无错误捕获逻辑
你的代码未处理频道不存在、发送消息失败等异常,即使出错也不会有任何提示,需添加try/catch捕获错误。
完整修复代码
const { Client, GatewayIntentBits, EmbedBuilder, ChannelType } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); client.on('messageCreate', async message => { // 忽略机器人自身消息,避免循环记录 if (message.author.bot) return; const channelName = "logs"; try { const msglogEmbed = new EmbedBuilder() .setTitle('Message Log') .setDescription(`${message.author.tag}: ${message.content}`) .setTimestamp(message.createdAt) .setColor('#0099ff'); const channel = client.channels.cache.find(ch => ch.name === channelName && ch.type === ChannelType.GuildText); if (!channel) { console.error(`找不到名为${channelName}的文本频道`); return; } await channel.send({ embeds: [msglogEmbed] }); console.log(`消息已记录到频道${channelName}`); } catch (error) { console.error('记录消息时出错:', error); } }); client.login('你的机器人令牌');
额外注意事项
- 确保机器人在目标
logs频道拥有发送消息和嵌入链接权限 - 不要硬编码机器人令牌,建议用环境变量管理敏感信息
内容的提问来源于stack exchange,提问作者Gerold Michael
相关产品推荐
相关产品推荐

