Discord.js中message.reply函数无法正常工作的问题求助
解决Discord.js中message.reply无法工作的问题
问题根源
你的代码存在两个关键问题导致机器人无法回复消息:
- 未实际调用
message.reply()方法,仅在控制台输出内容 - 缺少监听消息所需的网关意图,Discord不会推送消息事件给机器人
修正后的代码
// Require the necessary discord.js classes const { Client, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); // Create a new client instance - 添加消息相关意图 const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); // When the client is ready, run this code (only once) client.once('ready', () => { console.log('The Bot is ready'); }); client.on('messageCreate', (message) => { if(message.content === 'hello') { // 调用reply方法发送回复 message.reply('hello buddy'); console.log('已发送回复: hello buddy'); } }) // Login to Discord with your client's token client.login(token);
关键修改说明
- 添加网关意图:新增
GuildMessages(允许接收服务器内的消息事件)和MessageContent(允许读取消息内容),这两个是监听并回复消息必须的权限 - 替换控制台输出为reply调用:把
console.log('hello buddy')改成message.reply('hello buddy'),让机器人实际发送消息到频道
内容的提问来源于stack exchange,提问作者Mido
相关产品推荐
相关产品推荐

