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

Node.js开发的Discord Bot无报错上线但不响应命令

Discord Bot在线但不响应前缀命令问题排查

问题描述

我使用Node.js开发了一款Discord Bot,执行node .或node main.js时代码无报错,Bot也能在Discord中正常切换为在线状态,但无法响应命令。Bot在服务器中拥有管理员角色及全部权限。

主代码

const { Client, Intents, DiscordAPIError } = require('discord.js');
const { token } = require('./config.json');
Discord = require("discord.js")
const client = new Client({ intents: [32767]});

const prefix = '!';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFile = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFile) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Ready!');
})

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') {
        client.commands.get('ping').execute(message,args);
    }
});

client.login(token);

/commands/ping.js代码

module.exports = {
    name: 'ping',
    description: "this is a ping command",
    execute(message, args) {
        message.channel.send('pong');
    }
}

解决方案

  • 开启Message Content Intent权限
    登录Discord开发者门户,进入你的Bot应用,切换到「Bot」标签页,在「Privileged Gateway Intents」下找到「Message Content Intent」并开启,保存后重启Bot。Discord.js v14及以上版本必须开启此权限才能读取消息内容。

  • 验证意图配置
    虽然使用32767代表所有意图,但显式声明所需意图可避免隐式问题:

    const client = new Client({ 
      intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.MESSAGE_CONTENT
      ] 
    });
    
  • 检查命令加载状态
    在命令循环中添加日志,确认命令是否成功加载:

    for(const file of commandFile) {
        const command = require(`./commands/${file}`);
        console.log(`已加载命令: ${command.name}`);
        client.commands.set(command.name, command);
    }
    

    运行Bot后查看控制台是否输出「已加载命令: ping」,若未输出则检查./commands/目录路径或文件是否存在。

  • 调试消息接收事件
    在messageCreate事件开头添加日志,确认Bot是否接收到消息:

    client.on('messageCreate', message => {
        console.log(`收到消息: ${message.content} 来自 ${message.author.tag}`);
        if(!message.content.startsWith(prefix) || message.author.bot) return;
        // 剩余代码...
    });
    

    若控制台无消息输出,说明Bot未接收到消息事件,需重新确认意图权限配置。

  • 简化命令执行逻辑
    暂时跳过命令集合,直接在事件中执行命令,排除集合加载问题:

    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');
        }
    });
    

    若此方式能响应命令,说明命令集合加载存在问题,检查文件导出格式或路径。

内容的提问来源于stack exchange,提问作者CT4TuEI3

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 15:15:38