discord | discord utilities | | Search

The code imports dependencies from discord API and utilities, and defines a deleteCommands function that retrieves a list of commands for a specified guild, skips exceptions, and deletes each command after a 3-second timeout. The deleteCommands function is then exported as a module for use in other applications.

Run example

npm run import -- "delete all commands"

delete all commands

const {
  registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {timeout} = importer.import("discord utilities")

const EXCEPT_COMMANDS = [
  
]

async function deleteCommands(guildId) {
  var toRemove = await getCommands(guildId)
  for(var i = 0; i < toRemove.length; i++) {
    if(EXCEPT_COMMANDS.includes(toRemove[i].name))
      continue
    await timeout(3000)
    await deleteCommand(toRemove[i].id, guildId)
  }
}


module.exports = deleteCommands

What the code could have been:

const { registerCommand, getCommands, deleteCommand, updateCommand } = require('discord.js').cmds;
const { timeout } = require('discord.js').utils;

/**
 * Commands to be excluded from deletion
 * @type {string[]}
 */
const EXCEPT_COMMANDS = [];

/**
 * Deletes commands in a guild, excluding the ones specified in EXCEPT_COMMANDS
 * @param {string} guildId - ID of the guild
 */
async function deleteCommands(guildId) {
  // Get the commands in the guild
  const commandsToDel = await getCommands(guildId);

  // Filter out the commands that should be excluded
  const commandsToDelFiltered = commandsToDel.filter((cmd) =>!EXCEPT_COMMANDS.includes(cmd.name));

  // Wait for 3 seconds between each command deletion
  for (const command of commandsToDelFiltered) {
    await timeout(3000);
    // Delete the command
    await deleteCommand(command.id, guildId);
  }
}

module.exports = deleteCommands;

Code Breakdown

Importing Dependencies

The code imports the following dependencies:

Constants and Variables

deleteCommands Function

The deleteCommands function performs the following operations:

  1. Calls getCommands to retrieve a list of commands for the specified guildId.
  2. Iterates through the retrieved commands.
  3. For each command, it checks if the command name is in the EXCEPT_COMMANDS array. If it is, the function skips to the next iteration.
  4. Waits for 3 seconds using timeout.
  5. Deletes the command with the specified id and guildId using deleteCommand.

Export

The deleteCommands function is exported as a module using module.exports.