diff --git a/src/Bot.ts b/src/Bot.ts index 93b8019..c315b63 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -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 () => { diff --git a/src/CommandManager.ts b/src/CommandManager.ts index acd1444..844e989 100644 --- a/src/CommandManager.ts +++ b/src/CommandManager.ts @@ -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) => { diff --git a/src/StateManager.ts b/src/StateManager.ts new file mode 100644 index 0000000..b54b489 --- /dev/null +++ b/src/StateManager.ts @@ -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; + } + } +} diff --git a/src/global/state.ts b/src/global/state.ts new file mode 100644 index 0000000..06c3f4d --- /dev/null +++ b/src/global/state.ts @@ -0,0 +1,3 @@ +import { StateManager } from "../StateManager.js"; + +export const applicationState = new StateManager();