34 lines
973 B
TypeScript
34 lines
973 B
TypeScript
import type { Client, Message } from "discord.js";
|
|
import { readdir } from "fs/promises";
|
|
|
|
const CMD_PREFIX = ".";
|
|
|
|
export interface Command {
|
|
name: string,
|
|
description: string,
|
|
execute: (message: Message) => void | Promise<void>
|
|
}
|
|
|
|
export class CommandService {
|
|
private client: Client
|
|
private registry: Record<string, Command> = {}
|
|
|
|
constructor(client: Client) {
|
|
this.client = client;
|
|
}
|
|
|
|
async init() {
|
|
const commands = await Promise.all(
|
|
(await readdir(`${import.meta.dir}/registry`)).map(async file => (await import(`./registry/${file}`)).default as Command)
|
|
);
|
|
for (const command of commands)
|
|
this.registry[command.name] = command;
|
|
};
|
|
|
|
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 ?? ""];
|
|
}
|
|
}
|