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;
    }
}
Screenshot_2025-02-19_at_5.36.27_PM.png
Was this page helpful?