Discord机器人突然无响应,报错“The application did not respond”求助
Discord机器人Slash Command无响应,报错“The application did not respond”
- 此前机器人可正常运行,已成功创建并使用首个Slash Command
- 关闭命令提示符与Discord后重启,所有指令(包括/finish)均无响应,始终报错“The application did not respond”
- 已尝试操作:修改机器人读取指令的脚本、检查Discord权限、移除并重新添加机器人至服务器,未找到解决方案,无法定位问题
提供的代码
机器人主代码
const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.commands = new Collection(); const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } } } const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); for (const file of eventFiles) { const filePath = path.join(eventsPath, file); const event = require(filePath); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } client.login(token);
指令部署代码
const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('./config.json'); const fs = require('node:fs'); const path = require('node:path'); const commands = []; const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { commands.push(command.data.toJSON()); } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } } } const rest = new REST().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); } })();
排查与解决方案
1. 补全interactionCreate事件处理
你的主代码加载了events目录,但缺少处理Slash Command交互的核心事件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 }); } }, };
2. 验证指令文件的execute方法
确保commands目录下的指令文件(比如/finish对应的文件)包含正确的execute方法,且必须调用回复交互的方法,示例:
const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('finish') .setDescription('描述你的指令功能'), async execute(interaction) { // 必须回复交互,否则会触发超时报错 await interaction.reply('指令执行成功!'); }, };
3. 检查启动日志
启动机器人时查看命令提示符输出:
- 有没有
[WARNING]提示某个指令缺失data或execute属性 - 有没有token无效、网络连接失败等报错
4. 重新部署指令
确认部署代码中的clientId、guildId、token均正确,重新运行部署脚本,确保输出显示成功刷新指令。
内容的提问来源于stack exchange,提问作者Aloxenツ
相关产品推荐
相关产品推荐

