JS编写的Discord bot启动后无响应、无法触发指令问题求助
你的代码有三个核心问题导致无法响应消息:
- 事件监听名称错误:discord.js v13及以上版本已经废弃了
message事件,替换为messageCreate事件 - 意图拼写错误:不存在
GUILDS_MESSAGES这个意图,正确的服务器消息意图名称为GUILD_MESSAGES - 缺少必要的消息内容权限:要读取用户发送的消息内容,必须声明
MESSAGE_CONTENT意图,同时需要你在Discord开发者后台的Bot设置页开启「Message Content Intent」开关
修正后的完整代码如下:
const Discord = require('discord.js'); const { token } = require('./config.json'); // 修正意图拼写,添加必要的MESSAGE_CONTENT意图 const intents = new Discord.Intents('GUILDS', 'GUILD_MESSAGES', 'MESSAGE_CONTENT'); const client = new Discord.Client({ intents }); const prefix = '-'; const fs = require('fs'); client.commands = new Discord.Collection(); const commandFiles = fs .readdirSync('./commands/') .filter((file) => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.once('ready', () => { console.log('Ready!'); }); // 替换废弃的message事件为messageCreate client.on('messageCreate', (message) => { console.log('made it!'); 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') { client.commands.get('ping').execute(message, args); } }); client.login(token);
内容的提问来源于stack exchange,提问作者Sokker
相关产品推荐
相关产品推荐

