quake3 server connector | , test sending a discord message | respond discord commands | Search

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.

Cell 1

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

What the code could have been:

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;

Code Breakdown

Importing Modules

var importer = require('../Core')
var {authorizeGateway} = importer.import('authorize discord')
var {registerCommand, getCommands, deleteCommand} = importer.import('discord api')

Syncing Commands Function

async function syncCommands() {
    //...
}

Authorizing Gateway and Retrieving Commands

await authorizeGateway()
var commandResult = (await getCommands())
var commands = commandResult.map(command => command.name)

Registering and Deleting Commands

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.')
//...