Javascript Discord Bot无法响应交互请求的问题求助
Discord.js 斜杠命令无响应,控制台报错
ReferenceError: interaction is not defined 问题详情
Bot可正常登录,但触发斜杠命令时无响应,控制台抛出错误:ReferenceError: interaction is not defined at Client.handleInteraction (file:///C:/Program%20Files/discord-bot/bot.js:18:5)
.env文件已正确配置SERVERID、CLIENTID和TOKEN,命令部署与Bot登录状态正常,使用/sticks命令时提示"应用未响应"。
相关代码文件
bot.js
import { Client, Events, GatewayIntentBits } from 'discord.js'; import { config } from 'dotenv'; import * as bos from './commands/sticks.js'; config(); const client = new Client({ intents: [GatewayIntentBits.Guilds], }); function readyDiscord() { console.log("Hello " + client.user.tag); } async function handleInteraction() { if (!interaction.isCommand()) return; if (interaction.commandName === 'sticks') { await bos.execute(interaction); } } client.once(Events.ClientReady, readyDiscord); client.login(process.env.TOKEN); client.on(Events.InteractionCreate, handleInteraction);
commands/sticks.js
import { SlashCommandBuilder } from 'discord.js'; // Command Builder export export const data = new SlashCommandBuilder() .setName('sticks') .setDescription('Fun bundle of sticks!'); // Execute function export export async function execute(interaction) { await interaction.reply('Sticks'); }
部署命令文件
// Importing modules using ES6 syntax import { REST, Routes } from 'discord.js'; import { config } from 'dotenv'; import fs from 'node:fs'; config(); // Using dotenv config function directly const commands = []; const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js')); // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment for (const file of commandFiles) { const command = await import(`./commands/${file}`); // Using dynamic import if ('data' in command && 'execute' in command) { commands.push(command.data.toJSON()); } else { console.log(`[WARNING] The command ${file} is missing a required "data" or "execute" property.`); } } // Construct and prepare an instance of the REST module const rest = new REST().setToken(process.env.TOKEN); // and deploy your commands! (async () => { try { console.log(`Started refreshing ${commands.length} application (/) commands.`); // The put method is used to fully refresh all commands in the guild with the current set const data = await rest.put(Routes.applicationGuildCommands(process.env.CLIENTID, process.env.SERVERID), { body: commands, }); console.log(`Successfully reloaded ${data.length} application (/) commands.`); } catch (error) { // And of course, make sure you catch and log any errors! console.error(error); } })();
解决方案
核心问题
handleInteraction函数未接收InteractionCreate事件触发时传入的interaction参数,导致函数内引用interaction变量时出现未定义错误。
修复代码
修改bot.js中的handleInteraction函数,添加interaction参数:
async function handleInteraction(interaction) { // 添加interaction参数 if (!interaction.isCommand()) return; if (interaction.commandName === 'sticks') { await bos.execute(interaction); } }
验证步骤
- 重启Bot进程
- 再次发送
/sticks命令,此时Bot应正常回复"Sticks"
内容的提问来源于stack exchange,提问作者Leviticus 2629
相关产品推荐
相关产品推荐

