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);
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
- 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+-- [email protected]
+-- [email protected]
`-- [email protected]
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
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
cool
just sending screenshots without explanation doesnt help
It's code from guide page. File location like
-- commands
- info.js
And this code gives the error I sent earlier
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
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
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'
please reread my last message
structure matches the guide
it does not
the guide uses subfolders for commands
My bad. Sorry, I create subfolder. But no logs by registration slash command. Bot started. But commands - do not
and did you run the deploy script
because your bot isnt supposed to start if you run it
There is no information that the commands have been launched.
did you save your code
If so
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 morei save code.
My code. Console didn't give any errors. And no logs for start file
ty!