How to delete slash commands in Discord.js v13

Viewed 19591

const { glob } = require("glob");
const { promisify } = require("util");
const { Client } = require("discord.js");
const mongoose = require("mongoose");

const globPromise = promisify(glob);

/**
 * @param {Client} portle
 */
module.exports = async (portle) => {
    const commandFiles = await globPromise(`${process.cwd()}/commands/**/*.js`);
    commandFiles.map((value) => {
        const file = require(value);
        const splitted = value.split("/");
        const directory = splitted[splitted.length - 2];

        if (file.name) {
            const properties = { directory, ...file };
            portle.commands.set(file.name, properties);
        }
    });

    const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
    eventFiles.map((value) => require(value));

    const slashCommands = await globPromise(
        `${process.cwd()}/slash-cmds/*/*.js`
    );

    const arrayOfSlashCommands = [];
    slashCommands.map((value) => {
        const file = require(value);
        if (!file?.name) return;
        portle.slashCommands.set(file.name, file);

        if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
        arrayOfSlashCommands.push(file);
    });

    portle.on("ready", async () => {
        await portle.guilds.cache
            .get("884380331170484244")
            .commands.set(arrayOfSlashCommands);
    });

    const mongooseURI = process.env.URI;
    if (!mongooseURI) throw new Error("Unspecified mongoose connection string!");

    mongoose.connect(mongooseURI).then(() => console.log('Connected to mongodb'));
};

I just started learning how to make slash commands and when I made one I restarted my bot Image of my commands, and now one of my slash commands is duplicated. How would I go about deleting the duplicate?

Command and Event Handler code below, I found on a yt tutorial.

2 Answers

I suspect if you followed their tutorial you're looking to do this via the REST client directly - here's a way to delete all slash commands for a specific Guild.

require('dotenv').config();

const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

const token = process.env.TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.TEST_GUILD_ID;
    
const rest = new REST({ version: '9' }).setToken(token);
rest.get(Routes.applicationGuildCommands(clientId, guildId))
    .then(data => {
        const promises = [];
        for (const command of data) {
            const deleteUrl = `${Routes.applicationGuildCommands(clientId, guildId)}/${command.id}`;
            promises.push(rest.delete(deleteUrl));
        }
        return Promise.all(promises);
    });

to do this for your global commands just use Routes.applicationCommands(clientId) instead of Routes.applicationGuildCommands(clientId, guildId)

You may simply use the ApplicationCommand#delete method to delete your slash commands, how? firstly let's get the command / ApplicationCommand object like so:


    client.application.commands.fetch('123456789012345678') // id of your command
      .then( (command) => {
    console.log(`Fetched command ${command.name}`)
    // further delete it like so:
    command.delete()
    console.log(`Deleted command ${command.name}`)
    }).catch(console.error);

This also exists with ApplicationCommandManager#delete method which is way easier! you may simply get your command's Id and pass it through the manager's method like so:

<guild>.commands.delete('123456789012345678')
Related