Max Nguyen
Max Nguyen

Reputation: 311

How to delete a Discord.js commands (v13)

I been looking all over google but still haven't find a way to delete a command I accidentally created. So I have this command call "ping" and I want to delete it. Here the code I use to create it:

// importing stuff
const DiscordJS = require("discord.js");
const dotenv = require("dotenv");
dotenv.config();

console.log("Starting up");

const client = new DiscordJS.Client({
  intents: [DiscordJS.Intents.FLAGS.GUILDS, DiscordJS.Intents.FLAGS.GUILD_MESSAGES]
});

console.log("Waiting for bot to log in...");

client.on("ready", () => {
  console.log("Bot Login");

  const guild = client.guilds.cache.get("913238101902630983");
  let commands;


  if (guild) {
    commands = guild.commands;
  } else {
    commands = client.application.commands;
  }
 

  commands.create({
    name: "ping",
    description: "Reply with pong"
  });
});

client.login(process.env.token);

So now I want to delete that ping command. After searching google I come across 2 way to do so.

  1. Use client.api.applications(client.user.id).commands("command-id (interaction.data.id)").delete() the problem with this solution is I don't know how to get the commandId google searching still doesn't help. Source

  2. Use commands.cache.find(c => c.name === 'command name').delete();. The problem with this one is when I put the ping command name in so commands.cache.find(c => c.name === 'ping').delete(); it give me this error TypeError: Cannot read properties of undefined (reading 'delete'). Source

Please help me, Thanks in advance!

Upvotes: 0

Views: 1388

Answers (1)

Kaeso
Kaeso

Reputation: 136

An easy solution to this is to simply clear all slash commands, and then recreate them after. The following code will clear all global commands.

await client.application.commands.set([]);

And to clear guild-specific commands do this:

const guild = await client.guilds.fetch(/*Guild ID goes here*/);
guild.commands.set([]);

Upvotes: 1

Related Questions