I've recently followed a slash command tutorial from discordjs guide. It all works, but the issue is that I am unable to have the slash commands in more than 1 server, as I have manually put the guild ID in, and cannot find a way to get the guild ID when the bot is ready. Here's the code:
const clientID = '942177936553959464';
const guildID = '936781831037157426';
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientID, guildID), // Error is at guildID, as it is not getting the actual guilds ID, just the one I set for testing.
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
The way you register is a guild-specific way of registering slash commands. There are 2 routes you can take. First - Have an array of guild IDs and loop over them and register it per guild. Something like this would work fine
const clientID = "942177936553959464";
const guilds = ["id1", "id2", "id3"];
guilds.forEach((guildID) => {
// Register the slash commands here
});
Second - Register the slash commands globally(For all guilds). Do note global commands take some time to be cached and made available by the discord API.
https://discordjs.guide/interactions/registering-slash-commands.html#global-commands
Note - The First approach should be followed while the bot is in the development stage and not in production