quake3 server connector | quake 3 server commands | quake 3 server responses | Search

The testRcon function is an asynchronous module that retrieves a list of server masters, logs the list, sends an RCON command to the first master, waits for a second, and closes the UDP client, before being exported as a module. This function utilizes the serverApi to interact with the Quake 3 server, including importing and executing specific functions for listing masters and sending RCON commands.

Run example

npm run import -- "test quake 3 rcon commands"

test quake 3 rcon commands

var importer = require('../Core')
var serverApi = importer.import("quake 3 server commands")

async function testRcon (command) {
    var masters = await serverApi.listMasters(void 0, void 0, false)
    console.log(masters)
    await serverApi.sendRcon(masters[0].ip, masters[0].port, command)
    await new Promise(resolve => setTimeout(resolve, 1000))
    serverApi.udpClient.close()
}

module.exports = testRcon

What the code could have been:

// Import required modules
const { Core } = require('../Core');
const { Quake3ServerCommands } = Core.import('quake 3 server commands');

// Define the testRcon function
async function testRcon(command) {
  /**
   * Tests an RCON command on the first available master server.
   * 
   * @param {string} command - The RCON command to send.
   * 
   * @throws {Error} If the master server list is empty or the RCON command fails.
   */
  try {
    // Get the list of master servers
    const masters = await Quake3ServerCommands.listMasters();

    // If no master servers are available, throw an error
    if (!masters.length) {
      throw new Error('No master servers available');
    }

    // Get the first master server
    const master = masters[0];

    // Log the master server details
    console.log(`Master Server: ${master.ip}:${master.port}`);

    // Send the RCON command to the master server
    await Quake3ServerCommands.sendRcon(master.ip, master.port, command);

    // Pause for 1 second to allow the RCON command to be processed
    await new Promise(resolve => setTimeout(resolve, 1000));

    // Close the UDP client
    await Quake3ServerCommands.udpClient.close();
  } catch (error) {
    // Log any errors that occur
    console.error(error);
  }
}

// Export the testRcon function
module.exports = testRcon;

Code Breakdown

Importing Modules

Server API Import

testRcon Function

Functionality

  1. List Server Masters:
  2. Log Masters:
  3. Send RCON Command:
  4. Delay:
  5. Close UDP Client:

Export