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.
npm run import -- "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
/**
* @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;The code imports several modules using the importer.import function.
discord api module is imported, extracting the following functions: registerCommand, getCommands, deleteCommand, and updateCommand.discord gateway module is imported, extracting the interactionsCommands object.discord express server module is not actually used in the code, so it is commented out.ALL_COMMANDS is a constant array containing a single string, 'launch'.The activityCommands function is an asynchronous function that manages bot commands for a specific guild.
guildId parameter, which defaults to null.getCommands(guildId).interactionsCommands object to point startActivity and endActivity to the discordExpress and closeExpress functions respectively.cmdDef with the name, description, type, and handler for the command launch.launch command is not already registered, it registers the command using registerCommand.ALL_COMMANDS array and deletes them using deleteCommand.The activityCommands function is exported as a module.