Discord机器人前缀指令失效求助:discord.js开发机器人无响应
Discord机器人启动正常但指令无响应的解决方案
核心问题分析
你的代码使用了discord.js旧版本的message事件,但在discord.js v13及以上版本中,该事件已被废弃,替换为messageCreate事件。同时,Discord API要求必须开启**消息内容特权(Message Content Intent)**才能接收消息内容,否则机器人无法读取用户发送的指令。
修复步骤
替换事件监听
将client.on('message', ...)替换为client.on('messageCreate', ...),这是新版本discord.js接收消息的标准事件。开启消息内容意图
登录Discord开发者后台,找到你的机器人应用:- 进入「Bot」页面
- 在「Privileged Gateway Intents」区域,开启「Message Content Intent」
- 保存更改
初始化客户端时传入必要的意图
创建Discord.Client实例时,需要明确传入Guilds和MessageContent等必要意图,否则机器人无法正常接收消息。
修复后的完整代码
const Discord = require('discord.js'); // 传入必要的意图 const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent] }); const prefix = '-'; client.once('ready', () => { console.log('Discord Bot Is On!'); }); // 使用messageCreate事件替代message 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 === 'fish'){ message.channel.send('Here is a fish', {files: ["fish.png"]}); } else if (command === 'bluefish'){ message.channel.send('Here is a blue fish', {files: ["bluefish.png"]}); } else if (command ==='greenfish'){ message.channel.send('Here is a green fish', {files: ["greenfish.png"]}); } else if (command ==='orangefish'){ message.channel.send('Here is a orange fish', {files: ["orangefish.png"]}); } else if (command ==='yellowfish'){ message.channel.send('Here is a yellow fish', {files: ["yellowfish.png"]}); } else if (command ==='violetfish'){ message.channel.send('Here is a violet fish', {files: ["violetfish.png"]}); } else if (command ==='indigofish'){ message.channel.send('Here is a indigo fish', {files: ["indigofish.png"]}); } else if (command ==='redfish'){ message.channel.send('Here is a red fish', {files: ["redfish.png"]}); } }); client.login('你的机器人Token');
额外检查项
- 确认机器人Token正确无误,没有泄露或过期
- 确保图片文件(如
fish.png)放在项目根目录,路径正确 - 机器人在服务器中拥有发送消息和上传文件的权限
内容的提问来源于stack exchange,提问作者Someoneǃ
相关产品推荐
相关产品推荐

