You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:09:03