Slash commands being duplicated in global registering

Hey, When I try to register my slash commands globally, they are getting duplicated.

Here is the code I am using:
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const clientId = 'cliend_id';
const guildId = 'guild_id';
module.exports = (client) => {
    client.handleCommands = async (slashCommandFolders, path) => {
        client.slashCommandArray = [];
        for (folder of slashCommandFolders) {
            const slashCommandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
            for (const file of slashCommandFiles) {
                const command = require(`../slashcommands/${folder}/${file}`);
                client.slashCommandArray.push(command.data.toJSON());
                client.slashcommands.set(command.data.name, command)
            }

            const rest = new REST({ version: '9' }).setToken(process.env.DISCORD_TOKEN);

            (async () => {
                try {
                    console.log('Started refreshing application (/) commands.');
                    rest.put(Routes.applicationGuildCommands(clientId, guildId), {
                        body: client.slashCommandArray
                    })
                    console.log('Started refreshing application (/) commands.');
                } catch (error) {
                    console.error(error);
                }
            })();
        }
    }
}

I am using discord.js v13.8.0 and node.js v16
Was this page helpful?