Register Global Command

Hi everyone, this code takes care of recording all commands globally. However, currently it doesn't work, in the console it only prints the message of the start of recording the commands but never the completion message without even giving an error, in fact the commands are not visible. I am 100% sure that the code is correct since by changing the application, i.e. by changing the token and the client id, it works correctly and records all the commands. I'm also sure of it from the fact that it also worked in this same application that is now giving me this problem. But then how is it possible that it suddenly doesn't work anymore? The problem started when I created a command that reloaded the commands, as when starting the bot, using the exact same script and running it a couple of times I realized that the code that was supposed to load them at startup was no longer working. Code:
51 Replies
d.js toolkit
d.js toolkit5mo 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!
Velenosoh
Velenosoh5mo ago
const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); const path = require('path'); const commandFolders = [ path.join(dirname, 'commands', 'general', 'economy'), path.join(dirname, 'commands', 'general', 'fun'), path.join(dirname, 'commands', 'general', 'info'), path.join(dirname, 'commands', 'general', 'level'), path.join(dirname, 'commands', 'general', 'music'), path.join(dirname, 'commands', 'general', 'utility'), path.join(dirname, 'commands', 'general', 'message'), path.join(dirname, 'commands', 'general', 'partner'), path.join(dirname, 'commands', 'general', 'invite'), path.join(dirname, 'commands', 'staff', 'admin'), path.join(dirname, 'commands', 'staff', 'giveaway'), path.join(dirname, 'commands', 'staff', 'mod'), path.join(__dirname, 'commands', 'staff', 'setup'), // Aggiungi qui i percorsi delle altre cartelle dei comandi ]; const clientId = '1142069838307663952'; const token = config.token; const rest = new REST({ version: '9' }).setToken(token); function readCommands(folder) { const commands = []; if (fs.existsSync(folder)) { const items = fs.readdirSync(folder); items.forEach(item => { const itemPath = path.join(folder, item); const isDirectory = fs.statSync(itemPath).isDirectory(); if (isDirectory) { // Se è una directory, leggi i comandi all'interno di essa commands.push(...readCommands(itemPath)); } else if (item.endsWith('.js')) { // Se è un file .js, richiama il comando e aggiungilo all'array dei comandi const command = require(itemPath); // Verifica se il tipo di opzione specificato per il comando è uno dei valori consentiti const commandData = command.data; const commandOptions = commandData.options || []; const allowedOptions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // Sostituisci con i valori consentiti per il tuo comando commandOptions.forEach(option => { if (!allowedOptions.includes(option.type)) { console.error(Il comando ${commandData.name} ha un tipo di opzione non valido: ${option.type}); } }); commands.push(command.data); // Usiamo solo le proprietà necessarie } }); } else { console.error(La directory ${folder} non esiste o non è accessibile.); } return commands; } const globalCommands = []; // Leggi i comandi da tutte le cartelle dei comandi commandFolders.forEach(folder => { globalCommands.push(...readCommands(folder)); }); (async () => { try { console.log('Inizio la registrazione dei comandi globali ⌛'); await rest.put( Routes.applicationCommands(clientId), { body: globalCommands } ); console.log(Comandi globali registrati con successo ✅); } catch (error) { console.error('Errore durante la registrazione dei comandi globali:', error); } })(); module.exports = globalCommands; As you asked I made sure to run it in a script separate from the main file so I created a commandLoader.js file that contains this code and in the main file I called the loadCommands function recovering the file client.once('ready', () => { console.log("Zenith Bot ON🤖"); // Imposta l'attività iniziale setNextActivity(); // Intervallo per cambiare l'attività ogni 2 secondi setInterval(setNextActivity, 2000); const loadCommands = require("./commandLoader") loadCommands()
}); So isn’t it? this is the file commandLoader + this https://srcb.in/tZPdD7Cecy the command that i executed before the problem https://srcb.in/PtnPhlCKyC commandLoader.js https://srcb.in/gvltoO3zS6 index.js (the part where i call the function for load the commands)
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Velenosoh
Velenosoh5mo ago
ok so, is right if i load the command with a message? https://srcb.in/MVTtQf9BTv
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Velenosoh
Velenosoh5mo ago
I know, I know, for the moment it's like this, after I've fixed it, I'll change the word, maybe with a code or something safe
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Velenosoh
Velenosoh5mo ago
Okay, I'll do it but for now I'd like to focus on getting my commands back available again
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs5mo ago
Suggestion for @Jordy: :guide: Creating Your Bot: Registering slash commands The command deployment script, to register your slash commands with Discord so they appear in the interface. read more
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Velenosoh
Velenosoh5mo ago
Yes, but how would making a command more secure by making it usable only for me help get my commands back?? I did what you asked, I created a separate script (commandLoader.js) and instead of invoking it every time the bot starts it only runs if I write the message
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Velenosoh
Velenosoh5mo ago
so for example node commandLoader.js ?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server