Sometimes Unknown Interaction

Hey, I made a discordjs Bot that relies heavily on slash commands, sadly sometimes it gives out just unknown interaction and crashes the bot. In this example, as soon as the command is fired, it crashed. But there are multile files/commands like this. The interaction.deffer() is just experimental
import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import { client } from '../other/client.js';
import { dbQuery } from '../other/database.js';
import { isInEventTime } from '../other/functions.js';
const embed_color = ['#4285F4', '#34A853', '#FBBC05', '#EA4335', '#0F9D58', '#551A8B', '#008080', '#800000', '#86feff', '#8de2ff', '#c3f9ff', '#00d9e6', '#00e4ff'];
const embed_footer = 'by @schlaumeyer';

const notifyCommand = new SlashCommandBuilder()
.setName('notify')
.setDescription('Pingt dich, wenn du wieder /collect ausführen kannst')
.addBooleanOption((option) =>
option
.setName('set')
.setDescription('Pingt dich, wenn du wieder /collect ausführen kannst')
.setRequired(true)
)

client.on('interactionCreate', async (interaction) => {
interaction.deferReply();

const { commandName } = interaction;

try {
if (commandName === 'notify') {
// if (await isInEventTime(interaction)) { return };

let notification = interaction.options.getBoolean('set');
let result = await dbQuery('SELECT * FROM leaderboard');
let resIndex = result.findIndex((obj => obj.userID == interaction.user.id));
import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import { client } from '../other/client.js';
import { dbQuery } from '../other/database.js';
import { isInEventTime } from '../other/functions.js';
const embed_color = ['#4285F4', '#34A853', '#FBBC05', '#EA4335', '#0F9D58', '#551A8B', '#008080', '#800000', '#86feff', '#8de2ff', '#c3f9ff', '#00d9e6', '#00e4ff'];
const embed_footer = 'by @schlaumeyer';

const notifyCommand = new SlashCommandBuilder()
.setName('notify')
.setDescription('Pingt dich, wenn du wieder /collect ausführen kannst')
.addBooleanOption((option) =>
option
.setName('set')
.setDescription('Pingt dich, wenn du wieder /collect ausführen kannst')
.setRequired(true)
)

client.on('interactionCreate', async (interaction) => {
interaction.deferReply();

const { commandName } = interaction;

try {
if (commandName === 'notify') {
// if (await isInEventTime(interaction)) { return };

let notification = interaction.options.getBoolean('set');
let result = await dbQuery('SELECT * FROM leaderboard');
let resIndex = result.findIndex((obj => obj.userID == interaction.user.id));
8 Replies
d.js toolkit
d.js toolkit12mo 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!
Schlaumeyer
SchlaumeyerOP12mo ago
result[resIndex].notification = notification;

let message_notify = " "

var embed_notify = new EmbedBuilder()
.setTitle(`Benachrichtigung`)
.setColor(embed_color[Math.floor(Math.random() * embed_color.length)])
.setDescription(message_notify)
.setFooter({ text: embed_footer })

if (notification) {
message_notify = "Ping erfolgreich **aktiviert**!";
notification = 1;
} else {
message_notify = "Ping erfolgreich **deaktiviert**!";
notification = 0;
}

embed_notify.setDescription(message_notify);

dbQuery(`UPDATE leaderboard SET notification = '${notification}' WHERE userID = ${interaction.user.id}`)

interaction.editReply({
content: " ",
embeds: [embed_notify],
ephemeral: true
})
}
} catch (err) {
console.error(err);
}
});

export default notifyCommand.toJSON();
result[resIndex].notification = notification;

let message_notify = " "

var embed_notify = new EmbedBuilder()
.setTitle(`Benachrichtigung`)
.setColor(embed_color[Math.floor(Math.random() * embed_color.length)])
.setDescription(message_notify)
.setFooter({ text: embed_footer })

if (notification) {
message_notify = "Ping erfolgreich **aktiviert**!";
notification = 1;
} else {
message_notify = "Ping erfolgreich **deaktiviert**!";
notification = 0;
}

embed_notify.setDescription(message_notify);

dbQuery(`UPDATE leaderboard SET notification = '${notification}' WHERE userID = ${interaction.user.id}`)

interaction.editReply({
content: " ",
embeds: [embed_notify],
ephemeral: true
})
}
} catch (err) {
console.error(err);
}
});

export default notifyCommand.toJSON();
In my index.js I just import notifyCommand from './commands/notify.js';
treble/luna
treble/luna12mo ago
first of all, deferReply is a promise second, why are you creating a new listener there? because thats your issue
Schlaumeyer
SchlaumeyerOP12mo ago
What should I do instead?
treble/luna
treble/luna12mo ago
use one single listener and a proper event/command handler not create a listener for every command that you run
Schlaumeyer
SchlaumeyerOP12mo ago
Do you have something like a template or sth?
d.js docs
d.js docs12mo ago
guide Creating Your Bot: Event handling read more
Schlaumeyer
SchlaumeyerOP12mo ago
thx
Want results from more Discord servers?
Add your server