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

Discord.js全局命令报错‘应用未响应’求助

问题原因与修复方案

核心问题1:命令方法名不匹配

你的命令模块(ping.js)导出的执行方法是execute,但在handleCommands.js里调用的是commandObject.callback,这会导致命令逻辑根本没被执行,Discord因为在3秒内没收到任何回复,就会显示「应用未响应」。

核心问题2:Ping命令逻辑错误

在ping.js的execute方法里,你先调用了interaction.fetchReply(),但此时你还没有发送任何回复,这个方法会抛出错误,直接导致命令执行失败,进一步触发超时提示。


修复后的代码

1. 修正ping.js

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Krijg de bot ping en Websocket ping.'),
  
    async execute(client, interaction) {
        // 先发送延迟回复,避免超时
        await interaction.deferReply();
        
        // 获取延迟回复的消息来计算ping
        const reply = await interaction.fetchReply();
        const ping = reply.createdTimestamp - interaction.createdTimestamp;

        // 编辑延迟回复,替换成最终内容
        await interaction.editReply(`Pong! Bot: ${ping}ms | Websocket: ${client.ws.ping}ms.`);
    },
};

2. 修正handleCommands.js

把调用commandObject.callback的地方改成commandObject.execute:

const { devs, testServer } = require('../../../config.json');
const getLocalCommands = require('../../utils/getLocalCommands');

module.exports = async (client, interaction) => {
  if (!interaction.isChatInputCommand()) return;

  const localCommands = getLocalCommands();

  try {
    const commandObject = localCommands.find(
      (cmd) => cmd.name === interaction.commandName
    );

    if (!commandObject) return;

    if (commandObject.devOnly) {
      if (!devs.includes(interaction.member.id)) {
        interaction.reply({
          content: 'Alleen bestuur mag deze command uitvoeren.',
          ephemeral: true,
        });
        return;
      }
    }

    if (commandObject.testOnly) {
      if (!(interaction.guild.id === testServer)) {
        interaction.reply({
          content: 'Deze command is alleen in testmodus beschikbaar.',
          ephemeral: true,
        });
        return;
      }
    }

    if (commandObject.permissionsRequired?.length) {
      for (const permission of commandObject.permissionsRequired) {
        if (!interaction.member.permissions.has(permission)) {
          interaction.reply({
            content: 'U hebt hier niet de geschikte permissies voor.',
            ephemeral: true,
          });
          return;
        }
      }
    }

    if (commandObject.botPermissions?.length) {
      for (const permission of commandObject.botPermissions) {
        const bot = interaction.guild.members.me;

        if (!bot.permissions.has(permission)) {
          interaction.reply({
            content: "Ik heb hier geen permissie voor. (geef me 'Administrator' rechten.)",
            ephemeral: true,
          });
          return;
        }
      }
    }

    // 修正方法名,匹配命令模块的导出方法
    await commandObject.execute(client, interaction);
  } catch (error) {
    console.log(`[FOXILITY] There was an error running this command: ${error}`);
    // 新增错误回复,避免用户一直看到超时状态
    if (!interaction.replied && !interaction.deferred) {
      await interaction.reply({
        content: 'Er is een fout opgetreden bij het uitvoeren van deze opdracht.',
        ephemeral: true
      });
    }
  }
};

额外说明

  • 全局命令的生效时间可能需要1小时左右,如果刚更新完命令,先等待一段时间再测试。
  • 加上interaction.deferReply()是为了告诉Discord「我正在处理,稍后回复」,规避3秒超时限制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:07:40