Node.js开发Discord机器人斜杠命令报错:应用未响应
问题根源与修复方案
核心问题分析
- 未定义变量引发报错:按钮点击事件中使用的
application变量未声明,执行时会抛出ReferenceError,直接中断交互响应流程,触发Discord的“应用未响应”提示。 - 事件监听拆分导致逻辑混乱:两个独立的
interactionCreate监听可能导致交互被重复处理或部分逻辑失效。 - 未实现输入界面需求:当前代码仅展示按钮,缺少用户输入文本的交互组件(需用Discord Modal实现)。
- 异步操作未正确等待:
webhook.send未添加await,可能引发未处理的Promise拒绝,中断后续回复逻辑。
修复后的完整代码
// Configs const token = ''; const clientId = ''; const guildId = ''; const webhookToken = ''; const webhookId = ''; // Imports import Discord from 'discord.js'; import { REST } from '@discordjs/rest'; import { Routes } from 'discord-api-types/v9'; const { Client, MessageActionRow, Modal, TextInputComponent } = Discord; const client = new Client({ intents: 32767 }); // Login client.login(token) .then(() => console.log('Bot logged in successfully!')); process.on('unhandledRejection', error => console.error('Unhandled promise rejection:', error)); // 注册斜杠命令 const commands = [{ name: 'apply', description: 'Start your application!', }]; const rest = new REST({ version: '9' }).setToken(token); (async () => { try { await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands }, ); console.log('Successfully reloaded application (/) commands.'); } catch (error) { console.error(error); } })(); // 合并所有交互处理逻辑 client.on('interactionCreate', async interaction => { // 处理斜杠命令 if (interaction.isCommand()) { if (interaction.commandName === 'apply') { // 创建输入Modal const modal = new Modal() .setCustomId('application_modal') .setTitle('Submit Your Application'); // 添加输入框组件 const applicationInput = new TextInputComponent() .setCustomId('application_content') .setLabel('Please enter your application content') .setStyle('PARAGRAPH') // 多行输入 .setRequired(true); // 将输入框放入行组件 const row = new MessageActionRow().addComponents(applicationInput); modal.addComponents(row); // 弹出Modal供用户输入 await interaction.showModal(modal); } return; } // 处理Modal提交 if (interaction.isModalSubmit()) { if (interaction.customId === 'application_modal') { // 获取用户输入的内容 const applicationContent = interaction.fields.getTextInputValue('application_content'); // 先延迟回复,避免超时 await interaction.deferReply({ ephemeral: true }); try { // 通过Webhook发送内容 const webhook = new Discord.WebhookClient({ id: webhookId, token: webhookToken }); await webhook.send({ content: 'New application received:', embeds: [{ description: applicationContent, color: 0x00ff00 }] }); // 回复用户提交成功 await interaction.editReply({ content: 'Your application has been submitted!', ephemeral: true }); } catch (error) { console.error('Failed to send webhook:', error); await interaction.editReply({ content: 'Failed to submit your application, please try again later.', ephemeral: true }); } } return; } });
关键修改说明
- 添加Modal实现输入界面:用
Modal和TextInputComponent创建用户输入表单,满足需求中的“弹出输入界面”功能。 - 合并交互处理器:将命令、Modal的处理逻辑整合到一个
interactionCreate监听中,避免逻辑冲突。 - 修复未定义变量:通过获取Modal输入值来生成要发送的内容,替代原代码中未定义的
application变量。 - 异步操作添加await:对
webhook.send添加await,确保操作完成后再回复用户,同时用deferReply避免交互超时。 - 错误捕获:添加try/catch捕获Webhook发送失败的情况,给用户明确的错误提示。
内容的提问来源于stack exchange,提问作者Monder
相关产品推荐
相关产品推荐

