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

Discord.js机器人不响应命令问题排查求助

问题排查:Discord.js机器人不响应斜杠命令且无报错

核心问题分析及修复步骤

  • 缺少fs模块引入
    你的index.js中使用了fs.readdirSync读取命令文件,但未引入fs模块。在文件顶部添加:

    const fs = require('node:fs');
    

    缺少此引入会导致ReferenceError: fs is not defined,若未看到报错,需检查完整控制台输出。

  • 未部署斜杠命令到Discord服务器
    Discord的斜杠命令必须先部署到目标服务器(或全局),Discord才会识别这些命令。当前代码仅加载了本地命令文件,未执行部署操作。
    可新增部署脚本(如deploy-commands.js),示例代码如下:

    const { REST, Routes } = require('discord.js');
    const { clientId, guildId, token } = require('./config.json');
    const fs = require('node:fs');
    
    const commands = [];
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
    
    for (const file of commandFiles) {
      const command = require(`./commands/${file}`);
      commands.push(command.data.toJSON());
    }
    
    const rest = new REST({ version: '10' }).setToken(token);
    
    (async () => {
      try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);
    
        const data = await rest.put(
          Routes.applicationGuildCommands(clientId, guildId),
          { body: commands },
        );
    
        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
      } catch (error) {
        console.error(error);
      }
    })();
    

    在config.json中补充clientId(机器人ID)和guildId(目标服务器ID),运行node deploy-commands.js完成部署。

  • 冗余前缀变量的修正(不影响斜杠命令,但建议修正)
    从config.json导入prefix的方式有误,应使用解构赋值:

    const { prefix } = require('./config.json');
    

验证步骤

  1. 添加fs模块引入,确保本地命令正常加载
  2. 编写并运行部署脚本,完成斜杠命令的服务器注册
  3. 重启机器人,尝试使用/ping命令

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 14:15:30