Slash command unknown interaction without reload

I have been building a discord bot with slash commands, it is frustrating that everytime I restart my server, the discord page requires refresh/reload too to have the latest slash command May I know what is the fix to it, have been trying to reset slash commands and clear cache too, but none is working
export default async function deployGlobalCommands() {
const commands: RESTPostAPIApplicationCommandsJSONBody[] = [];
const commandCollection = new Collection<string, ApplicationCommand>();

// Clear require cache for commands
Object.keys(require.cache).forEach(key => {
if (key.includes('commands')) {
delete require.cache[key];
}
});


// Register commands
const allCommands = [StartCommand, HelpCommand];

for (const command of allCommands) {
commands.push(command.data.toJSON());
commandCollection.set(command.data.name, command);
}

const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

try {
console.log('Started refreshing application commands...');

// Clear all global commands
await rest.put(Routes.applicationCommands(DISCORD_CLIENT_ID), { body: [] });

// Clear guild-specific commands if any guilds are registered
if (client?.guilds.cache.size) {
for (const guild of client.guilds.cache.values()) {
await rest.put(
Routes.applicationGuildCommands(DISCORD_CLIENT_ID, guild.id),
{ body: [] }
);
}
}

// Deploy new global commands
await rest.put(Routes.applicationCommands(DISCORD_CLIENT_ID), {
body: commands
});

console.log('Successfully reloaded application commands.');
} catch (error) {
console.error('Error refreshing commands:', error);
throw error;
}
}
export default async function deployGlobalCommands() {
const commands: RESTPostAPIApplicationCommandsJSONBody[] = [];
const commandCollection = new Collection<string, ApplicationCommand>();

// Clear require cache for commands
Object.keys(require.cache).forEach(key => {
if (key.includes('commands')) {
delete require.cache[key];
}
});


// Register commands
const allCommands = [StartCommand, HelpCommand];

for (const command of allCommands) {
commands.push(command.data.toJSON());
commandCollection.set(command.data.name, command);
}

const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

try {
console.log('Started refreshing application commands...');

// Clear all global commands
await rest.put(Routes.applicationCommands(DISCORD_CLIENT_ID), { body: [] });

// Clear guild-specific commands if any guilds are registered
if (client?.guilds.cache.size) {
for (const guild of client.guilds.cache.values()) {
await rest.put(
Routes.applicationGuildCommands(DISCORD_CLIENT_ID, guild.id),
{ body: [] }
);
}
}

// Deploy new global commands
await rest.put(Routes.applicationCommands(DISCORD_CLIENT_ID), {
body: commands
});

console.log('Successfully reloaded application commands.');
} catch (error) {
console.error('Error refreshing commands:', error);
throw error;
}
}
No description
15 Replies
d.js toolkit
d.js toolkit3mo 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!
An Unnormal Person =)
application commands are cached by your discord client, that's why it required a reload to cache the latest commands. You have nothing to do about it Afaik, "Unknown Integration" error will try to fetch and cache the latest application commands. So you can just find and use the new command after encouraging the error without any issues
Vandyck
VandyckOP3mo ago
means that I have no other option than just reloading the page everytime so as the discord bot users?
An Unnormal Person =)
yes. If so, what makes you register the commands everytime?
Vandyck
VandyckOP3mo ago
deployment, since I am running it with express for each release, it will needa reg everytime
An Unnormal Person =)
it's a rare error on production, even there's a new update. Only small users will encourage it, but they can just reuse the commands after that this
Vandyck
VandyckOP3mo ago
for this, what's ur insights on writing the logic of the code? abit lost haha
An Unnormal Person =)
you should not register the commands every time it runs, only if you need to change the commands since you have a limited requests of commands register per day makes 2 bots, one for development, only you can use it and one for production, which has stable changes
Vandyck
VandyckOP3mo ago
so my thinking here would be I will have a scripting that works on registering slash command that run only once unless function changes and another scripting that is hosting 24/7 for dif interactionCreate, messagecCreate event? just to make sure we are in the same page I see, tat makes sense but does the user face this error if production is renewing release?
An Unnormal Person =)
yes, that should work too
Vandyck
VandyckOP3mo ago
or we could have a way to inform user the bot is in maintenance or smtg
An Unnormal Person =)
this
Vandyck
VandyckOP3mo ago
I see, alright Thanks for the insights! Appreciate that but there's smtg that could work with this, I think it would be perfect, if anyone knows any method
An Unnormal Person =)
you can rely in env variables, such as NODE_ENV. If it's production, you can just makes the register automatically Idk, it should only be in maintenance if you don't want users use it
Vandyck
VandyckOP3mo ago
👍

Did you find this page helpful?