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

discord.js开发Discord bot无法日志记录用户消息问题求助

问题排查与解决方案

核心原因

你使用的discord.js v13+版本做了多处不兼容更新,旧版教程的写法已经失效,具体问题有3处:

  • 事件名废弃:旧版message事件已被移除,替换为messageCreate事件
  • Intent配置缺失:除了已配置的GUILD_MESSAGES,还需要补充GUILDS、MESSAGE_CONTENT两个必要Intent
  • 后台权限未开启:Discord要求特权Intent需要在开发者后台手动开启开关才能生效

修复步骤

  1. 打开Discord开发者后台,进入对应Bot应用的「Bot」设置页,找到Privileged Gateway Intents分区,开启Message Content Intent开关后保存
  2. 将代码替换为如下版本:
require('dotenv').config(); 
const Discord = require('discord.js');
const client = new Discord.Client({ 
  intents: [
    Discord.GatewayIntentBits.Guilds,
    Discord.GatewayIntentBits.GuildMessages,
    Discord.GatewayIntentBits.MessageContent
  ] 
});
client.on('ready', () => {
   console.log(`${client.user.tag} has logged in`);
});

client.on('messageCreate', (msg) => {
  console.log(msg.content);
});

client.login(process.env.TOKEN);

如果你更习惯用字符串形式配置Intent,可以将intents数组替换为["GUILDS", "GUILD_MESSAGES", "MESSAGE_CONTENT"],效果一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 15:15:02