this motd is frustrating

This commit is contained in:
Evert Prants 2021-03-22 21:55:53 +02:00
parent 1f46817721
commit 3f852ea6fb
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 36 additions and 11 deletions

View File

@ -2,7 +2,7 @@
"main": "plugin.js",
"name": "gamedig",
"description": "Game server status string",
"version": "1.1.0",
"version": "1.1.1",
"tags": ["commands", "games"],
"dependencies": ["simplecommands"],
"npmDependencies": ["gamedig@^2.0.23"]

View File

@ -12,13 +12,7 @@ import { query, QueryResult } from 'gamedig';
interface IMinecraftType {
vanilla: {
raw: {
description: {
extra: [
{
text: string;
}
];
},
description: any,
players: {
max: number,
online: number;
@ -39,6 +33,36 @@ interface ICommonResponse {
version: string;
}
/**
* Find a string from a random mash of objects..
* @param obj Object
* @returns found string or undefined
*/
function traverseFindString(obj: any): string | undefined {
if (typeof obj === 'string') {
return obj;
}
if (Array.isArray(obj)) {
obj = obj.map((v) => traverseFindString(v));
for (const p of obj) {
if (typeof p === 'string') {
return p;
}
}
return;
}
for (const k of Object.keys(obj)) {
const find = traverseFindString(obj[k]);
if (find) {
return find;
}
}
return;
}
class Parsers {
public static parseMinecraftStatus(response: QueryResult): ICommonResponse | null {
const result: ICommonResponse = {
@ -56,8 +80,8 @@ class Parsers {
return null;
}
result.description = raw.vanilla.raw.description.extra[0].text
.replace(/§./g, '').replace(/\n/g, ' ');
const desc = traverseFindString(raw.vanilla.raw.description) || '';
result.description = desc.replace(/§./g, '').replace(/\n/g, ' ');
result.maxPlayers = response.maxplayers;
result.version = raw.vanilla.raw.version.name;
@ -110,6 +134,7 @@ class GamePlugin extends Plugin {
throw new Error();
}
} catch (e) {
console.log(e)
msg.resolve([['field', 'Server is offline.', { type: 'title' }]]);
return true;
}

View File

@ -19,7 +19,7 @@
},
{
"name": "gamedig",
"version": "1.1.0"
"version": "1.1.1"
},
{
"name": "jukebox",