Error Loading the src/commands/slashCommands/Other Command files.

So in the register commands file it doesnt register the src/command/slashCommands/Other If it's okay for you, it would be great if you could simply correct the code so that it works and then tell me what you changed (so that I can learn). Because in many other servers, people just talked around the issue (so they knew what was wrong but didn't help me clearly enough).
23 Replies
d.js toolkit
d.js toolkit13mo 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
treble/luna
treble/luna13mo ago
#rules 3, we are not gonna fix it for you Also
d.js docs
d.js docs13mo ago
To share long code snippets, use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks or files.
Jxni
JxniOP13mo ago
Its also fine if u tell me whats wrong
treble/luna
treble/luna13mo ago
if you would read the tag i would be able to help
treble/luna
treble/luna13mo ago
you are just setting the commands in client.commands. That does not register thel For that you'd have a separate script (and that code can be simplified)
Jxni
JxniOP13mo ago
But the Moderation commands are getting registered tho
treble/luna
treble/luna13mo ago
Because you probably registered them before
Jxni
JxniOP13mo ago
So what do I do?
treble/luna
treble/luna13mo ago
Register your commands the proper way you probably have a separate file for thar
Jxni
JxniOP13mo ago
I have the same project with just one Slash Command folder. And there the same file (like in the sourcebin link) looks like this:
const { Client, Collection } = require('discord.js');
const fs = require('fs');
const { logger } = require('./src/functions/functions');

module.exports = async (client) => {
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/commands/slashCommands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./src/commands/slashCommands/${file}`);
client.commands.set(command.data.name, command);
}

try {
const commands = client.commands.map(command => command.data.toJSON());
const globalCommands = await client.application.commands.set(commands);
console.log(`${logger()} ✅ Registered ${globalCommands.size} commands globally`);
} catch (error) {
console.error(`${logger()} ❌ Error registering slash commands: ${error}`);
}
};
const { Client, Collection } = require('discord.js');
const fs = require('fs');
const { logger } = require('./src/functions/functions');

module.exports = async (client) => {
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/commands/slashCommands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./src/commands/slashCommands/${file}`);
client.commands.set(command.data.name, command);
}

try {
const commands = client.commands.map(command => command.data.toJSON());
const globalCommands = await client.application.commands.set(commands);
console.log(`${logger()} ✅ Registered ${globalCommands.size} commands globally`);
} catch (error) {
console.error(`${logger()} ❌ Error registering slash commands: ${error}`);
}
};
treble/luna
treble/luna13mo ago
well, you dont have that code in the sourcebin you showed
d.js docs
d.js docs13mo ago
guide Creating Your Bot: Registering slash commands read more
treble/luna
treble/luna13mo ago
Thats the proper way to register commands
Jxni
JxniOP13mo ago
Ye bc thats the other project Where I just have to register files in one folder
treble/luna
treble/luna13mo ago
well yeah, you never even register your commands in the first place
Jxni
JxniOP13mo ago
but how do I do that all files getting registered in /src/commands/slashCommands/Moderation and /src/commands/slashCommands/Other
treble/luna
treble/luna13mo ago
Thats not really djs related, its just reading files
Jxni
JxniOP13mo ago
I know bruh But I asked how
treble/luna
treble/luna13mo ago
you can just iterate over your slashCommands folder and then check those subfolders
Jxni
JxniOP13mo ago
So if I use this code for the Project with multiple subfolders it will work? No it doesnt work it just says: [11/27/2023, 1:19:03 PM] ✅ Registered 0 commands globally
treble/luna
treble/luna13mo ago
yes, assuming you properly iterate over them
Want results from more Discord servers?
Add your server