Slash commands not being created

I have multiple slash commands that wont be create for some reason, and I have no Idea how to fix it, heres my code that sets it up:
const { Client, Events, GatewayIntentBits, ActivityType, SlashCommandBuilder } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent] });
client.setMaxListeners(20);

const { setupRules } = require('./rules');
const { setupAutoResponders } = require('./autoresponders');
const { setupRoles } = require('./roles');
const { setupWelcome } = require('./welcome');
const { setupGoodbye } = require('./leave');
const { setupBoost } = require('./boost');
const { setupTickets } = require('./tickets');
const { setupAffiliates } = require('./affiliates');
const { setupStarBoard } = require('./starboard');
const { setupClownBoard } = require('./clownboard')
const { setupEmbeds } = require('./embeds')
const { setupLevels } = require('./levelup')
const { setupColors } = require('./colors')
// Specify the role ID that users must have to use the commands
const requiredRoleID = '1302436968633864242'; // Replace with your actual role ID

client.on(Events.GuildMemberAdd, member => {
setupWelcome(member);
});

client.on(Events.GuildMemberRemove, member => {
setupGoodbye(member);
});

client.on(Events.ClientReady, (x) => {
console.log(`${x.user.tag} is ready!`);
client.user.setPresence({
activities: [{ name: `.gg/agu`, type: ActivityType.Listening }],
status: 'online',
});
});
const { Client, Events, GatewayIntentBits, ActivityType, SlashCommandBuilder } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent] });
client.setMaxListeners(20);

const { setupRules } = require('./rules');
const { setupAutoResponders } = require('./autoresponders');
const { setupRoles } = require('./roles');
const { setupWelcome } = require('./welcome');
const { setupGoodbye } = require('./leave');
const { setupBoost } = require('./boost');
const { setupTickets } = require('./tickets');
const { setupAffiliates } = require('./affiliates');
const { setupStarBoard } = require('./starboard');
const { setupClownBoard } = require('./clownboard')
const { setupEmbeds } = require('./embeds')
const { setupLevels } = require('./levelup')
const { setupColors } = require('./colors')
// Specify the role ID that users must have to use the commands
const requiredRoleID = '1302436968633864242'; // Replace with your actual role ID

client.on(Events.GuildMemberAdd, member => {
setupWelcome(member);
});

client.on(Events.GuildMemberRemove, member => {
setupGoodbye(member);
});

client.on(Events.ClientReady, (x) => {
console.log(`${x.user.tag} is ready!`);
client.user.setPresence({
activities: [{ name: `.gg/agu`, type: ActivityType.Listening }],
status: 'online',
});
});
21 Replies
d.js toolkit
d.js toolkit3mo 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
Sincerely, alxx ᴬᴳ
heres the rest of it:
const setupFunctions = [
setupRules,
setupAutoResponders,
setupRoles,
setupBoost,
setupTickets,
setupAffiliates,
setupEmbeds,
setupLevels,
setupColors,
// Pass the parameters to setup Boards using an anonymous function
() => setupStarBoard(client, '1262971002225430639', ':star:', 3), // Replace with your actual channel ID
() => setupClownBoard(client, '1301683483336507402', ':clown:', 3)
];

setupFunctions.forEach(setupFunction => {
client.on(Events.ClientReady, () => {
// Call the setup function
setupFunction(client);
});
});


client.login('token');
const setupFunctions = [
setupRules,
setupAutoResponders,
setupRoles,
setupBoost,
setupTickets,
setupAffiliates,
setupEmbeds,
setupLevels,
setupColors,
// Pass the parameters to setup Boards using an anonymous function
() => setupStarBoard(client, '1262971002225430639', ':star:', 3), // Replace with your actual channel ID
() => setupClownBoard(client, '1301683483336507402', ':clown:', 3)
];

setupFunctions.forEach(setupFunction => {
client.on(Events.ClientReady, () => {
// Call the setup function
setupFunction(client);
});
});


client.login('token');
Amgelo
Amgelo3mo ago
there's nothing registering commands in any of that code
Sincerely, alxx ᴬᴳ
As in I cant just make the slash commands in the required files? such as:
const { setupRules } = require('./rules');
const { setupRules } = require('./rules');
the .rules is a .js file in the code and it has all the slash commands inside it but its not setting it up?
Amgelo
Amgelo3mo ago
it depends on what you mean by "setting it up"
Sincerely, alxx ᴬᴳ
like in the ./rules it has:
client.on(Events.ClientReady, () => {
const memberRules = new SlashCommandBuilder()
.setName('member_rules')
.setDescription('This command sends the member rules.')
.addStringOption(option =>
option
.setName('embeds')
.setDescription('Which rule embed do you want to send?')
.setRequired(true)
.addChoices(
{ name: 'Headers', value: 'member_headers' },
{ name: 'Warning System', value: 'member_warning_system' },
{ name: 'Reporting System', value: 'member_reporting_system' },
{ name: 'Guidelines', value: 'member_guidelines' },
{ name: 'Server Rules', value: 'member_server_rules' },
{ name: 'VC Rules', value: 'member_vc_rules' },
{ name: 'Text Channel Rules', value: 'member_text_channel_rules' },
)
);

const staffRules = new SlashCommandBuilder()
.setName('staff_rules')
.setDescription('This command sends the staff rules.')
.addStringOption(option =>
option
.setName('embeds')
.setDescription('Which rule embed do you want to send?')
.setRequired(true)
.addChoices(
{ name: 'Headers', value: 'staff_headers' },
{ name: 'Punishments', value: 'staff_punishments' },
{ name: 'Punishments', value: 'staff_guidelines' },
{ name: 'Punishments', value: 'staff_rules' }
)
);

client.application.commands.create(memberRules);
client.application.commands.create(staffRules);
});
client.on(Events.ClientReady, () => {
const memberRules = new SlashCommandBuilder()
.setName('member_rules')
.setDescription('This command sends the member rules.')
.addStringOption(option =>
option
.setName('embeds')
.setDescription('Which rule embed do you want to send?')
.setRequired(true)
.addChoices(
{ name: 'Headers', value: 'member_headers' },
{ name: 'Warning System', value: 'member_warning_system' },
{ name: 'Reporting System', value: 'member_reporting_system' },
{ name: 'Guidelines', value: 'member_guidelines' },
{ name: 'Server Rules', value: 'member_server_rules' },
{ name: 'VC Rules', value: 'member_vc_rules' },
{ name: 'Text Channel Rules', value: 'member_text_channel_rules' },
)
);

const staffRules = new SlashCommandBuilder()
.setName('staff_rules')
.setDescription('This command sends the staff rules.')
.addStringOption(option =>
option
.setName('embeds')
.setDescription('Which rule embed do you want to send?')
.setRequired(true)
.addChoices(
{ name: 'Headers', value: 'staff_headers' },
{ name: 'Punishments', value: 'staff_punishments' },
{ name: 'Punishments', value: 'staff_guidelines' },
{ name: 'Punishments', value: 'staff_rules' }
)
);

client.application.commands.create(memberRules);
client.application.commands.create(staffRules);
});
does that not set/register it already?
Amgelo
Amgelo3mo ago
well that should show them in discord, assuming that's actually called somewhere
Sincerely, alxx ᴬᴳ
uh more explanation on the "called somewhere"?
Amgelo
Amgelo3mo ago
you can place some logs to see if that's called you need that code to be called somewhere or well, to execute at all node won't just read all your files and execute every .js it encounters given you're using client there then I assume that's inside a function of some sort that's being called by index, passing the client ^^
duck
duck3mo ago
I also worry that when you say this is in ./rules, that means with the following, you're adding a ClientReady listener when ClientReady is emitted
const { setupRules } = require('./rules');
client.on(Events.ClientReady, () => {
// Call the setup function
setupFunction(client);
});
const { setupRules } = require('./rules');
client.on(Events.ClientReady, () => {
// Call the setup function
setupFunction(client);
});
Sincerely, alxx ᴬᴳ
yes I use it in a function such as:
function setupRules(client) {

}
function setupRules(client) {

}
with all the code between and at the end export it and import it into index.js
module.exports = { setupRules };
module.exports = { setupRules };
so what should I do about that?
Amgelo
Amgelo3mo ago
if you're registering your ready event listener after it has been emitted then those listeners will never be triggered because ready only triggers once
duck
duck3mo ago
honestly I'd suggest reorganizing and consolidating all of your event listeners into 1 listener per event so that it's less confusing about what executes when
Amgelo
Amgelo3mo ago
just don't listen to ready inside those setup functions do your setup directly there pretty sure that'll error the second boot though since iirc application.commands.create throws an error if the command already exists
duck
duck3mo ago
(it doesn't, it just edits the command)
Amgelo
Amgelo3mo ago
ah, mb then still, don't register commands in ready it's more recommended to only register when your command's datas actually change you can reach the daily command register limit if you keep doing it on ready
Sincerely, alxx ᴬᴳ
sorry back! Hm so how should I rewrite the code, I am still pretty new to this
duck
duck3mo ago
instead of multiple listeners per event, just have 1 per event then delegate work from there
Sincerely, alxx ᴬᴳ
Hmm okay ty, I'll see if i can
Amgelo
Amgelo3mo ago
I mean it's just removing the wrap on client.on() just call your setups directly don't make them listen to ready
Sincerely, alxx ᴬᴳ
okayy ty thank you both, it made my code less messy but my commandds still dont get created nvmm it worked tysmm

Did you find this page helpful?