SlashCommand interaction is undefined

Hey, I just encountered an Error I can't explain... I get an error every time I execute an SlashCommand from my Bot telling me "interaction" is undefined. I haven't changed anything in my App for a couple of weeks, so I can't tell when this error started to occur. Things I've checked: - if discord.js has a newer version that i can install - already had the latest version 14.16.3 - I republished my SlashCommands to Discord - still not working This is an example breakdown of a command in my Application:
module.exports = {
data: new SlashCommandBuilder()
.setName('joke')
.setDescription('Tells a Joke!'),
async execute(interaction: CommandInteraction) {
await interaction.deferReply({ ephemeral: false }) // this line already throws the error
// here is just some code to response
}
};
module.exports = {
data: new SlashCommandBuilder()
.setName('joke')
.setDescription('Tells a Joke!'),
async execute(interaction: CommandInteraction) {
await interaction.deferReply({ ephemeral: false }) // this line already throws the error
// here is just some code to response
}
};
Here is the Stacktrace (I edited the path to make it more readable)
TypeError: Cannot read properties of undefined (reading 'deferReply')
at Object.execute (/path/to/bot/dist/discord/commands/community/joke.js:13:27)
at Object.execute (/path/to/bot/dist/discord/events/interactionCreate.js:15:27)
at Client.<anonymous> (/path/to/bot/dist/discord/DiscordClient.js:105:56)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (/path/to/bot/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/path/to/bot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/path/to/bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:348:31)
at WebSocketManager.<anonymous> (/path/to/bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:232:12)
at WebSocketManager.emit (/path/to/bot/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:287:31)
at WebSocketShard.<anonymous> (/path/to/bot/node_modules/@discordjs/ws/dist/index.js:1190:51)
TypeError: Cannot read properties of undefined (reading 'deferReply')
at Object.execute (/path/to/bot/dist/discord/commands/community/joke.js:13:27)
at Object.execute (/path/to/bot/dist/discord/events/interactionCreate.js:15:27)
at Client.<anonymous> (/path/to/bot/dist/discord/DiscordClient.js:105:56)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (/path/to/bot/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/path/to/bot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/path/to/bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:348:31)
at WebSocketManager.<anonymous> (/path/to/bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:232:12)
at WebSocketManager.emit (/path/to/bot/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:287:31)
at WebSocketShard.<anonymous> (/path/to/bot/node_modules/@discordjs/ws/dist/index.js:1190:51)
npm list: [email protected] node -v: v20.16.0 If you need more information just let me know ...
11 Replies
d.js toolkit
d.js toolkit2d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Dopi
DopiOP2d ago
Sure, I just found something there that could cause the issue, I'll check that first 👍
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Dopi
DopiOP2d ago
Okay, that was not the Issue ... interactionCreate.ts
module.exports = {
name: Events.InteractionCreate,
async execute(interaction: Interaction) {
if (!interaction.isChatInputCommand()) return;
// SmeetaClient#commands is typed the following:
// public commands: Collection<string, ExportedSlashCommand>;
// I checked and can confirm the Collection is filled as it should be
const command = (interaction.client as SmeetaClient).commands.get(interaction.commandName);

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute();
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
},
};
module.exports = {
name: Events.InteractionCreate,
async execute(interaction: Interaction) {
if (!interaction.isChatInputCommand()) return;
// SmeetaClient#commands is typed the following:
// public commands: Collection<string, ExportedSlashCommand>;
// I checked and can confirm the Collection is filled as it should be
const command = (interaction.client as SmeetaClient).commands.get(interaction.commandName);

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute();
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
},
};
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Dopi
DopiOP2d ago
Ohh whoops you are right ... I'm wondering how this was working before 😅
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2d ago
We highly recommend you extend the Client structure properly instead of just attaching custom properties like .commands to the regular discord.js Client instance. - Using typescript, you might want to consider casting or augmenting the module type
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Dopi
DopiOP2d ago
Yeah that was the issue 😄 the typing of the execute function was wrong thats why I haven't noticed the issue there It's working now, thanks for your quick help! ❤️
Want results from more Discord servers?
Add your server