discord remote | | discord remote proxy server | Search

The activityCommands function is used to manage bot commands for a specific Discord guild, registering or updating the remote command and removing any unnecessary commands. It retrieves existing commands, iterates over them, and returns an updated list of commands for the guild.

Run example

npm run import -- "discord remote control"

discord remote control


const {
  registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {interactionsCommands} = importer.import("discord gateway")
const {discordExpress, closeExpress} = importer.import("discord remote proxy server")
const {doMention, doPrivate} = importer.import("discord llm interactions")

const ALL_COMMANDS = [
  'remote',
]

// 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

  interactionsCommands['promptMention'] = doMention
  interactionsCommands['promptPrivate'] = doPrivate

  cmdDef = {
    'name': 'remote',
    'description': 'Launch the remote',
    'type': 4,
    'handler': 2,
  }
  if(!commands.includes('remote')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'remote')[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:

const {
  registerCommand,
  getCommands,
  deleteCommand,
  updateCommand,
} = require('discord-api');
const { interactionsCommands } = require('discord-gateway');
const {
  discordExpress,
  closeExpress,
} = require('discord-remote-proxy-server');
const { doMention, doPrivate } = require('discord-llm-interactions');

// Define bot commands
const ALL_COMMANDS = ['remote'];

/**
 * Get bot commands for a specific guild or global.
 * @param {string} [guildId] - Guild ID (optional)
 * @returns {Promise<Array>} - Array of bot commands
 */
async function activityCommands(guildId = null) {
  // Get existing commands for the guild or global
  const commandResult = await getCommands(guildId);
  const commands = commandResult.map((command) => command.name);

  // Update interactions commands with custom handlers
  interactionsCommands['startActivity'] = discordExpress;
  interactionsCommands['endActivity'] = closeExpress;
  interactionsCommands['promptMention'] = doMention;
  interactionsCommands['promptPrivate'] = doPrivate;

  // Define command definition
  const cmdDef = {
    name:'remote',
    description: 'Launch the remote',
    type: 4,
    handler: 2,
  };

  // Register or update command
  if (!commands.includes('remote')) {
    // Register new command
    await registerCommand(cmdDef, null, guildId);
  } else {
    // Get existing command
    const existingCmd = commandResult.find((c) => c.name ==='remote');
    if (
      cmdDef.name!== existingCmd.name ||
      cmdDef.description!== existingCmd.description
    ) {
      // Update existing command
      await updateCommand(cmdDef, null, existingCmd.id, guildId);
    }
  }

  // Delete unused commands
  const toRemove = commandResult.filter((c) =>!ALL_COMMANDS.includes(c.name));
  for (const cmd of toRemove) {
    await deleteCommand(cmd.id, guildId);
  }

  // Return updated commands
  return await getCommands();
}

module.exports = activityCommands;

Code Breakdown

Importing Modules

The code imports various modules from the importer object:

const {
  registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import('discord api')
const {interactionsCommands} = importer.import('discord gateway')
const {discordExpress, closeExpress} = importer.import('discord remote proxy server')
const {doMention, doPrivate} = importer.import('discord llm interactions')

These imported modules are used to interact with the Discord API, Gateway, Remote Proxy Server, and LLM Interactions.

Defining Constants

A constant array ALL_COMMANDS is defined to contain a list of command names:

const ALL_COMMANDS = [
 'remote',
]

Defining the activityCommands Function

The activityCommands function is defined to manage bot commands for a specific guild:

async function activityCommands(guildId = null) {
  // function implementation
}

Registering and Updating Commands

The function retrieves existing commands for the guild using getCommands and iterates over them to:

  1. Register or update the remote command if it doesn't exist or has changed.
  2. Remove commands not in the ALL_COMMANDS array.

Returning Updated Commands

The function returns the updated list of commands for the guild:

return await getCommands()

Exporting the activityCommands Function

The activityCommands function is exported as a module:

module.exports = activityCommands