y21

Untitled

y21
May 25th, 2020
282
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const reactionHandlers: Map<string, ReactionHandler> = new Map();
  2. const messageHandlers: Map<string, MessageHandler> = new Map();
  3.  
  4. export type OnMessageMatchCallback = (
  5.   messageHandler: MessageHandler,
  6.   msg: discord.Message
  7. ) => any;
  8.  
  9. export type OnReactionMatchCallback = (
  10.   reactionHandler: ReactionHandler,
  11.   reaction: discord.Event.IMessageReactionAdd
  12. ) => any;
  13.  
  14. export type MessageFilterCallback = (
  15.   messageHandler: MessageHandler,
  16.   msg: discord.Message
  17. ) => Promise<boolean> | boolean;
  18.  
  19. export type ReactionFilterCallback = (
  20.   reactionHandler: ReactionHandler,
  21.   reaction: discord.Event.IMessageReactionAdd
  22. ) => Promise<boolean> | boolean;
  23.  
  24. export interface BaseHandlerData {
  25.   message: discord.Message;
  26. }
  27.  
  28. export class BaseHandler {
  29.   public message: discord.Message;
  30.  
  31.   constructor(data: BaseHandlerData) {
  32.     this.message = data.message;
  33.   }
  34.  
  35.   public done() {
  36.     throw new Error(
  37.       'This function must be called from a child class (MessageHandler, ReactionHandler)'
  38.     );
  39.   }
  40. }
  41.  
  42. export interface MessageHandlerData {
  43.   message: discord.Message;
  44.   filter: MessageFilterCallback;
  45.   onMatch: OnMessageMatchCallback;
  46. }
  47.  
  48. export class MessageHandler extends BaseHandler {
  49.   public filter: MessageFilterCallback;
  50.   public onMatch: OnMessageMatchCallback;
  51.  
  52.   constructor(data: MessageHandlerData) {
  53.     super({ message: data.message });
  54.     this.message = data.message;
  55.     this.filter = data.filter;
  56.     this.onMatch = data.onMatch;
  57.   }
  58.  
  59.   public done(): boolean {
  60.     return messageHandlers.delete(this.message.author!.id);
  61.   }
  62. }
  63.  
  64. export interface ReactionHandlerData {
  65.   message: discord.Message;
  66.   emojis: Array<discord.decor.Emojis | string>;
  67.   onMatch: OnReactionMatchCallback;
  68.   selfReact?: boolean;
  69. }
  70.  
  71. export class ReactionHandler extends BaseHandler {
  72.   public emojis: Array<discord.decor.Emojis | string>;
  73.   public onMatch: OnReactionMatchCallback;
  74.  
  75.   constructor(data: ReactionHandlerData) {
  76.     super({ message: data.message });
  77.     this.message = data.message;
  78.     this.emojis = data.emojis;
  79.     this.onMatch = data.onMatch;
  80.   }
  81.  
  82.   public done(): boolean {
  83.     return reactionHandlers.delete(this.message.id);
  84.   }
  85. }
  86.  
  87. export class Handler {
  88.   constructor() {
  89.     this.initEvents();
  90.   }
  91.  
  92.   private initEvents(): void {
  93.     discord.on(discord.Event.MESSAGE_REACTION_ADD, async (reaction) => {
  94.       const match = reactionHandlers.get(reaction.messageId);
  95.       if (!match) return;
  96.  
  97.       if (!match.emojis.some((e) => e === reaction.emoji.name)) return;
  98.  
  99.       await match.onMatch(match, reaction);
  100.     });
  101.  
  102.     discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
  103.       if (!message.author) return;
  104.  
  105.       const match = messageHandlers.get(message.author.id);
  106.       if (!match) return;
  107.  
  108.       const filterRes = await match.filter(match, message);
  109.       if (!filterRes) return; // filter did not return true
  110.  
  111.       await match.onMatch(match, message);
  112.     });
  113.   }
  114.  
  115.   public async createReactionHandler(
  116.     data: ReactionHandlerData
  117.   ): Promise<ReactionHandler> {
  118.     if (data.selfReact === undefined || data.selfReact) {
  119.       for (const emoji of data.emojis) {
  120.         await data.message.addReaction(emoji);
  121.       }
  122.     }
  123.     const reactionHandler = new ReactionHandler(data);
  124.     reactionHandlers.set(data.message.id, reactionHandler);
  125.     return reactionHandler;
  126.   }
  127.  
  128.   public async createMessageHandler(
  129.     data: MessageHandlerData
  130.   ): Promise<MessageHandler> {
  131.     const messageHandler = new MessageHandler(data);
  132.     messageHandlers.set(data.message.author!.id, messageHandler);
  133.     return messageHandler;
  134.   }
  135. }
RAW Paste Data