This commit is contained in:
Michel Fedde 2025-06-18 22:53:54 +02:00
parent 441715675c
commit a79898b2e9
48 changed files with 2062 additions and 1503 deletions

View file

@ -2,40 +2,54 @@ 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";
const commands: Set<Command> = new Set<Command>([
new HelloWorldCommand(),
new GroupCommand(),
new PlaydatesCommand()
new HelloWorldCommand(),
new GroupCommand(),
new PlaydatesCommand()
]);
export default class Commands {
private mappedCommands: Map<string, Command> = new Map<string, Command>();
constructor() {
this.mappedCommands = this.getMap();
private mappedCommands: Map<string, Command> = new Map<string, Command>();
private definitions: Nullable<RESTPostAPIChatInputApplicationCommandsJSONBody[]>;
constructor() {
this.mappedCommands = this.getMap();
}
public getCommand(commandName: string): CommandUnion | undefined {
if (!this.mappedCommands.has(commandName)) {
throw new Error(`Unknown command: ${commandName}`);
}
public getCommand(commandName: string): CommandUnion|undefined {
if (!this.mappedCommands.has(commandName)) {
throw new Error(`Unknown command: ${commandName}`);
}
return this.mappedCommands.get(commandName);
return this.mappedCommands.get(commandName);
}
public get allCommands(): Set<Command> {
return commands;
}
public getJsonDefinitions(): RESTPostAPIChatInputApplicationCommandsJSONBody[] {
if (this.definitions) {
return this.definitions;
}
public get allCommands(): Set<Command> {
return commands;
}
private getMap(): Map<string, Command>
{
const map = new Map<string, Command>();
for (const command of commands) {
const definition = command.definition()
map.set(definition.name, command);
}
return map;
this.definitions = [];
commands.forEach((command) => {
this.definitions?.push(command.definition().toJSON())
})
return this.definitions;
}
private getMap(): Map<string, Command> {
const map = new Map<string, Command>();
for (const command of commands) {
const definition = command.definition()
map.set(definition.name, command);
}
return map;
}
}