The code imports necessary functions from custom modules, then defines an asynchronous function syncCommands
that synchronizes Discord commands by authorizing the gateway, retrieving existing commands, and registering or deleting specific commands based on their presence. The function uses functions like authorizeGateway
, getCommands
, registerCommand
, and deleteCommand
to perform these tasks.
var importer = require('../Core')
var {authorizeGateway} = importer.import("authorize discord")
var {registerCommand, getCommands, deleteCommand} = importer.import("discord api")
async function syncCommands() {
await authorizeGateway()
var commandResult = (await getCommands())
var commands = commandResult.map(command => command.name)
if(commands.includes('hello-orbb'))
await deleteCommand(commandResult.filter(c => c.name == 'hello-orbb')[0].id)
if(!commands.includes('hello'))
await registerCommand('hello', 'Check if Orbb is awake.')
if(!commands.includes('challenge'))
await registerCommand({
'name': 'challenge',
'description': 'Challenges another user to match, Orbb waits for the thumbs up.',
'options': [
{
'name': 'opponent-id',
'description': 'Name of the player you want to challenge for 1v1.',
'required': true,
'type': 6
},
{
'name': 'map',
'description': 'Name of the map to start on the server.',
'required': true,
'type': 3
}
]
})
if(!commands.includes('connect'))
await registerCommand({
'name': 'connect',
'description': 'RCon Connect to a Quake 3 server for remote administration over Discord.',
'options': [
{
'name': 'server-address',
'description': 'The IP address or domain name of the server to connect to including port.',
'required': true,
'type': 3
}
]
})
if(!commands.includes('rcon'))
await registerCommand({
'name': 'rcon',
'description': 'Set the password for future RCon commands, or send an rcon command to the connected server.',
'options': [
{
'name': 'rcon-password',
'description': 'Password to use with future RCon commands.',
'required': true,
'type': 3
},
{
'name': 'rcon-command',
'description': 'Send the following RCon command to the server.',
'required': false,
'type': 3
}
]
})
if(!commands.includes('config'))
await registerCommand({
'name': 'config',
'description': 'Execute a config file on the remote Quake 3 server after using /connect command.',
'options': [
{
'name': 'config-name',
'description': 'Name of the config script to execute',
'required': true,
'type': 3
}
// TODO: not required and list availabe config scripts through engine
]
})
if(!commands.includes('map'))
await registerCommand({
'name': 'map',
'description': 'Starts a server with the specified map and sends you a personal message when the server is ready.',
'options': [
{
'name': 'map-name',
'description': 'Name of the map to run the server.',
'required': true,
'type': 3
}
]
})
return await getCommands()
}
module.exports = syncCommands
const { authorizeGateway, registerCommand, getCommands, deleteCommand } = require('../Core');
/**
* Synchronizes Discord commands with the Quake 3 server.
* @returns {Promise} A list of available commands.
*/
async function syncCommands() {
// Authorize the gateway to interact with Discord
await authorizeGateway();
// Get the current list of commands from Discord
const commands = await getCommands();
// Define the commands to be registered
const commandsToRegister = [
{ name: 'hello', description: 'Check if Orbb is awake.' },
{
name: 'challenge',
description: 'Challenges another user to match, Orbb waits for the thumbs up.',
options: [
{
name: 'opponent-id',
description: 'Name of the player you want to challenge for 1v1.',
required: true,
type: 6,
},
{
name:'map',
description: 'Name of the map to start on the server.',
required: true,
type: 3,
},
],
},
{
name: 'connect',
description: 'RCon Connect to a Quake 3 server for remote administration over Discord.',
options: [
{
name:'server-address',
description: 'The IP address or domain name of the server to connect to including port.',
required: true,
type: 3,
},
],
},
{
name: 'rcon',
description: 'Set the password for future RCon commands, or send an rcon command to the connected server.',
options: [
{
name: 'rcon-password',
description: 'Password to use with future RCon commands.',
required: true,
type: 3,
},
{
name: 'rcon-command',
description: 'Send the following RCon command to the server.',
required: false,
type: 3,
},
],
},
{
name: 'config',
description: 'Execute a config file on the remote Quake 3 server after using /connect command.',
options: [
{
name: 'config-name',
description: 'Name of the config script to execute',
required: true,
type: 3,
},
],
},
{
name:'map',
description: 'Starts a server with the specified map and sends you a personal message when the server is ready.',
options: [
{
name:'map-name',
description: 'Name of the map to run the server.',
required: true,
type: 3,
},
],
},
];
// Remove the hello-orbb command if it exists
if (commands.map(command => command.name).includes('hello-orbb')) {
await deleteCommand(commands.find(command => command.name === 'hello-orbb').id);
}
// Register the commands that need to be added
for (const command of commandsToRegister) {
if (!commands.map(command => command.name).includes(command.name)) {
await registerCommand(command);
}
}
// Return the updated list of commands
return await getCommands();
}
module.exports = syncCommands;
var importer = require('../Core')
var {authorizeGateway} = importer.import('authorize discord')
var {registerCommand, getCommands, deleteCommand} = importer.import('discord api')
importer
from ../Core
.importer
to import specific functions from two modules:
authorizeGateway
from authorize discord
.registerCommand
, getCommands
, and deleteCommand
from discord api
.async function syncCommands() {
//...
}
syncCommands
that synchronizes Discord commands.await authorizeGateway()
var commandResult = (await getCommands())
var commands = commandResult.map(command => command.name)
authorizeGateway
.getCommands
and stores the result in commandResult
.name
and stores the result in the commands
array.if(commands.includes('hello-orbb'))
await deleteCommand(commandResult.filter(c => c.name == 'hello-orbb')[0].id)
if(!commands.includes('hello'))
await registerCommand('hello', 'Check if Orbb is awake.')
//...
hello-orbb
exists in the commands
array. If it does, the function deletes the command using deleteCommand
.hello
exists in the array. If it doesn't, the function registers a new command named hello
with the specified description.challenge
and connect
, registering them if they don't exist.rcon
with an array of options.