how do you modularize your subcommands so their implementation is in different files?

can someone link me to a resource? https://discordjs.guide/slash-commands/advanced-creation.html#subcommands i understand you can .addSubcommand(<imported subcommand>.data) but how should the subcommand file look like? is it still SlashCommandBuilder or SlashCommandSubcommandBuilder?
9 Replies
d.js toolkit
d.js toolkit16mo 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!
irukanoko
irukanokoOP16mo ago
let's say i just have
command
|
|__ subcommand
command
|
|__ subcommand
both /command and /command subcommand show up. how do i hide the main command so just the latter shows up? ty for the help and clarifications! how do i make sure to prevent this?
d.js docs
d.js docs16mo ago
If you have duplicate commands on your server, you registered both global and guild commands. You can remove the duplicates by resetting either the global or guild commands - Resetting global commands: rest.put(Routes.applicationCommands(clientId), { body: [] }) - Resetting guild commands: rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })
irukanoko
irukanokoOP16mo ago
i have that in my deploy-commands.js which i run everytime i start up the bot
irukanoko
irukanokoOP16mo ago
./brawl create is running the main brawl command (which shouldn't be seen) and ./create is my actual command and it works. is there a fix to removing ./create but having ./brawl create be the correct functionality
irukanoko
irukanokoOP16mo ago
ideally, /brawl create is the only thing that should be visable. i reset the global/guild commands using the guide's deploy-commands.js is there something special you need to do with the command handler for subcommands?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
irukanoko
irukanokoOP16mo ago
everything works fine when i have it all in 1 file. somehow separating it is causing a problem do you have an example of something i can reference? the only thing i have in my main slashcommand is .addSubcommand(importedcmd.data, importedcmd.execute) ahhh i failed to understand that ty only ./brawl create shows up. great! but the output is wrong bc it's executing the brawl.js interaction reply (which shouldn't be seen) do i have to change my slashcommand handler or is there something i can do inside brawl.js from my understanding, my command handler is essentially the guide's https://discordjs.guide/creating-your-bot/command-handling.html#command-categories await command.execute(interaction); brawl.js
module.exports = {
data: new SlashCommandBuilder()
.setName("brawl")
.setDescription("Brawl main command description. You shouldn't see this.")
.addSubcommand(create.data, create.execute),
category: "public",
async execute(interaction) {
await interaction.reply(
"Brawl main command reply. You shouldn't see this."
);
},
};
module.exports = {
data: new SlashCommandBuilder()
.setName("brawl")
.setDescription("Brawl main command description. You shouldn't see this.")
.addSubcommand(create.data, create.execute),
category: "public",
async execute(interaction) {
await interaction.reply(
"Brawl main command reply. You shouldn't see this."
);
},
};
on discord the ./brawl create shows with all of it's desc and options but when i run it, executes the reply you see above. any thoughts on how to change the interaction to execute my subcommands? do i need to add my subcommands separately from the main commands?
const { loadFiles } = require("../functions/fileLoader");

async function loadCommands(client) {
console.time("Commands loaded");

const tableArray = new Array();
const commandFiles = await loadFiles("src/commands");
for (const file of commandFiles) {
const command = require(file);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
tableArray.push({ Command: command.data.name, Status: "✅" });
} else {
// [WARNING] The command at ${file} is missing a required "data" or "execute" property.`
tableArray.push({ Command: file, Status: "❌" });
}
}

console.table(tableArray, ["Command", "Status"]);
console.info("\n\x1b[36m%s\x1b[0m", "Loaded Commands."); // Cyan
console.timeEnd("Commands loaded");
}

module.exports = { loadCommands };
const { loadFiles } = require("../functions/fileLoader");

async function loadCommands(client) {
console.time("Commands loaded");

const tableArray = new Array();
const commandFiles = await loadFiles("src/commands");
for (const file of commandFiles) {
const command = require(file);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
tableArray.push({ Command: command.data.name, Status: "✅" });
} else {
// [WARNING] The command at ${file} is missing a required "data" or "execute" property.`
tableArray.push({ Command: file, Status: "❌" });
}
}

console.table(tableArray, ["Command", "Status"]);
console.info("\n\x1b[36m%s\x1b[0m", "Loaded Commands."); // Cyan
console.timeEnd("Commands loaded");
}

module.exports = { loadCommands };
do i need to explain more? fixed it. have to .getSubcommand() and invoke it in the main slashcommand execute
Want results from more Discord servers?
Add your server