Registering Command

I'm working on my own discord bot for the first time and i got it to where it can go online and so i made a command and a command register script and it wont register the commands, here is my code: const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('../config.json'); const fs = require('node:fs'); const path = require('node:path'); const commands = []; const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { commands.push(command.data.toJSON()); } else { console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.); } } } const rest = new REST().setToken(token); (async () => { try { console.log(Started refreshing ${commands.length} application (/) commands.); const data = await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands }, ); console.log(Successfully reloaded ${data.length} application (/) commands.); } catch (error) { console.error(error); } })();
7 Replies
d.js toolkit
d.js toolkit2mo 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
duck2mo ago
does it log Successfully reloaded ${data.length} application (/) commands. with the correct number of commands? do your commands appear in the integrations tab of the given server's settings? have you restarted your discord client?
Reaper
ReaperOP2mo ago
when i do node handlers/command-Register.js it shows this: PS C:\Users\Kameron\OneDrive\Documents\Discord Bot> node handlers/command-Register.js node:fs:1507 const result = binding.readdir( ^ Error: ENOENT: no such file or directory, scandir 'C:\Users\Kameron\OneDrive\Documents\Discord Bot\handlers\commands' at Object.readdirSync (node:fs:1507:26) at Object.<anonymous> (C:\Users\Kameron\OneDrive\Documents\Discord Bot\handlers\command-Register.js:8:27) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12) at node:internal/main/run_main_module:28:49 { errno: -4058, code: 'ENOENT', syscall: 'scandir', path: 'C:\\Users\\Kameron\\OneDrive\\Documents\\Discord Bot\\handlers\\commands' }
Reaper
ReaperOP2mo ago
and no they do not show in the intergrations tab
No description
duck
duck2mo ago
well if the registering is erroring, it wouldn't register properly furthermore I can't verify your file paths for you does C:\Users\Kameron\OneDrive\Documents\Discord Bot\handlers\commands look correct? as in, is your commands folder in your handlers folder?
Reaper
ReaperOP2mo ago
no it is not, this is my file structure
No description
duck
duck2mo ago
then you may want to reconsider how you've defined foldersPath this is your reminder that if you want to deviate from the guide (including by project structure), you need to update the code accordingly instead of just copy-pasting
Want results from more Discord servers?
Add your server