My Discord bot does stop at deploying commands to the specific guild. It did work with the same code, at the same place, on the same guild, in the same terminal 3 min ago, and got stuck on deploying now. I got this problem today morning once, and it fixed itself after many hours my mac was disabled. Does someone got an idea what the problem is?
My deploy.js:
const { REST, Routes } = require('discord.js');
const { GlobalModules } = require('../config/modules');
const Guild = require('../models/Guild');
require('dotenv').config();
let clientCommands = null;
function setClientCommands(commands) {
clientCommands = commands;
}
async function clearCommands(guildId) {
const rest = new REST().setToken(process.env.TOKEN);
try {
console.log(`Lösche Commands für Guild ${guildId}...`);
// Prüfe globale Commands
console.log('Prüfe globale Commands...');
const globalCommands = await rest.get(
Routes.applicationCommands(process.env.CLIENT_ID)
);
console.log(
`Gefundene globale Commands: ${JSON.stringify(globalCommands, null, 2)}`
);
// Lösche globale Commands
for (const command of globalCommands) {
console.log(`Lösche globalen Command ${command.name} (${command.id})...`);
await rest.delete(
Routes.applicationCommand(process.env.CLIENT_ID, command.id)
);
}
// Prüfe guild-spezifische Commands
console.log('Prüfe guild-spezifische Commands...');
const guildCommands = await rest.get(
Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId)
);
console.log(
`Gefundene Guild Commands: ${JSON.stringify(guildCommands, null, 2)}`
);
// Lösche guild-spezifische Commands
for (const command of guildCommands) {
console.log(`Lösche Guild Command ${command.name} (${command.id})...`);
await rest.delete(
Routes.applicationGuildCommand(
process.env.CLIENT_ID,
guildId,
command.id
)
);
}
console.log('Erfolgreich alle Commands gelöscht.');
} catch (error) {
console.error('Fehler beim Löschen der Commands:', error);
throw error;
}
}
async function deployCommandsForGuild(guildId) {
if (!clientCommands) {
console.error('Client commands wurden nicht initialisiert');
return;
}
const rest = new REST().setToken(process.env.TOKEN);
let guild = await Guild.findOne({ guildId });
// Wenn keine Konfiguration existiert, erstelle eine neue
if (!guild) {
try {
// Hole Guild-Informationen von Discord
const discordGuild = await rest.get(Routes.guild(guildId));
console.log(
`Erstelle neue Guild-Konfiguration für ${guildId} (${discordGuild.name})...`
);
guild = new Guild({
guildId,
name: discordGuild.name,
enabledModules: [],
availableModules: [],
});
await guild.save();
} catch (error) {
console.error(`Fehler beim Erstellen der Guild-Konfiguration: ${error}`);
return;
}
}
try {
// Erst alle Commands löschen
await clearCommands(guildId);
// Sammle alle Commands die deployed werden sollen
const commandsToRegister = [];
for (const [name, command] of clientCommands) {
// Wenn es ein globales Modul ist oder wenn das Modul aktiviert ist
if (
!command.module ||
GlobalModules.includes(command.module) ||
guild.enabledModules.includes(command.module)
) {
commandsToRegister.push(command.data.toJSON());
}
}
console.log(
`Starte Deployment von ${commandsToRegister.length} Commands für Guild ${guildId}...`
);
// Registriere die neuen Commands
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId),
{ body: commandsToRegister }
);
console.log(
`Erfolgreich ${commandsToRegister.length} Commands für Guild ${guildId} deployed.`
);
} catch (error) {
console.error('Fehler beim Deployen der Commands:', error);
}
}
module.exports = {
setClientCommands,
deployCommandsForGuild,
clearCommands,
};
And the output:
✅ Erfolgreich mit MongoDB verbunden
🎉 Giveaway-System wird gestartet...
✅ Giveaway-System erfolgreich gestartet
Bereit! Eingeloggt als BayMax Dev#7945
Lösche Commands für Guild 1325228698642681928...
Prüfe globale Commands...
🕒 Reminder-System gestartet
Gefundene globale Commands: []
Prüfe guild-spezifische Commands...
Gefundene Guild Commands: []
Erfolgreich alle Commands gelöscht.
Lösche Commands für Guild 1325228698642681928...
Prüfe globale Commands...
Gefundene globale Commands: []
Prüfe guild-spezifische Commands...
Gefundene Guild Commands: []
Erfolgreich alle Commands gelöscht.
Starte Deployment von 2 Commands für Guild 1325228698642681928...
Discord has harsh rate-limits in place at updating/modifying/removing existing application commands. Since you loop through every command and send one api request per command you get easily rate-limited very fast.
To fix this behaviour update your code as follows:
Do not clear old commands, you can just override them and the old ones will be gone.
If you want to clear all commands you can either collect them all in an array before sending the api request, or - and this is the better behaviour - just put an empty array.