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.
npm run import -- "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
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;
This is a JavaScript module that exports an asynchronous function playerCommands
. The function manages Discord bot commands using the Discord API.
The code imports the following modules:
discord api
: Provides functions for managing Discord commands, such as registerCommand
, getCommands
, deleteCommand
, and updateCommand
.discord gateway
: Provides the interactionsCommands
object, which is used to register custom interaction commands.discord music player server
: Provides the discordExpress
and closeExpress
functions, which are used to control the music player server.discord llm interactions
: Provides the doMention
and doPrivate
functions, which are used to handle LLM interactions.The playerCommands
function performs the following tasks:
registerCommand
, getCommands
, updateCommand
, and deleteCommand
functions.interactionsCommands
object.The function takes an optional guildId
parameter, which specifies the guild ID for which to manage commands.
The code defines the following variables:
ALL_COMMANDS
: An array of required command names, which is used to check for unused commands.cmdDef
: An object that defines the browse
command.commands
: An array of command names retrieved from the getCommands
function.toRemove
: An array of commands to be removed.cmd
: A command object retrieved from the getCommands
function.