The code defines a formatQuake3Response
function that extracts and processes data from a Quake 3 server response string using regular expressions, conditional logic, and object manipulation, and returns an embed object or the original response string if data is not available.
Alternatively, you can condense it into two sentences:
The code defines a formatQuake3Response
function that extracts data from a Quake 3 server response string using regular expressions and conditional logic.
The function returns an embed object with server information and player data if available, or the original response string if data is not available.
npm run import -- "format quake 3 response"
var removeCtrlChars = importer.import("remove ctrl characters")
var importer = require('../Core')
function formatQuake3Response(response, command, server) {
// try to detect format from response
var map = (/map:\s(.+)$/igm).exec(response)
var status = response.match(/name/ig) && response.match(/ping/ig)
var div = (/^[\-\s]+$/igm).exec(response)
var players = importer.regexToArray(/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([^\s]+)\s+([^\s]+)\s+.*?$/igm, response, false)
if(map && status && div) {
server.mapname = map[1]
return {
embeds: [{
title: removeCtrlChars(server.sv_hostname || server.hostname || server.gamename || server.game || ''),
description: server.ip + ':' + server.port,
color: 0xdda60f,
fields: [
{
name: 'Map',
value: map[1],
inline: false
},
{
name: 'Players',
value: server.clients + '/' + server.sv_maxclients,
inline: false
},
{
name: 'Player',
value: '```http\n' + players.map((p, i) => i + ') ' + removeCtrlChars(p[4])).join('\n') + '```',
inline: true
},
{
name: 'Score',
value: '```yaml\n' + players.map(p => p[2]).join('\n') + '```',
inline: true
},
{
name: 'Ping',
value: '```fix\n' + players.map(p => p[3]).join('\n') + '```',
inline: true
}
]
}]
}
}
return '\n```\n' + response + '\n```\n'
}
module.exports = formatQuake3Response
const removeCtrlChars = require('remove-control-characters');
const regexToArray = require('regex-array');
/**
* Formats a Quake3 response into a Discord embed.
*
* @param {string} response - The Quake3 server response.
* @param {string} command - The command that triggered the response.
* @param {object} server - The Quake3 server object.
* @returns {object|string} The formatted embed or the original response if formatting fails.
*/
function formatQuake3Response(response, command, server) {
// Regular expression patterns
const mapPattern = /map:\s(.+)$/igm;
const statusPattern = /name|ping/ig;
const divPattern = /^[\-\s]+$/igm;
const playersPattern = /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([^\s]+)\s+([^\s]+)\s*.*?$/igm;
// Try to detect format from response
const match = {
map: mapPattern.exec(response),
status: response.match(statusPattern) && response.match(statusPattern),
div: divPattern.exec(response),
};
// Extract players data
const players = regexToArray(playersPattern, response, false);
// Check if formatting was successful
if (match.map && match.status && match.div && players.length >= 5) {
server.mapname = match.map[1];
return {
embeds: [{
title: removeCtrlChars(server.sv_hostname || server.hostname || server.gamename || server.game || ''),
description: `${server.ip}:${server.port}`,
color: 0xdda60f,
fields: [
{
name: 'Map',
value: match.map[1],
inline: false,
},
{
name: 'Players',
value: `${server.clients}/${server.sv_maxclients}`,
inline: false,
},
{
name: 'Player',
value: '```http\n' + players.map((p, i) => `${i + 1}) ${removeCtrlChars(p[4])}`).join('\n') + '```',
inline: true,
},
{
name: 'Score',
value: '```yaml\n' + players.map(p => p[2]).join('\n') + '```',
inline: true,
},
{
name: 'Ping',
value: '```fix\n' + players.map(p => p[3]).join('\n') + '```',
inline: true,
},
],
}],
};
}
// Return original response if formatting fails
return `\n```\n${response}\n```\n`;
}
module.exports = formatQuake3Response;
The code starts by importing two modules:
var removeCtrlChars = importer.import('remove ctrl characters')
var importer = require('../Core')
The removeCtrlChars
function is imported from a module named'remove ctrl characters' using the import
method of the importer
module. The importer
module itself is required from a relative path ../Core
.
A function named formatQuake3Response
is defined with three parameters: response
, command
, and server
.
function formatQuake3Response(response, command, server) {
//...
}
The function uses several regular expressions to extract data from the response
string:
var map = (/map:\s(.+)$/igm).exec(response)
var status = response.match(/name/ig) && response.match(/ping/ig)
var div = (/^[\-\s]+$/igm).exec(response)
var players = importer.regexToArray(/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([^\s]+)\s+([^\s]+)\s+.*?$/igm, response, false)
These regular expressions are used to extract the following data:
map
: the map namestatus
: a flag indicating whether the response contains a server name and ping informationdiv
: an empty match (not used)players
: an array of player information (extracted using regexToArray
method of the importer
module)The function uses a conditional statement to determine whether to extract and process the data further.
if(map && status && div) {
//...
}
If the data is available, the function extracts and processes the map name, server information, and player data.
server.mapname = map[1]
return {
embeds: [{
title: removeCtrlChars(server.sv_hostname || server.hostname || server.gamename || server.game || ''),
description: server.ip + ':' + server.port,
color: 0xdda60f,
fields: [
//...
]
}]
}
The function returns an object with an embeds
property containing an array with a single embed object. The embed object has several properties, including title
, description
, color
, and fields
.
If the data is not available or not processed, the function returns a default value.
return '\n```\n' + response + '\n```\n'
Finally, the formatQuake3Response
function is exported as a module.
module.exports = formatQuake3Response