I’m new to making Discord bots with JavaScript and I’m stuck. My bot logs in fine but it’s not responding to slash commands. When I use the command, I get this error:
ReferenceError: interaction is not defined at Client.handleInteraction
Here’s my main bot file main.js
:
import { Client, GatewayIntentBits } from 'discord.js';
import { config } from 'dotenv';
import * as commandModule from './commands/poke.js';
config();
const bot = new Client({
intents: [GatewayIntentBits.Guilds],
});
function botReady() {
console.log(`${bot.user.tag} is online!`);
}
async function handleCommand(interaction) {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'poke') {
await commandModule.run(interaction);
}
}
bot.once('ready', botReady);
bot.on('interactionCreate', handleCommand);
bot.login(process.env.BOT_TOKEN);
And here’s my command file poke.js
:
import { SlashCommandBuilder } from 'discord.js';
export const info = new SlashCommandBuilder()
.setName('poke')
.setDescription('Poke someone!');
export async function run(interaction) {
await interaction.reply('You poked someone!');
}
I’ve set up my .env
file with the right IDs and token. I’ve also deployed the commands. What am I missing? The bot should respond to /poke
but instead it says ‘The application did not respond’. Help!
hey there! looks like ur missing the interaction parameter in ur handleCommand function. try changing it to:
async function handleCommand(interaction) {
if (!interaction.isCommand()) return;
// rest of ur code
}
that should fix the error. lmk if u need more help!
I’ve faced similar issues when starting with Discord bots. One thing that often gets overlooked is properly handling the interaction. Make sure you’re awaiting the interaction in your main file. Try modifying your handleCommand
function like this:
async function handleCommand(interaction) {
if (!interaction.isCommand()) return;
try {
if (interaction.commandName === 'poke') {
await commandModule.run(interaction);
}
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
This way, you’re properly handling the Promise returned by the run
function and catching any errors. Also, double-check that your bot has the necessary permissions in the server. Sometimes, that can cause silent failures. If it still doesn’t work, try adding some console.logs in your handleCommand
function to see if it’s being triggered at all.
I noticed you’re using ES modules in your project. Make sure your package.json
includes “type”: “module” to enable ES module support. Also, double-check that you’ve registered your slash commands with Discord’s API. This step is separate from deploying your bot and is crucial for slash commands to work. You might need to use the REST API to register commands, especially if you’re testing in a specific guild. If you’ve done all this and it’s still not working, try adding some debug logging in your handleCommand
function to see if it’s being called at all when you use the slash command.