mirror of
https://github.com/cloudmaker97/FS25-Discord-Bot.git
synced 2025-12-06 08:28:33 +00:00
Added validation for existence of fields #3
This commit is contained in:
parent
2e5dc546d7
commit
ef8deb53b5
1 changed files with 34 additions and 25 deletions
|
|
@ -3,17 +3,25 @@ import IApplicationConfiguration from "../Interfaces/Configuration/IApplicationC
|
||||||
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";
|
import Logging from "./Logging";
|
||||||
|
import {Logger} from "winston";
|
||||||
|
|
||||||
export default class Configuration implements IConfiguration{
|
export default class Configuration implements IConfiguration{
|
||||||
|
private readonly logger: Logger;
|
||||||
public readonly discord: IDiscordConfiguration;
|
public readonly discord: IDiscordConfiguration;
|
||||||
public readonly application: IApplicationConfiguration;
|
public readonly application: IApplicationConfiguration;
|
||||||
public readonly translation: ITranslation;
|
public readonly translation: ITranslation;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.logger = Logging.getLogger();
|
||||||
|
try {
|
||||||
let config = require('../../config.json');
|
let config = require('../../config.json');
|
||||||
this.discord = config.discord;
|
this.discord = config.discord;
|
||||||
this.application = config.application;
|
this.application = config.application;
|
||||||
this.translation = config.translation;
|
this.translation = config.translation;
|
||||||
|
} catch (exception) {
|
||||||
|
this.logger.error("Error while loading configuration file, please check if the configuration file exists and is valid.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -39,7 +47,7 @@ export default class Configuration implements IConfiguration{
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private validateDiscordConfiguration(): boolean {
|
private validateDiscordConfiguration(): boolean {
|
||||||
return !(this.isValueEmptyOrUndefined(this.discord.botToken) || this.isValueEmptyOrUndefined(this.discord.channelId));
|
return !(this.isValueEmptyOrUndefined(this.discord?.botToken) || this.isValueEmptyOrUndefined(this.discord?.channelId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -48,10 +56,10 @@ export default class Configuration implements IConfiguration{
|
||||||
*/
|
*/
|
||||||
private validateApplicationConfiguration(): boolean {
|
private validateApplicationConfiguration(): boolean {
|
||||||
return !(
|
return !(
|
||||||
this.isValueUndefined(this.application.serverPassword)
|
this.isValueUndefined(this.application?.serverPassword)
|
||||||
|| this.isValueEmptyOrUndefined(this.application.serverStatsUrl)
|
|| this.isValueEmptyOrUndefined(this.application?.serverStatsUrl)
|
||||||
|| this.isValueEmptyOrUndefined(this.application.serverMapUrl)
|
|| this.isValueEmptyOrUndefined(this.application?.serverMapUrl)
|
||||||
|| this.isValueEmptyOrUndefined(this.application.updateIntervalSeconds)
|
|| this.isValueEmptyOrUndefined(this.application?.updateIntervalSeconds)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,15 +69,15 @@ export default class Configuration implements IConfiguration{
|
||||||
*/
|
*/
|
||||||
private validateTranslationConfiguration(): boolean {
|
private validateTranslationConfiguration(): boolean {
|
||||||
return !(
|
return !(
|
||||||
this.isValueEmptyOrUndefined(this.translation.discordEmbed.title)
|
this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.title)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionOnline)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.descriptionOnline)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionOffline)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.descriptionOffline)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.descriptionUnknown)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.descriptionUnknown)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerName)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerName)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerPassword)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerPassword)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.titleServerTime)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titleServerTime)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.titlePlayerCount)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.titlePlayerCount)
|
||||||
|| this.isValueEmptyOrUndefined(this.translation.discordEmbed.noPlayersOnline)
|
|| this.isValueEmptyOrUndefined(this?.translation?.discordEmbed?.noPlayersOnline)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,24 +86,25 @@ export default class Configuration implements IConfiguration{
|
||||||
* @returns boolean True if the configuration is valid
|
* @returns boolean True if the configuration is valid
|
||||||
*/
|
*/
|
||||||
public isConfigurationValid(): boolean {
|
public isConfigurationValid(): boolean {
|
||||||
const logger = Logging.getLogger();
|
|
||||||
if(!this.validateDiscordConfiguration()) {
|
if(!this.validateDiscordConfiguration()) {
|
||||||
logger.info("Discord configuration is not valid. Please check your configuration file.");
|
this.logger.error("Discord configuration is not valid. Please check your configuration file.");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
logger.info("Discord configuration is valid.");
|
this.logger.info("Discord configuration is valid.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!this.validateApplicationConfiguration()) {
|
if(!this.validateApplicationConfiguration()) {
|
||||||
logger.info("Application configuration is not valid. Please check your configuration file.");
|
this.logger.error("Application configuration is not valid. Please check your configuration file.");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
logger.info("Application configuration is valid.");
|
this.logger.info("Application configuration is valid.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!this.validateTranslationConfiguration()) {
|
if(!this.validateTranslationConfiguration()) {
|
||||||
logger.info("Translation configuration is not valid. Please check your configuration file.");
|
this.logger.error("Translation configuration is not valid. Please check your configuration file.");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
logger.info("Translation configuration is valid.");
|
this.logger.info("Translation configuration is valid.");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue