Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Delete All Commands Function #10

Closed
ming-suhi opened this issue Jan 27, 2021 · 0 comments · Fixed by #9
Closed

Delete All Commands Function #10

ming-suhi opened this issue Jan 27, 2021 · 0 comments · Fixed by #9
Labels
Client Class enhancement New feature or request

Comments

@ming-suhi
Copy link
Owner

While da-slash does provide a delete function, it's only setup for commands it created and that still exists.
So if I delete a command's file, I can no longer call it up and delete it, I'd have to recreate the command with the same name, then delete it.

Here's a deleteCommands function that could be added to client.js, allowing you to delete all commands initialized by the bot, either globally or per guild via await slash.deleteCommands(guild_ids, del_global).
guild_ids being an array of guild ids.
del_global being a boolean to delete global commands or not.

Function

async deleteCommands(guild_ids = false, del_global = false) {
  let deletedCommands = new Array();
  if (guild_ids) {
    let postedGuildCommands = new Array();
    for (let guild_id of guild_ids) {
      const guildCommands = await this.client.api.applications(this.client.user.id).guilds(guild_id).commands.get();
      for (let i = 0; i < guildCommands.length; i++) {
        this.client.api.applications(this.client.user.id).guilds(guild_id).commands(guildCommands[i].id).delete();
      }
      postedGuildCommands.push({ guild: guild_id, commands: guildCommands });
    }
    deletedCommands.push({ guilds: postedGuildCommands });
  }
  if (del_global) {
    const postedGlobalCommands = await this.client.api.applications(this.client.user.id).commands.get();
    for (let i = 0; i < postedGlobalCommands.length; i++) {
      this.client.api.applications(this.client.user.id).commands(postedGlobalCommands[i].id).delete();
    }
    deletedCommands.push({ global: postedGlobalCommands });
  }
  return deletedCommands;
}

Usage

client.on('ready', async () => {
  await slash.deleteCommands(['613425648685547541','197038439483310086'], true);
  slash.postCommands();
});

Originally posted by @fasko-web in #8

@ming-suhi ming-suhi added enhancement New feature or request Client Class labels Jan 27, 2021
@ming-suhi ming-suhi linked a pull request Jan 27, 2021 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client Class enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant