From 883406c3de5231abd6db47685ce9bf870994d2f8 Mon Sep 17 00:00:00 2001 From: Dennis Heinrich Date: Mon, 25 Nov 2024 23:58:44 +0100 Subject: [PATCH] Added validation for user configuration #3 --- config.example.json | 1 - .../Configuration/IDiscordConfiguration.ts | 1 - source/Main.ts | 12 +++ source/Services/Configuration.ts | 88 +++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/config.example.json b/config.example.json index dd5c604..df368ec 100644 --- a/config.example.json +++ b/config.example.json @@ -6,7 +6,6 @@ "updateIntervalSeconds": 30 }, "discord": { - "guildId": "DiscordServerId_12345", "channelId": "DiscordChannelId_12345", "botToken": "DiscordSecretBotToken_XYZ" }, diff --git a/source/Interfaces/Configuration/IDiscordConfiguration.ts b/source/Interfaces/Configuration/IDiscordConfiguration.ts index 63b9d6d..4d196ec 100644 --- a/source/Interfaces/Configuration/IDiscordConfiguration.ts +++ b/source/Interfaces/Configuration/IDiscordConfiguration.ts @@ -1,5 +1,4 @@ export default interface IDiscordConfiguration { - guildId: string; channelId: string; botToken: string; } \ No newline at end of file diff --git a/source/Main.ts b/source/Main.ts index bf2dedb..2d86736 100644 --- a/source/Main.ts +++ b/source/Main.ts @@ -5,6 +5,18 @@ import DiscordService from "./Services/DiscordEmbed"; const appLogger = Logging.getLogger(); const appConfig: Configuration = new Configuration(); + +/** + * Check if the configuration is valid and exit the application if it is not + */ +if(!appConfig.isConfigurationValid()) { + appLogger.error("Configuration is not valid. Exiting application."); + process.exit(1); +} + +/** + * Create a new discord client instance + */ const discordClient = new Client({ intents: [IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages] }); diff --git a/source/Services/Configuration.ts b/source/Services/Configuration.ts index f892fc2..0f203be 100644 --- a/source/Services/Configuration.ts +++ b/source/Services/Configuration.ts @@ -2,6 +2,7 @@ import IDiscordConfiguration from "../Interfaces/Configuration/IDiscordConfigura import IApplicationConfiguration from "../Interfaces/Configuration/IApplicationConfiguration"; import IConfiguration from "../Interfaces/Configuration/IConfiguration"; import ITranslation from "../Interfaces/Configuration/ITranslation"; +import Logging from "./Logging"; export default class Configuration implements IConfiguration{ public readonly discord: IDiscordConfiguration; @@ -15,6 +16,93 @@ export default class Configuration implements IConfiguration{ this.translation = config.translation; } + /** + * Returns true if the value is empty or undefined + * @param value + * @private + */ + private isValueEmptyOrUndefined(value: any): boolean { + return value == null || value == "" || value == undefined; + } + + /** + * Returns true if the value is undefined + * @param value + * @private + */ + private isValueUndefined(value: any): boolean { + return value == undefined; + } + + /** + * Validates the discord configuration and returns true if the configuration is valid + * @private + */ + private validateDiscordConfiguration(): boolean { + return !(this.isValueEmptyOrUndefined(this.discord.botToken) || this.isValueEmptyOrUndefined(this.discord.channelId)); + } + + /** + * Validates the application configuration and returns true if the configuration is valid + * @private + */ + private validateApplicationConfiguration(): boolean { + return !( + this.isValueUndefined(this.application.serverPassword) + || this.isValueEmptyOrUndefined(this.application.serverStatsUrl) + || this.isValueEmptyOrUndefined(this.application.serverMapUrl) + || this.isValueEmptyOrUndefined(this.application.updateIntervalSeconds) + ); + } + + /** + * Validates the translation configuration and returns true if the configuration is valid + * @private + */ + private validateTranslationConfiguration(): boolean { + return !( + this.isValueEmptyOrUndefined(this.translation.discordEmbed.title) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionOnline) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionOffline) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionUnknown) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerName) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerPassword) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerTime) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.titlePlayerCount) + || this.isValueEmptyOrUndefined(this.translation.discordEmbed.noPlayersOnline) + ); + } + + /** + * Validates the configuration file and returns true if the configuration is valid + * @returns boolean True if the configuration is valid + */ + public isConfigurationValid(): boolean { + const logger = Logging.getLogger(); + if(!this.validateDiscordConfiguration()) { + logger.info("Discord configuration is not valid. Please check your configuration file."); + return false; + } else { + logger.info("Discord configuration is valid."); + } + if(!this.validateApplicationConfiguration()) { + logger.info("Application configuration is not valid. Please check your configuration file."); + return false; + } else { + logger.info("Application configuration is valid."); + } + if(!this.validateTranslationConfiguration()) { + logger.info("Translation configuration is not valid. Please check your configuration file."); + return false; + } else { + logger.info("Translation configuration is valid."); + } + return true; + } + + /** + * Returns the configuration object + */ public static getConfiguration(): IConfiguration { return new Configuration(); }