From 3f852ea6fb3590ca773ca0b28a4686e42786cc8c Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 22 Mar 2021 21:55:53 +0200 Subject: [PATCH] this motd is frustrating --- gamedig/plugin.json | 2 +- gamedig/plugin.ts | 43 ++++++++++++++++++++++++++++++++++--------- squeebot.repo.json | 2 +- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/gamedig/plugin.json b/gamedig/plugin.json index bca1cac..342c87f 100644 --- a/gamedig/plugin.json +++ b/gamedig/plugin.json @@ -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"] diff --git a/gamedig/plugin.ts b/gamedig/plugin.ts index 94a28cc..d9fc109 100644 --- a/gamedig/plugin.ts +++ b/gamedig/plugin.ts @@ -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; } diff --git a/squeebot.repo.json b/squeebot.repo.json index 8c1056f..fe3430d 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -19,7 +19,7 @@ }, { "name": "gamedig", - "version": "1.1.0" + "version": "1.1.1" }, { "name": "jukebox",