import {HelloWorldCommand} from "./HelloWorldCommand"; import {Command, CommandUnion} from "./Command"; import {GroupCommand} from "./Groups"; import {PlaydatesCommand} from "./Playdates"; import {RESTPostAPIChatInputApplicationCommandsJSONBody} from "discord.js"; import {Nullable} from "../../types/Nullable"; import {ServerCommand} from "./Server"; const commands: Set = new Set([ new HelloWorldCommand(), new GroupCommand(), new PlaydatesCommand(), new ServerCommand() ]); export default class Commands { private mappedCommands: Map = new Map(); private definitions: Nullable; constructor() { this.mappedCommands = this.getMap(); } public getCommand(commandName: string): CommandUnion | undefined { if (!this.mappedCommands.has(commandName)) { throw new Error(`Unknown command: ${commandName}`); } return this.mappedCommands.get(commandName); } public get allCommands(): Set { return commands; } public getJsonDefinitions(): RESTPostAPIChatInputApplicationCommandsJSONBody[] { if (this.definitions) { return this.definitions; } this.definitions = []; commands.forEach((command) => { this.definitions?.push(command.definition().toJSON()) }) return this.definitions; } private getMap(): Map { const map = new Map(); for (const command of commands) { const definition = command.definition() map.set(definition.name, command); } return map; } }