Discord机器人无法读取message.content的问题求助
解决Discord机器人无法读取消息内容的问题
你的问题核心是缺少消息内容特权意图的代码声明——discord.js v14开始,message.content的读取需要单独的MessageContent特权意图,即便你在开发者平台开启了所有特权意图,代码里未声明该意图就无法获取消息内容。
修复步骤:
- 再次确认Discord开发者平台的机器人页面中,Message Content Intent已开启(你提到已启用所有特权意图,可再次核对该选项状态)。
- 在客户端的
intents数组中添加GatewayIntentBits.MessageContent。 - 清理代码中重复的意图项(重复项不影响功能,但会造成冗余)。
修改后的代码示例:
import {Client, GatewayIntentBits} from 'discord.js'; const token = "token" const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMessageTyping, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.MessageContent // 新增消息内容特权意图 ] }) client.login(token); client.on('ready', () => { console.log(`Ready and logged on as ${client.user.tag}`); }); client.on('messageCreate', message => { console.log(message.content); // 直接打印消息内容测试 });
修改完成后重启机器人,即可正常读取message.content的内容。
内容的提问来源于stack exchange,提问作者AlexStan0
相关产品推荐
相关产品推荐

