From c8c0e1018a889b31fe85520fca444575b724e247 Mon Sep 17 00:00:00 2001 From: Dennis Heinrich Date: Tue, 26 Nov 2024 00:48:02 +0100 Subject: [PATCH] Added current game month in discord embed #2 --- config.example.json | 16 +++++++++ .../Interfaces/Configuration/ITranslation.ts | 2 ++ .../Configuration/ITranslationCommon.ts | 14 ++++++++ .../Configuration/ITranslationDiscordEmbed.ts | 2 ++ source/Services/Configuration.ts | 14 ++++++++ source/Services/DiscordEmbed.ts | 3 ++ source/Services/ServerStatusFeed.ts | 36 +++++++++++++++++-- 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 source/Interfaces/Configuration/ITranslationCommon.ts diff --git a/config.example.json b/config.example.json index df368ec..4839b1d 100644 --- a/config.example.json +++ b/config.example.json @@ -16,10 +16,26 @@ "descriptionOffline": "Server is offline", "descriptionUnknown": "Server status fetching", "titleServerName": "Server name", + "titleServerMap": "Server map", + "titleServerMods": "Server mods", "titleServerPassword": "Server password", "titleServerTime": "Server time", "titlePlayerCount": "Players online", "noPlayersOnline": "No players online" + }, + "common": { + "monthJanuary": "January", + "monthFebruary": "February", + "monthMarch": "March", + "monthApril": "April", + "monthMay": "May", + "monthJune": "June", + "monthJuly": "July", + "monthAugust": "August", + "monthSeptember": "September", + "monthOctober": "October", + "monthNovember": "November", + "monthDecember": "December" } } } \ No newline at end of file diff --git a/source/Interfaces/Configuration/ITranslation.ts b/source/Interfaces/Configuration/ITranslation.ts index 2cf3af6..81ae185 100644 --- a/source/Interfaces/Configuration/ITranslation.ts +++ b/source/Interfaces/Configuration/ITranslation.ts @@ -1,5 +1,7 @@ import ITranslationDiscordEmbed from "./ITranslationDiscordEmbed"; +import ITranslationCommon from "./ITranslationCommon"; export default interface ITranslation { discordEmbed: ITranslationDiscordEmbed; + common: ITranslationCommon; } \ No newline at end of file diff --git a/source/Interfaces/Configuration/ITranslationCommon.ts b/source/Interfaces/Configuration/ITranslationCommon.ts new file mode 100644 index 0000000..f075d96 --- /dev/null +++ b/source/Interfaces/Configuration/ITranslationCommon.ts @@ -0,0 +1,14 @@ +export default interface ITranslationCommon { + monthJanuary: string; + monthFebruary: string; + monthMarch: string; + monthApril: string; + monthMay: string; + monthJune: string; + monthJuly: string; + monthAugust: string; + monthSeptember: string; + monthOctober: string; + monthNovember: string; + monthDecember: string; +} \ No newline at end of file diff --git a/source/Interfaces/Configuration/ITranslationDiscordEmbed.ts b/source/Interfaces/Configuration/ITranslationDiscordEmbed.ts index 5b9bc69..e4c8fa2 100644 --- a/source/Interfaces/Configuration/ITranslationDiscordEmbed.ts +++ b/source/Interfaces/Configuration/ITranslationDiscordEmbed.ts @@ -4,6 +4,8 @@ export default interface ITranslationDiscordEmbed { descriptionOffline: string; descriptionUnknown: string; titleServerName: string; + titleServerMap: string; + titleServerMods: string; titleServerPassword: string; titleServerTime: string; titlePlayerCount: string; diff --git a/source/Services/Configuration.ts b/source/Services/Configuration.ts index 693e17b..6bca667 100644 --- a/source/Services/Configuration.ts +++ b/source/Services/Configuration.ts @@ -76,8 +76,22 @@ export default class Configuration implements IConfiguration{ || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerName) || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerPassword) || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerTime) + || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerMap) + || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerMods) || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titlePlayerCount) || this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.noPlayersOnline) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthJanuary) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthFebruary) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthMarch) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthApril) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthMay) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthJune) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthJuly) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthAugust) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthSeptember) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthOctober) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthNovember) + || this.isValueEmptyOrUndefined(this?.translation?.common?.monthDecember) ); } diff --git a/source/Services/DiscordEmbed.ts b/source/Services/DiscordEmbed.ts index b479c7d..a47e3d6 100644 --- a/source/Services/DiscordEmbed.ts +++ b/source/Services/DiscordEmbed.ts @@ -98,6 +98,8 @@ export default class DiscordEmbed { let embed = new EmbedBuilder(); let config = this.appConfiguration; + serverStats.getServerMonth(); + embed.setTitle(config.translation.discordEmbed.title); if (!serverStats.isOnline()) { embed.setColor(0xCA0000); @@ -127,6 +129,7 @@ export default class DiscordEmbed { {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.titlePlayerCount} (${serverStats.getPlayerCount()}/${serverStats.getMaxPlayerCount()}):`, value: playerListString diff --git a/source/Services/ServerStatusFeed.ts b/source/Services/ServerStatusFeed.ts index e5f7942..74257d3 100644 --- a/source/Services/ServerStatusFeed.ts +++ b/source/Services/ServerStatusFeed.ts @@ -3,6 +3,7 @@ import Configuration from "./Configuration"; import {XMLParser} from "fast-xml-parser"; import Logging from "./Logging"; import IPlayer from "../Interfaces/Feed/IPlayer"; +import IConfiguration from "../Interfaces/Configuration/IConfiguration"; export const CONNECTION_REFUSED = 'ECONNREFUSED'; export const NOT_FOUND = 'ENOTFOUND'; @@ -102,7 +103,7 @@ export default class ServerStatusFeed { * @returns {string} The server map name */ public getServerMap(): string { - return this.getServerStats()?.Server.map; + return this.getServerStats()?.Server.mapName; } /** @@ -117,6 +118,37 @@ export default class ServerStatusFeed { return dayTime / (60 * 60 * 1000) + 0.0001; } + /** + * Returns the server month as a string + * @returns {string} The server month as a string + */ + public getServerMonth(): string { + let config: IConfiguration = Configuration.getConfiguration(); + let dayTime = this.getServerStats()?.Server.dayTime; + if (dayTime === undefined) { + return "Error"; + } + let month = dayTime / (60 * 60 * 1000 * 24); + month = month % 1; + month = month * 12; + month = Math.floor(month); + let months = [ + config.translation.common.monthJanuary, + config.translation.common.monthFebruary, + config.translation.common.monthMarch, + config.translation.common.monthApril, + config.translation.common.monthMay, + config.translation.common.monthJune, + config.translation.common.monthJuly, + config.translation.common.monthAugust, + config.translation.common.monthSeptember, + config.translation.common.monthOctober, + config.translation.common.monthNovember, + config.translation.common.monthDecember + ] + return months[month-1]; + } + /** * Returns the server time in the format HH:MM * @returns {string} The server time in the format HH:MM @@ -136,7 +168,7 @@ export default class ServerStatusFeed { if(minutesString.length === 1) { minutesString = `0${minutesString}`; } - return `${hoursString}:${minutesString}`; + return `${hoursString}:${minutesString} (${this.getServerMonth()})`; } /**