Split command

Hello, I'm asking for help because I don't even know how to implement this I have 2 commands /creative work and /creative action Keeping them in one file is very inconvenient, and I want to split them into two (something like work.js and action.js) But here's the problem: when I split it into different files, I get an error
DiscordAPIError[50035]: Invalid Form Body
4[APPLICATION_COMMANDS_DUPLICATE_NAME]: Application command names must be unique
DiscordAPIError[50035]: Invalid Form Body
4[APPLICATION_COMMANDS_DUPLICATE_NAME]: Application command names must be unique
As I understand it, he swears specifically at /creative So the question is, what needs to be changed in commandHandler and/or deploy so that I can use /creative in different files? CommandHandler code:
const { readdirSync } = require('fs');
const { log } = require('../functions');
const ExtendedClient = require('../class/ExtendedClient');

/**
*
* @param {ExtendedClient} client
*/
module.exports = (client) => {
for (const type of readdirSync('./src/commands/')) {
for (const dir of readdirSync('./src/commands/' + type)) {
for (const file of readdirSync('./src/commands/' + type + '/' + dir).filter((f) => f.endsWith('.js'))) {
const module = require('../commands/' + type + '/' + dir + '/' + file);

if (!module) continue;

if (type === 'prefix') {
if (!module.structure?.name || !module.run) {
log('Unable to load the command ' + file +' due to missing \'structure#name\' or/and \'run\' properties.', 'warn');

continue;
};

client.collection.prefixcommands.set(module.structure.name, module);

if (module.structure.aliases && Array.isArray(module.structure.aliases)) {
module.structure.aliases.forEach((alias) => {
client.collection.aliases.set(alias, module.structure.name);
});
};
} else {
if (!module.structure?.name || !module.run) {
log('Unable to load the command ' + file +' due to missing \'structure#name\' or/and \'run\' properties.', 'warn');

continue;
};

client.collection.interactioncommands.set(module.structure.name, module);
client.applicationcommandsArray.push(module.structure);
};

log('Loaded new command: ' + file, 'info');
};
};
};
};
const { readdirSync } = require('fs');
const { log } = require('../functions');
const ExtendedClient = require('../class/ExtendedClient');

/**
*
* @param {ExtendedClient} client
*/
module.exports = (client) => {
for (const type of readdirSync('./src/commands/')) {
for (const dir of readdirSync('./src/commands/' + type)) {
for (const file of readdirSync('./src/commands/' + type + '/' + dir).filter((f) => f.endsWith('.js'))) {
const module = require('../commands/' + type + '/' + dir + '/' + file);

if (!module) continue;

if (type === 'prefix') {
if (!module.structure?.name || !module.run) {
log('Unable to load the command ' + file +' due to missing \'structure#name\' or/and \'run\' properties.', 'warn');

continue;
};

client.collection.prefixcommands.set(module.structure.name, module);

if (module.structure.aliases && Array.isArray(module.structure.aliases)) {
module.structure.aliases.forEach((alias) => {
client.collection.aliases.set(alias, module.structure.name);
});
};
} else {
if (!module.structure?.name || !module.run) {
log('Unable to load the command ' + file +' due to missing \'structure#name\' or/and \'run\' properties.', 'warn');

continue;
};

client.collection.interactioncommands.set(module.structure.name, module);
client.applicationcommandsArray.push(module.structure);
};

log('Loaded new command: ' + file, 'info');
};
};
};
};
deploy.js code:
const { REST, Routes } = require("discord.js");
const { log } = require("../functions");
const config = require("../config");
const ExtendedClient = require('../class/ExtendedClient');

/**
*
* @param {ExtendedClient} client
*/
module.exports = async (client) => {
const rest = new REST({ version: '10' }).setToken(process.env.CLIENT_TOKEN || config.client.token);

try {
log('Started loading application commands... (this might take minutes!)', 'warn');

await rest.put(Routes.applicationCommands(process.env.CLIENT_ID || config.client.id), {
body: client.applicationcommandsArray
});

log('Successfully loaded application commands to Discord API.', 'done');
} catch (e) {
log(`Unable to load application commands to Discord API. ${e}`, 'err');
};
};
const { REST, Routes } = require("discord.js");
const { log } = require("../functions");
const config = require("../config");
const ExtendedClient = require('../class/ExtendedClient');

/**
*
* @param {ExtendedClient} client
*/
module.exports = async (client) => {
const rest = new REST({ version: '10' }).setToken(process.env.CLIENT_TOKEN || config.client.token);

try {
log('Started loading application commands... (this might take minutes!)', 'warn');

await rest.put(Routes.applicationCommands(process.env.CLIENT_ID || config.client.id), {
body: client.applicationcommandsArray
});

log('Successfully loaded application commands to Discord API.', 'done');
} catch (e) {
log(`Unable to load application commands to Discord API. ${e}`, 'err');
};
};
Thanks in advance for helping a newbie!
4 Replies
d.js toolkit
d.js toolkit15mo 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
ShompiFlen
ShompiFlen15mo ago
can you show how did you separate this two commands show the scripts
eternalxty
eternalxtyOP15mo ago
work.js
module.exports = {
structure: new SlashCommandBuilder()
.setName("creative")
.setDescription("test")
.addSubcommand(subcommand =>
subcommand
.setName("work")
.setDescription("test")
),
/**
* @param {ExtendedClient} client
* @param
{ChatInputCommandInteraction<true>} interaction
*/
run: async (client, interaction, args) => {
},
};
module.exports = {
structure: new SlashCommandBuilder()
.setName("creative")
.setDescription("test")
.addSubcommand(subcommand =>
subcommand
.setName("work")
.setDescription("test")
),
/**
* @param {ExtendedClient} client
* @param
{ChatInputCommandInteraction<true>} interaction
*/
run: async (client, interaction, args) => {
},
};
action.js
module.exports = {
structure: new SlashCommandBuilder()
.setName("creative")
.setDescription("test")
.addSubcommand(subcommand =>
subcommand
.setName('action')
.setDescription('test')
.addUserOption((option) =>
option.setName("User").setDescription("Select User").setRequired(true)
)),
/**
* @param {ExtendedClient} client
* @param {ChatInputCommandInteraction<true>} interaction
*/
run: async (client, interaction, args) => {
},
};
module.exports = {
structure: new SlashCommandBuilder()
.setName("creative")
.setDescription("test")
.addSubcommand(subcommand =>
subcommand
.setName('action')
.setDescription('test')
.addUserOption((option) =>
option.setName("User").setDescription("Select User").setRequired(true)
)),
/**
* @param {ExtendedClient} client
* @param {ChatInputCommandInteraction<true>} interaction
*/
run: async (client, interaction, args) => {
},
};
ShompiFlen
ShompiFlen15mo ago
yea i had a feeling ideally you would have an "entry" file for this command, in which you define its data (the builder) and use that file to deploy to the api, then inside that, lets call it "mini handler", you branch into the other subcommands, for example, with a switch statement const subcommand = interaction.options.getSubcommand() switch (subcommand) { case "test": TestCommandHandler(interaction); break; } if you plan on adding more and more commands this can get messy, you can go around that in other ways but for now this is just an idea right now your issue is that you are trying to deploy both commands as separate commands and both define the main one as "creative"
Want results from more Discord servers?
Add your server