mirror of
https://github.com/cloudmaker97/FS25-Discord-Bot.git
synced 2025-12-06 08:28:33 +00:00
Added validation for user configuration #3
This commit is contained in:
parent
a3f8586c01
commit
883406c3de
4 changed files with 100 additions and 2 deletions
|
|
@ -6,7 +6,6 @@
|
||||||
"updateIntervalSeconds": 30
|
"updateIntervalSeconds": 30
|
||||||
},
|
},
|
||||||
"discord": {
|
"discord": {
|
||||||
"guildId": "DiscordServerId_12345",
|
|
||||||
"channelId": "DiscordChannelId_12345",
|
"channelId": "DiscordChannelId_12345",
|
||||||
"botToken": "DiscordSecretBotToken_XYZ"
|
"botToken": "DiscordSecretBotToken_XYZ"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
export default interface IDiscordConfiguration {
|
export default interface IDiscordConfiguration {
|
||||||
guildId: string;
|
|
||||||
channelId: string;
|
channelId: string;
|
||||||
botToken: string;
|
botToken: string;
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,18 @@ import DiscordService from "./Services/DiscordEmbed";
|
||||||
|
|
||||||
const appLogger = Logging.getLogger();
|
const appLogger = Logging.getLogger();
|
||||||
const appConfig: Configuration = new Configuration();
|
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({
|
const discordClient = new Client({
|
||||||
intents: [IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages]
|
intents: [IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages]
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import IDiscordConfiguration from "../Interfaces/Configuration/IDiscordConfigura
|
||||||
import IApplicationConfiguration from "../Interfaces/Configuration/IApplicationConfiguration";
|
import IApplicationConfiguration from "../Interfaces/Configuration/IApplicationConfiguration";
|
||||||
import IConfiguration from "../Interfaces/Configuration/IConfiguration";
|
import IConfiguration from "../Interfaces/Configuration/IConfiguration";
|
||||||
import ITranslation from "../Interfaces/Configuration/ITranslation";
|
import ITranslation from "../Interfaces/Configuration/ITranslation";
|
||||||
|
import Logging from "./Logging";
|
||||||
|
|
||||||
export default class Configuration implements IConfiguration{
|
export default class Configuration implements IConfiguration{
|
||||||
public readonly discord: IDiscordConfiguration;
|
public readonly discord: IDiscordConfiguration;
|
||||||
|
|
@ -15,6 +16,93 @@ export default class Configuration implements IConfiguration{
|
||||||
this.translation = config.translation;
|
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 {
|
public static getConfiguration(): IConfiguration {
|
||||||
return new Configuration();
|
return new Configuration();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue