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

如何解决Discord.js v14斜杠命令正常运行却提示“应用未响应”

解决Discord斜杠命令“应用未响应”提示问题

你的问题核心是Discord要求斜杠命令必须在3秒内通过交互对象本身回复,而你当前用的interaction.channel.send()只是在频道发消息,没有告知Discord机器人已经收到并处理了这个命令,所以平台会判定“应用未响应”。

具体修复步骤:

  • 在命令执行开头调用interaction.deferReply(),告诉Discord机器人正在处理请求,暂时不返回结果,避免超时提示。
  • 把所有后续的interaction.channel.send()替换为interaction.followUp(),这是延迟回复后的标准后续消息发送方式。
  • 用async/await替代.then().catch()嵌套写法,让代码更易维护,同时确保错误处理也通过交互对象完成。

修改后的完整代码:

const Discord = require('discord.js');
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('totalfrozencheckouts')
    .setDescription('Add up every message in the frozen checkouts channel after a specific message ID')
    .addStringOption(option =>
      option.setName('messageid')
        .setDescription('The message ID')
        .setRequired(true)),
  async execute(interaction) {
    // 延迟回复,避免Discord判定未响应
    await interaction.deferReply();
    
    const channel = '<#' + process.env.FROZENCHECKOUTS + '>';
    const messageId = interaction.options.getString("messageid");
    
    // 检查频道格式
    if (!channel.startsWith('<#') || !channel.endsWith('>')) {
      return await interaction.followUp(`Invalid channel mention. Please use the format: ${this.usage}`);
    }
    
    const channelId = channel.slice(2, -1);
    
    try {
      // 使用await替代.then,代码逻辑更清晰
      const messages = await interaction.guild.channels.cache.get(channelId).messages.fetch({ after: messageId });
      const numbers = messages.map(message => message.content).filter(content => !isNaN(content));
      
      if (numbers.length === 0) {
        return await interaction.followUp(`No messages were found in ${channel} after message ID https://discord.com/channels/1059607354678726666/1060019655663689770/${messageId}`);
      }
      
      // 修正reduce初始值,正确统计消息数量
      const sum = numbers.reduce((accumulator) => accumulator + 1, 0);

      const embed = new EmbedBuilder()
        .setColor(0x4bd8c1)
        .setTitle(`Total Checkouts in #frozen-checkouts for today is:`)
        .addFields(
          {name: 'Total Checkouts', value: sum.toString() , inline: true},
        )
        .setThumbnail('https://i.imgur.com/7cmn8uY.png')
        .setTimestamp()
        .setFooter({ text: 'Frozen ACO', iconURL: 'https://i.imgur.com/7cmn8uY.png' });

      await interaction.followUp({embeds: [embed]});
    } catch (error) {
      console.error(error);
      await interaction.followUp('An error occurred while trying to fetch the messages. Please try again later.');
    }
  }
}

注:你原来的reduce初始值设为1会导致计数多1个,改成0才是正确的消息数量统计逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:55:45