command.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Message } from "discord.js";
  2. export interface CommandDocumentation {
  3. description: string;
  4. example: string;
  5. }
  6. export interface CommandOptions {
  7. pattern: string | RegExp;
  8. documentation?: CommandDocumentation;
  9. auth?: boolean;
  10. };
  11. export type BotAction = (actionsDone: boolean, m : Message, content: string) => boolean | Promise<boolean>;
  12. export interface ICommand {
  13. onStart?(): void | Promise<void>;
  14. _botCommands?: IBotCommand[];
  15. _botEvents?: { [action in ActionType]?: BotAction };
  16. BOT_COMMAND?: string;
  17. };
  18. export interface IBotCommand extends CommandOptions {
  19. action(message: Message, strippedContents: string, matches?: RegExpMatchArray) : void;
  20. };
  21. export const BOT_COMMAND_DESCRIPTOR = "BOT_COMMAND";
  22. export function CommandSet<T extends {new(...params: any[]): {}}>(base: T) {
  23. base.prototype.BOT_COMMAND = BOT_COMMAND_DESCRIPTOR;
  24. }
  25. export enum ActionType {
  26. MESSAGE,
  27. INDIRECT_MENTION,
  28. DIRECT_MENTION,
  29. POST_MESSAGE
  30. }
  31. export function Action(type: ActionType) {
  32. return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  33. if(!target._botEvents)
  34. target._botEvents= {};
  35. target._botEvents[type] = descriptor.value;
  36. };
  37. }
  38. export function Command(opts: CommandOptions) {
  39. return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  40. if(!target._botCommands)
  41. target._botCommands = [];
  42. target._botCommands.push(<IBotCommand>{
  43. action: descriptor.value,
  44. ...opts
  45. });
  46. };
  47. }