Failed to load slash commands

const prefix = 'K.'; client.commands = new Collection(); const readCommands = (dir) => { const files = fs.readdirSync(dir); for (const file of files) { const filePath = path.join(dir, file); const stat = fs.statSync(filePath); if (stat.isDirectory()) { readCommands(filePath); } else if (file.endsWith('.js')) { const command = require(filePath); client.commands.set(command.data.name, command); if (command.data.slash) { client.guilds.cache.forEach((guild) => { guild.commands.create(command.data); }); } } } }; // Read all command files recursively from the 'commands' directory readCommands(path.join(__dirname, 'commands')); const commandsData = [ { name: 'info', description: 'Get information about the bot.', slash: true, }, ]; commandsData.forEach((command) => { client.guilds.cache.forEach((guild) => { guild.commands.create(command); }); }); client.on('messageCreate', async (message) => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const commandName = args.shift().toLowerCase(); const command = client.commands.get(commandName); if (!command) return; try { await command.execute(message, args); } catch (error) { console.error(error);
client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; const { commandName } = interaction; const command = client.commands.get(commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error);
23 Replies
d.js toolkit
d.js toolkit3w 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! - Marked as resolved by OP
tan
tan3w ago
+-- discord.js@14.15.3 +-- dotenv@16.4.5 `-- fs@0.0.1-security
duck
duck3w ago
putting aside the fact that I'm not sure whether your bot is even logged in when you attempt to register slash commands, that looks like an awful way of doing it anyways 1. just use global commands instead of deploying as guild commands to every guild individually 2. it's suggested that you separate command registration from your bot running since it's unnecessary to do unless you actually change your commands' data
d.js docs
d.js docs3w ago
:guide: Creating Your Bot: Registering slash commands - Command registration > Global commands read more
tan
tan3w ago
No description
treble/luna
treble/luna3w ago
you're trying to scan a file as a directory but that should be the least of your worries you're deploying your commands 1) twice 2) per guild instead of global (as mentioned above) 3) you make 1 request per command you will get ratelimited
tan
tan3w ago
No description
treble/luna
treble/luna3w ago
cool just sending screenshots without explanation doesnt help
tan
tan3w ago
It's code from guide page. File location like -- commands - info.js And this code gives the error I sent earlier
treble/luna
treble/luna3w ago
thats indeed the code you need that should be in a separate file and the code you currently have can be deleted, apart from your handling part
duck
duck3w ago
though to address the error, you'll either want to ensure your project structure matches the guide's or modify the command loading to match yours
tan
tan3w ago
Structure of code: Error as same: node:fs:1507 const result = binding.readdir( ^ Error: ENOTDIR: not a directory, scandir 'C:\Users\vlasa\Downloads\Server\commands\info.js'
No description
duck
duck3w ago
please reread my last message
tan
tan3w ago
structure matches the guide
duck
duck3w ago
it does not the guide uses subfolders for commands
tan
tan3w ago
My bad. Sorry, I create subfolder. But no logs by registration slash command. Bot started. But commands - do not
treble/luna
treble/luna3w ago
and did you run the deploy script because your bot isnt supposed to start if you run it
tan
tan3w ago
There is no information that the commands have been launched.
No description
treble/luna
treble/luna2w ago
did you save your code If so
d.js docs
d.js docs2w ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
tan
tan2w ago
i save code.
tan
tan2w ago
My code. Console didn't give any errors. And no logs for start file
No description
tan
tan2w ago
ty!
Want results from more Discord servers?
Add your server
More Posts