From ae1413db60e722ad134f6b68c49bf577def101e3 Mon Sep 17 00:00:00 2001 From: Dennis Heinrich Date: Tue, 26 Nov 2024 01:05:36 +0100 Subject: [PATCH] Showing server mods in discord embed #5 --- source/Interfaces/Feed/IMod.ts | 5 +++++ source/Services/DiscordEmbed.ts | 7 +++++++ source/Services/ServerStatusFeed.ts | 15 +++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 source/Interfaces/Feed/IMod.ts diff --git a/source/Interfaces/Feed/IMod.ts b/source/Interfaces/Feed/IMod.ts new file mode 100644 index 0000000..39afb05 --- /dev/null +++ b/source/Interfaces/Feed/IMod.ts @@ -0,0 +1,5 @@ +export default interface IMod { + name: string; + author: string; + version: string; +} \ No newline at end of file diff --git a/source/Services/DiscordEmbed.ts b/source/Services/DiscordEmbed.ts index a47e3d6..593ed50 100644 --- a/source/Services/DiscordEmbed.ts +++ b/source/Services/DiscordEmbed.ts @@ -124,12 +124,19 @@ export default class DiscordEmbed { serverPassword = "-/-"; } + let serverMods = serverStats.getServerMods(); + let serverModsText = "-/-"; + if(serverMods.length > 0) { + serverModsText = serverMods.map(mod => `${mod.name}`).join(', '); + } + // @ts-ignore embed.addFields( {name: config.translation.discordEmbed.titleServerName, value: serverStats.getServerName()}, {name: config.translation.discordEmbed.titleServerPassword, value: serverPassword}, {name: config.translation.discordEmbed.titleServerTime, value: serverStats.getServerTime()}, {name: config.translation.discordEmbed.titleServerMap, value: serverStats.getServerMap()}, + {name: config.translation.discordEmbed.titleServerMods, value: serverModsText}, { name: `${config.translation.discordEmbed.titlePlayerCount} (${serverStats.getPlayerCount()}/${serverStats.getMaxPlayerCount()}):`, value: playerListString diff --git a/source/Services/ServerStatusFeed.ts b/source/Services/ServerStatusFeed.ts index 74257d3..036fb80 100644 --- a/source/Services/ServerStatusFeed.ts +++ b/source/Services/ServerStatusFeed.ts @@ -4,6 +4,7 @@ import {XMLParser} from "fast-xml-parser"; import Logging from "./Logging"; import IPlayer from "../Interfaces/Feed/IPlayer"; import IConfiguration from "../Interfaces/Configuration/IConfiguration"; +import IMod from "../Interfaces/Feed/IMod"; export const CONNECTION_REFUSED = 'ECONNREFUSED'; export const NOT_FOUND = 'ENOTFOUND'; @@ -118,6 +119,20 @@ export default class ServerStatusFeed { return dayTime / (60 * 60 * 1000) + 0.0001; } + public getServerMods(): IMod[] { + let modList = this.getServerStats()?.Server.Mods.Mod; + if(modList === undefined || !Array.isArray(modList)) { + return []; + } + return modList.map((mod: any) => { + return { + name: mod['#text'], + author: mod.author, + version: mod.version + } as IMod; + }); + } + /** * Returns the server month as a string * @returns {string} The server month as a string