added app state management

This commit is contained in:
mmacierzanka committed 2021-09-02 23:04:47 +02:00
1 parent c92cdf875c
commit f3c58f79ca
4 files changed
+22 -8

No files matched your search

+2 -1
View File
@@ -10,6 +10,7 @@ import { SystemMessageManager } from "./SystemMessage.js";
import { CommandMessageStructure } from "./types/Command.js";
import { InitOptions } from "./types/Bot.js";
import { HelpMessageParams } from "./types/HelpMessage.js";
import { applicationState } from "./global/state.js";
export declare interface Bot {
on(event: "READY", listener: Function): this;
@@ -134,7 +135,7 @@ export class Bot extends EventEmitter {
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name);
this.commands.add(helpMsg);
}
this.commands.freezeList();
applicationState.running = true;
process.stdout.write("Connecting to Discord... ");
this.client.login(this.token);
this.client.on("ready", async () => {
+4 -7
View File
@@ -3,6 +3,7 @@ import { MissingParameterError } from "./errors.js";
import { BooleanParameter, InputParameter, NumberParameter, ObjectParameter, StringParameter } from "./Parameter.js";
import { Command } from "./Command.js";
import { CommandMessageStructure, PhraseOccurrenceData } from "./types/Command.js";
import { applicationState } from "./global/state.js";
/**
* @class Command manager
@@ -66,6 +67,9 @@ export class CommandManager {
*/
public add(command: Command): boolean {
try {
if (applicationState.running) {
throw new Error("Cannot add command while the application is running");
}
if (!(command instanceof Command)) {
throw new TypeError("Inavlid argument type");
}
@@ -206,13 +210,6 @@ export class CommandManager {
return null;
}
public freezeList(): void {
if (!Object.isFrozen(this._list)) {
Object.freeze(this._list);
}
return;
}
private findPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null {
let returnValue: PhraseOccurrenceData | null = null;
this._list.map((c) => {
+13
View File
@@ -0,0 +1,13 @@
export class StateManager {
private _isRunning: boolean = false;
get running() {
return this._isRunning;
}
set running(bool) {
if (!this._isRunning) {
this._isRunning = bool;
}
}
}
+3
View File
@@ -0,0 +1,3 @@
import { StateManager } from "../StateManager.js";
export const applicationState = new StateManager();