How to register commands using .set() ( ill explain in thread )

hello
11 Replies
d.js toolkit
d.js toolkit4mo 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
noxics1
noxics14mo ago
ok so in my command handler i used to do a for loop with a client.commands.create(), but that made alot of ghost commands after a while. I changed it to a for loop with a array.push() for the slash commands and then client.commands.set(). but i cant get it to register the contextmenu commands. i do push them to the array aswell
CYPH ERZZ
CYPH ERZZ4mo ago
Hi, here's how i'm doing it : separate files for each commands :
async initializeCommands() {
const modulesPath = appRootPath.path + '/modules/discord/commands/';
const commandFiles = fs.readdirSync(modulesPath).filter(file => file.endsWith('.command.mjs'));

for (const file of commandFiles) {
const module = await import(`./commands/${file}`)
const command = module.default;
this.commands.set(command.data.name, command);
}
}
async initializeCommands() {
const modulesPath = appRootPath.path + '/modules/discord/commands/';
const commandFiles = fs.readdirSync(modulesPath).filter(file => file.endsWith('.command.mjs'));

for (const file of commandFiles) {
const module = await import(`./commands/${file}`)
const command = module.default;
this.commands.set(command.data.name, command);
}
}
this.commands is a Collection() And then in the ClientReady event :
this.client.once(Events.ClientReady, async () => {
console.log('Discord bot is online!');
const commands = this.commands.map(command => command.data.toJSON());
try {
for (const guildId of discordConfig.guildIds) {
await this.client.application.commands.set(commands, guildId);
console.log(`Successfully registered slash commands for guild ID: ${guildId}`);
}
} catch (error) {
console.error('Error registering commands:', error);
}
});
this.client.once(Events.ClientReady, async () => {
console.log('Discord bot is online!');
const commands = this.commands.map(command => command.data.toJSON());
try {
for (const guildId of discordConfig.guildIds) {
await this.client.application.commands.set(commands, guildId);
console.log(`Successfully registered slash commands for guild ID: ${guildId}`);
}
} catch (error) {
console.error('Error registering commands:', error);
}
});
This is part of a wider class ofc but this should help you Note that it's under dev, you might want to omit the for looping over guildIds (which is a whitelist for me) And use client.application.commands.set(commands)
noxics1
noxics14mo ago
@Cyph Erzz thanks, i made my own version, kinda. altough the contextmenus wont load
//in the above in code

let commandsArrayGlobal = [];

const commandFolders = [`commands`, `contextMenus`]

for (const folder of commandFolders) {

const files = fs.readdirSync(path.join(process.cwd(), folder)).filter(file => file.endsWith(".js"))
console.log(files)
for (const file of files) {
if(folder === "contextMenus") {
const commandName = file.name;
const command = require(path.join(process.cwd(), folder, file));
const contextMenu = command.execute;

/// client.commands.set(command.data.name, contextMenu)
commandsArrayGlobal.push({
name: command.name,
description: "",
type: command.type
})

}else if(folder === "commands") {
const commandName = file.split(".")[0]
const command = require(path.join(process.cwd(), folder, file));
const slashCommand = command.callback
client.commands.set(commandName, slashCommand)
commandsArrayGlobal.push({
name: commandName,
description: command.description,
options: command.options,
type: 1
})
}

}

}
//in client.ready

client.commands.set(commandsArrayGlobal)
//in the above in code

let commandsArrayGlobal = [];

const commandFolders = [`commands`, `contextMenus`]

for (const folder of commandFolders) {

const files = fs.readdirSync(path.join(process.cwd(), folder)).filter(file => file.endsWith(".js"))
console.log(files)
for (const file of files) {
if(folder === "contextMenus") {
const commandName = file.name;
const command = require(path.join(process.cwd(), folder, file));
const contextMenu = command.execute;

/// client.commands.set(command.data.name, contextMenu)
commandsArrayGlobal.push({
name: command.name,
description: "",
type: command.type
})

}else if(folder === "commands") {
const commandName = file.split(".")[0]
const command = require(path.join(process.cwd(), folder, file));
const slashCommand = command.callback
client.commands.set(commandName, slashCommand)
commandsArrayGlobal.push({
name: commandName,
description: command.description,
options: command.options,
type: 1
})
}

}

}
//in client.ready

client.commands.set(commandsArrayGlobal)
CYPH ERZZ
CYPH ERZZ4mo ago
Ah ! I'v never worked with contextMenu sorry can't help for now !
duck
duck4mo ago
context menu commands are still application commands, so they're deployed exactly the same as slash commands it's still recommended to separate your deployment from your bot running, as it's unnecessary to deploy commands if you haven't changed them our guide's example deployment uses the REST client instead since it also doesn't require a logged in client however, if you specifically want to use <Client>.application.commands.set(), that's totally up to you that being said, it doesn't look like you're actually calling this anywhere <Client>.commands isn't a native property and is presumably a Collection this is often used to store commands for your bot to actually handle interactions for, but setting commands in this collection doesn't send them to discord
noxics1
noxics14mo ago
hi, thanks for the answer. im getting help in #djs-help-v14 altough we cant seem to see why it doesnt want to work
duck
duck4mo ago
in future please don't crosspost
noxics1
noxics14mo ago
this was posted way before i started to ask in that channel.
duck
duck4mo ago
yes, and asking in that channel was the crossposting
noxics1
noxics14mo ago
cuz i didnt see it because of discord sorry bout that