Discord Bot未响应!ping命令问题求助
问题分析与修复方案
核心问题点
- 未定义
config变量却引用config.prefix:代码中声明了const prefix = '!',但判断消息前缀、截取内容时错误使用了未定义的config.prefix,会触发ReferenceError,导致命令逻辑无法执行。 - 使用废弃事件:Discord.js v14+版本已移除
message事件,需改用messageCreate事件监听消息。 - 额外检查:需在Discord开发者平台的机器人设置中,开启Message Content Intent权限,否则机器人无法读取消息内容。
修正后的代码
const Discord = require('discord.js') const client = new Discord.Client({intents: 32767}); const prefix = '!'; client.on('messageCreate', message => { if(!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const command = args.shift().toLowerCase(); if(command === 'ping'){ message.channel.send('pong!'); } }) client.once('ready', () => { console.log('TixyBot is online!'); }); client.login('你的机器人Token')
内容的提问来源于stack exchange,提问作者Tixy
相关产品推荐
相关产品推荐

