discord player | | discord music player server | Search

The playerCommands function is a JavaScript module that manages Discord bot commands by registering, updating, and deleting commands, as well as removing unused commands, using various functions from imported Discord modules.

Run example

npm run import -- "discord music player"

discord music player


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

const ALL_COMMANDS = [
  'browse',
]

// bot commands using new API, same names as above but lower-case
async function playerCommands(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': 'browse',
    'description': 'Launch the music player',
    'type': 4,
    'handler': 2,
  }
  if(!commands.includes('browse')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'browse')[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 = playerCommands


What the code could have been:

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

const {
  interactionsCommands,
} = require('discord-gateway').interactions;

const {
  discordExpress,
  closeExpress,
} = require('discord-music-player-server').server;

const {
  doMention,
  doPrivate,
} = require('discord-llm-interactions').interactions;

const ALL_COMMANDS = ['browse'];

/**
 * Register, update, or delete bot commands for a guild.
 * 
 * @param {string} [guildId] - The ID of the guild to register commands for.
 * @returns {Promise<object[]>} A list of registered commands.
 */
async function playerCommands(guildId) {
  // Get the current commands for the guild.
  const commandResult = await getCommands(guildId);

  // Register custom interactions.
  interactionsCommands['startActivity'] = discordExpress;
  interactionsCommands['endActivity'] = closeExpress;
  interactionsCommands['promptMention'] = doMention;
  interactionsCommands['promptPrivate'] = doPrivate;

  // Define the command to register or update.
  const cmdDef = {
    name: 'browse',
    description: 'Launch the music player',
    type: 4,
    handler: 2,
  };

  // Check if the command already exists.
  const existingCommand = commandResult.find((c) => c.name === cmdDef.name);

  // Register or update the command.
  if (!existingCommand) {
    await registerCommand(cmdDef, null, guildId);
  } else {
    // Update the command if its definition has changed.
    if (
      cmdDef.name!== existingCommand.name ||
      cmdDef.description!== existingCommand.description
    ) {
      await updateCommand(cmdDef, null, existingCommand.id, guildId);
    }
  }

  // Remove any commands that are not in the ALL_COMMANDS list.
  const toRemove = commandResult.filter((c) =>!ALL_COMMANDS.includes(c.name));
  await Promise.all(toRemove.map((c) => deleteCommand(c.id, guildId)));

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

module.exports = playerCommands;

Code Overview

This is a JavaScript module that exports an asynchronous function playerCommands. The function manages Discord bot commands using the Discord API.

Imported Modules

The code imports the following modules:

Functionality

The playerCommands function performs the following tasks:

  1. Registers and updates bot commands using the registerCommand, getCommands, updateCommand, and deleteCommand functions.
  2. Registers custom interaction commands using the interactionsCommands object.
  3. Removes unused commands.
  4. Returns the updated list of bot commands.

Parameters

The function takes an optional guildId parameter, which specifies the guild ID for which to manage commands.

Variables

The code defines the following variables: