26 lines
925 B
TypeScript
26 lines
925 B
TypeScript
import type { Message } from "discord.js";
|
|
import { readdir } from "fs/promises";
|
|
import { dirname } from "path";
|
|
import type { Command } from "../models";
|
|
|
|
const CMD_PREFIX = ".";
|
|
const CMD_REGISTRY_DIR = `${dirname(Bun.main)}/cmd/registry`;
|
|
|
|
export default abstract class CommandService {
|
|
private static registry: Record<string, Command> = {}
|
|
|
|
static async init() {
|
|
const commands = await Promise.all(
|
|
(await readdir(CMD_REGISTRY_DIR)).map(async file => (await import(`${CMD_REGISTRY_DIR}/${file}`)).default satisfies Command)
|
|
);
|
|
for (const command of commands)
|
|
CommandService.registry[command.name] = command;
|
|
};
|
|
|
|
static parse(message: Message): Command | undefined {
|
|
if (!message.content.startsWith(CMD_PREFIX)) return;
|
|
const name = message.content.slice(CMD_PREFIX.length).split(/\s+/)[0];
|
|
return this.registry[name ?? ""];
|
|
}
|
|
}
|