ghorsington 5 лет назад
Родитель
Сommit
2a48a170ba
1 измененных файлов с 56 добавлено и 0 удалено
  1. 56 0
      bot/src/model/command.ts

+ 56 - 0
bot/src/model/command.ts

@@ -0,0 +1,56 @@
+import { Message } from "discord.js";
+
+export interface CommandOptions {
+    pattern: string | RegExp;
+    documentation: string;
+    auth: boolean;
+};
+
+export type BotAction = (actionsDone: boolean, m : Message, content: string) => boolean | Promise<boolean>;
+
+export interface ICommand {
+    onStart?(): void | Promise<void>;
+    _botCommands?: IBotCommand[];
+    _botEvents?: { [action: number]: BotAction };
+    BOT_COMMAND?: string;
+};
+
+export interface IBotCommand extends CommandOptions {
+    action(message: Message, strippedContents: string, matches?: RegExpMatchArray) : void;
+};
+
+export const BOT_COMMAND_DESCRIPTOR = "BOT_COMMAND";
+export function CommandSet<T extends {new(...params: any[]): {}}>(base: T) {
+    return class extends base {
+        BOT_COMMAND = BOT_COMMAND_DESCRIPTOR;
+        _botCommands: IBotCommand[] = [];
+        _botEvents: { [action: number]: BotAction } = {};
+    } 
+}
+
+export enum ActionType {
+    MESSAGE,
+    INDIRECT_MENTION,
+    DIRECT_MENTION,
+    POST_MESSAGE
+}
+export function Action(type: ActionType) {
+    return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
+        if(target.BOT_COMMAND !== BOT_COMMAND_DESCRIPTOR)
+            throw "@Action is only usable on classes with @CommandSet!";
+
+        target._botEvents[type] = descriptor.value;
+    };
+}
+
+export function Command(opts: CommandOptions) {
+    return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
+        if(target.BOT_COMMAND !== BOT_COMMAND_DESCRIPTOR)
+            throw "@Command is only usable on classes with @CommandSet!";
+
+        target._botCommands.push(<IBotCommand>{
+            action: descriptor.value,
+            ...opts
+        });
+    };
+}