Cannot register commands globally

I am trying to register my slash commands to every server that my bot is in, but for some reason I can't. This code worked before it suddenly decided not working. I don't get any errors, it just halts the execution like it is still waiting for the response. My bot is in only two servers at the moment, I have tried kicking it from both of them and inviting back with application.commands and bot permissions. I have also tried using client.application.commands.set() instead of rest.put(). Here is a part of my code:
try {
await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: [],
});

const allGuildIds = await client.guilds.cache.map((guild) => guild.id);

await allGuildIds.forEach((guildId) => {
rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId),
{
body: [],
}
);
});

console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

// THIS WORKS
const data = await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID,
process.env.GUILD_ID
),
{
body: commands,
}
);

// THIS DOES NOT WORK
// const data = await rest.put(
// Routes.applicationCommands(process.env.CLIENT_ID),
// {
// body: commands,
// }
// );

console.log(
`βœ… Successfully refreshed ${data.length} application commands!`
);
} catch (err) {
console.log(`There was an error: ${err}`);
}
try {
await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: [],
});

const allGuildIds = await client.guilds.cache.map((guild) => guild.id);

await allGuildIds.forEach((guildId) => {
rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId),
{
body: [],
}
);
});

console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

// THIS WORKS
const data = await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID,
process.env.GUILD_ID
),
{
body: commands,
}
);

// THIS DOES NOT WORK
// const data = await rest.put(
// Routes.applicationCommands(process.env.CLIENT_ID),
// {
// body: commands,
// }
// );

console.log(
`βœ… Successfully refreshed ${data.length} application commands!`
);
} catch (err) {
console.log(`There was an error: ${err}`);
}
Also, if there is a better way to clear the application commands from every guild before adding the new ones, please let me know.
7 Replies
d.js toolkit
d.js toolkitβ€’8mo 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!
duck
duckβ€’8mo ago
Routes.applicationCommands() is the route you would use for global commands
it just halts the execution like it is still waiting for the response
that sounds like a rate limit, which given the fact that you're making several unnecessary api calls prior to your commented code, this seems probable - deploying guild commands to each guild individually is not the same as deploying global commands - it's not necessary to clear commands by deploying an empty array before deploying commands bulk overwriting commands (with a PUT request) already removes any command that isn't in the array you deployed - this also means that it's not necessary to clear each guild's guild commands by deploying empty arrays to each
IAmBatman
IAmBatmanOPβ€’8mo ago
The reason I am deploying an empty array was because I had duplicates of every command previously, so I wanted to clear everything in every guild before deploying. Maybe clearing global application commands is not needed though.
that sounds like a rate limit, which given the fact that you're making several unnecessary api calls prior to your commented code, this seems probable
Even when I delete the unnecessary api calls, Routes.applicationCommands() still doesn't work. Routes.applicationGuildCommands() (which I am using for a single guild) works with or without the unnecessary api calls. Not sure if the problem is rate limit
duck
duckβ€’8mo ago
The reason I am deploying an empty array was because I had duplicates of every command previously, so I wanted to clear everything in every guild before deploying. Maybe clearing global application commands is not needed though.
this would be if you had both global and guild commands of the same commands clearing them is something you would only need to do once and only for the type that you won't be using (guild commands in this case)
Even when I delete the unnecessary api calls, Routes.applicationCommands() still doesn't work. Routes.applicationGuildCommands() (which I am using for a single guild) works with or without the unnecessary api calls. Not sure if the problem is rate limit
returning to execution hanging, care to share your updated code? and on which line does it hang? is it that you've confirmed it hangs because Successfully refreshed ${data.length} application commands! doesn't log?
IAmBatman
IAmBatmanOPβ€’8mo ago
The code that works:
try {

console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

const data = await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID,
process.env.GUILD_ID
),
{
body: commands,
}
);

console.log(`βœ… Successfully refreshed ${data.length} application commands!`);
} catch (err) {
console.log(`There was an error: ${err}`);
}
try {

console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

const data = await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID,
process.env.GUILD_ID
),
{
body: commands,
}
);

console.log(`βœ… Successfully refreshed ${data.length} application commands!`);
} catch (err) {
console.log(`There was an error: ${err}`);
}
The code that doesn't work:
try {
console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

const data = await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{
body: commands,
}
);

console.log(`βœ… Successfully refreshed ${data.length} application commands!`);
} catch (err) {
console.log(`There was an error: ${err}`);
}
try {
console.log(
`πŸ”„ Started refreshing ${commands.length} application commands...`
);

const data = await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{
body: commands,
}
);

console.log(`βœ… Successfully refreshed ${data.length} application commands!`);
} catch (err) {
console.log(`There was an error: ${err}`);
}
and on which line does it hang? is it that you've confirmed it hangs because Successfully refreshed ${data.length} application commands! doesn't log?
Yes, it hangs right where I wait for rest.put(Routes.applicationGuildCommands(...)) Ah that's probably the issue, you are right. I wasn't aware of this limit. One question tho. Does refreshing an already existing command count towards this limit, or do only newly registered commands count? If everything counts, I guess I need check if a command already exists in the application commands, then register if it doesn't. Right?
duck
duckβ€’8mo ago
Updating an existing command does not count towards the command creation limit. Deleting and recreating is what would've caused this
IAmBatman
IAmBatmanOPβ€’8mo ago
Okay, that’s good to know. Thank you both for all the help
Want results from more Discord servers?
Add your server