Not really sure why, but seems that my slash commands are registering twice. I've tried various methods, yes using ChatGPT, but so far all of the results have duplicates.
Any suggestions? (Besides stop using ChatGPT since it's helping me learn to do this).
As far as I can tell it should only be registering once so not really sure where to go from here.
// Initialize your commands collection
clientmands = new Collection();
// Read command files from ./commands/
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Array to hold slash command definitions for registration
const slashCommandsData = [];
// Load each command file
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const commandModule = require(filePath);
// If a module exports multiple slash definitions:
if (Array.isArray(commandModule.data)) {
for (const slashDef of commandModule.data) {
if (!slashDef.name) continue; // Skip invalid definitions
slashCommandsData.push(slashDef.toJSON());
clientmands.set(slashDef.name, commandModule);
}
} else {
// Single slash command export
const { data } = commandModule;
if (!data.name) continue; // Skip if missing name
slashCommandsData.push(data.toJSON());
clientmands.set(data.name, commandModule);
}
}
console.log(`Total commands loaded: ${clientmands.size}`);
// Register slash commands for a specific guild after the client is ready
client.once('ready', async () => {
try {
const guildId = '1134123338734764072';
const guild = client.guilds.cache.get(guildId);
if (!guild) throw new Error(`Guild with ID ${guildId} not found`);
console.log(`Clearing existing commands for guild ${guildId}...`);
// Clear all existing commands for this guild
await guildmands.set([]);
console.log('Existing commands cleared.');
console.log(`Now registering (/) commands for guild ${guildId}...`);
const rest = new REST({ version: '10' }).setToken(BOT_TOKEN);
const result = await rest.put(
Routes.applicationGuildCommands(DISCORD_CLIENT_ID, guildId),
{ body: slashCommandsData }
);
console.log(`Successfully registered ${result.length} slash command(s) for the guild.`);
} catch (error) {
console.error('Error during command registration:', error);
}
});
// Listen for slash command interactions
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = clientmands.get(interactionmandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error('Error executing command:', error);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({ content: 'There was an error executing this command!', ephemeral: true });
} else {
await interaction.followUp({ content: 'There was an error executing this command!', ephemeral: true });
}
}
});
Not really sure why, but seems that my slash commands are registering twice. I've tried various methods, yes using ChatGPT, but so far all of the results have duplicates.
Any suggestions? (Besides stop using ChatGPT since it's helping me learn to do this).
As far as I can tell it should only be registering once so not really sure where to go from here.
// Initialize your commands collection
clientmands = new Collection();
// Read command files from ./commands/
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Array to hold slash command definitions for registration
const slashCommandsData = [];
// Load each command file
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const commandModule = require(filePath);
// If a module exports multiple slash definitions:
if (Array.isArray(commandModule.data)) {
for (const slashDef of commandModule.data) {
if (!slashDef.name) continue; // Skip invalid definitions
slashCommandsData.push(slashDef.toJSON());
clientmands.set(slashDef.name, commandModule);
}
} else {
// Single slash command export
const { data } = commandModule;
if (!data.name) continue; // Skip if missing name
slashCommandsData.push(data.toJSON());
clientmands.set(data.name, commandModule);
}
}
console.log(`Total commands loaded: ${clientmands.size}`);
// Register slash commands for a specific guild after the client is ready
client.once('ready', async () => {
try {
const guildId = '1134123338734764072';
const guild = client.guilds.cache.get(guildId);
if (!guild) throw new Error(`Guild with ID ${guildId} not found`);
console.log(`Clearing existing commands for guild ${guildId}...`);
// Clear all existing commands for this guild
await guildmands.set([]);
console.log('Existing commands cleared.');
console.log(`Now registering (/) commands for guild ${guildId}...`);
const rest = new REST({ version: '10' }).setToken(BOT_TOKEN);
const result = await rest.put(
Routes.applicationGuildCommands(DISCORD_CLIENT_ID, guildId),
{ body: slashCommandsData }
);
console.log(`Successfully registered ${result.length} slash command(s) for the guild.`);
} catch (error) {
console.error('Error during command registration:', error);
}
});
// Listen for slash command interactions
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = clientmands.get(interactionmandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error('Error executing command:', error);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({ content: 'There was an error executing this command!', ephemeral: true });
} else {
await interaction.followUp({ content: 'There was an error executing this command!', ephemeral: true });
}
}
});
Share
Improve this question
asked Mar 13 at 2:42
Jesse ElserJesse Elser
35 bronze badges
1 Answer
Reset to default 0Have you at any point registered the commands as global commands? If so, you may have a global command AND a local command both showing up in your server, because Discord treats them as separate. (I believe this is intended to make testing easier so you can test new commands in a private dev server before pushing test commands globally: Global command documentation)
I tested some of the code and it's working as expected for me - I don't see anything wrong there, unless you've got duplicates in your command files somewhere.
I'd recommend trying to clear the global commands and see if that gets rid of the duplicates:
await client.applicationmands.set([]);
Good luck!