discord activities | discord client auth code | check discord commands | Search

The code imports Discord API modules, defines constants, and an asynchronous activityCommands function that manages bot commands for a specific guild, including registering, updating, and deleting commands.

This function is then exported as a module.

Run example

npm run import -- "start activity server"

start activity server

const {
  registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {interactionsCommands} = importer.import("discord gateway")
//const {discordExpress, closeExpress, getInstance} = importer.import("discord express server")

const ALL_COMMANDS = [
  'launch',
]

// bot commands using new API, same names as above but lower-case
async function activityCommands(guildId = null) {
  var cmd, cmdDef
  var commandResult = await getCommands(guildId)
  var commands = commandResult.map(command => command.name)

  interactionsCommands['startActivity'] = discordExpress
  interactionsCommands['endActivity'] = closeExpress

  cmdDef = {
    'name': 'launch',
    'description': 'Launch an activity',
    'type': 4,
    'handler': 2,
  }
  if(!commands.includes('launch')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'launch')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }

  var toRemove = commandResult.filter(c => !ALL_COMMANDS.includes(c.name))
  for(var i = 0; i < toRemove.length; i++) {
    await deleteCommand(toRemove[i].id, guildId)
  }

  return await getCommands()
}

module.exports = activityCommands

What the code could have been:

/**
 * @file Activity commands handler
 * @description Handles registration, updates and removals of activity commands
 */

const {
  registerCommand,
  getCommands,
  deleteCommand,
  updateCommand,
} = require('discord-api').import();
const { interactionsCommands } = require('discord-gateway').import();

// List of all available commands
const ALL_COMMANDS = ['launch'];

/**
 * @async Register, update or remove activity commands based on guild id
 * @param {string} [guildId] Guild id to operate on (default: null)
 * @returns {Promise<Command[]>} Updated list of commands
 */
async function activityCommands(guildId = null) {
  // Get all commands from the guild
  const commandResult = await getCommands(guildId);

  // Filter out commands that should not be removed
  const commandsToKeep = commandResult.filter((command) =>
    ALL_COMMANDS.includes(command.name)
  );

  // Filter out commands to be removed
  const commandsToRemove = commandResult.filter((command) =>
   !ALL_COMMANDS.includes(command.name)
  );

  // Register or update the 'launch' command if it does not exist or its definition has changed
  if (!commandsToKeep.find((command) => command.name === 'launch')) {
    // Create the command definition
    const cmdDef = {
      name: 'launch',
      description: 'Launch an activity',
      type: 4,
      handler: 2,
    };

    // Register the command
    await registerCommand(cmdDef, null, guildId);
  } else {
    // Get the existing command
    const cmd = commandResult.find((command) => command.name === 'launch');

    // Update the command definition if it has changed
    if (
      cmdDef.name!== cmd.name ||
      cmdDef.description!== cmd.description
    ) {
      await updateCommand(cmdDef, null, cmd.id, guildId);
    }
  }

  // Remove commands that should not be present
  for (const cmd of commandsToRemove) {
    await deleteCommand(cmd.id, guildId);
  }

  // Return the updated list of commands
  return await getCommands();
}

module.exports = activityCommands;

Code Breakdown

Importing Modules

The code imports several modules using the importer.import function.

Defining Constants

activityCommands Function

The activityCommands function is an asynchronous function that manages bot commands for a specific guild.

Exporting the Function

The activityCommands function is exported as a module.