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

求助:Discord Bot的info斜杠命令无响应且控制台无报错

问题解决及优化建议

核心问题分析及修复

1. getSubcommand方法调用错误

在info.js的execute函数中,你错误地将getSubcommand当作属性使用,而它实际上是一个需要调用的方法。这导致所有子命令判断都无法匹配,机器人自然不会做出响应。

修改后的info.js执行部分:

async execute(interaction){
    // 调用getSubcommand()方法获取子命令名称
    if(interaction.options.getSubcommand() === "user"){
        const user = interaction.options.getUser('member');

        if (user){
            await interaction.reply(`Username: ${user.username}\nID: ${user.id}`);
        } else {
            await interaction.reply(`Your username: ${interaction.user.username}\nID: ${interaction.user.id}`);
        }

    } else if (interaction.options.getSubcommand() === 'server'){
        await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`)
    }
},

2. 缺少interactionCreate事件监听

你的index.js加载了events目录,但如果没有监听InteractionCreate事件的代码,机器人无法接收并处理斜杠命令的交互请求。

在events目录下创建interactionCreate.js文件,添加以下代码:

const { Events } = require('discord.js');

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) return;

        const command = interaction.client.commands.get(interaction.commandName);

        if (!command) {
            console.error(`No command matching ${interaction.commandName} was found.`);
            return;
        }

        try {
            await command.execute(interaction);
        } catch (error) {
            console.error(error);
            await interaction.reply({ content: '执行此命令时出错!', ephemeral: true });
        }
    },
};

额外检查项

  • 确认命令已正确部署到Discord服务器:如果是新增或修改了命令,需要运行部署脚本将命令同步到Discord的命令列表中。
  • 确认机器人权限:确保机器人在服务器中有发送消息的权限,否则即使代码正确也无法回复。

发帖方式反馈

  • 代码格式化:Stack Overflow中使用三个反引号(javascript 代码内容 )包裹代码块是最便捷的方式,比pre标签更适合平台规则。
  • 问题补充:可以额外说明是否已部署命令、机器人是否具备必要权限,这些信息能帮助更快定位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 23:40:26