buddy

node MVC discord bot
Log | Files | Refs | README

Action.js (2284B)


      1 'use strict';
      2 
      3 const { PartialTypes } = require('../../util/Constants');
      4 
      5 /*
      6 
      7 ABOUT ACTIONS
      8 
      9 Actions are similar to WebSocket Packet Handlers, but since introducing
     10 the REST API methods, in order to prevent rewriting code to handle data,
     11 "actions" have been introduced. They're basically what Packet Handlers
     12 used to be but they're strictly for manipulating data and making sure
     13 that WebSocket events don't clash with REST methods.
     14 
     15 */
     16 
     17 class GenericAction {
     18   constructor(client) {
     19     this.client = client;
     20   }
     21 
     22   handle(data) {
     23     return data;
     24   }
     25 
     26   getPayload(data, manager, id, partialType, cache) {
     27     const existing = manager.cache.get(id);
     28     if (!existing && this.client.options.partials.includes(partialType)) {
     29       return manager.add(data, cache);
     30     }
     31     return existing;
     32   }
     33 
     34   getChannel(data) {
     35     const id = data.channel_id || data.id;
     36     return (
     37       data.channel ||
     38       this.getPayload(
     39         {
     40           id,
     41           guild_id: data.guild_id,
     42           recipients: [data.author || { id: data.user_id }],
     43         },
     44         this.client.channels,
     45         id,
     46         PartialTypes.CHANNEL,
     47       )
     48     );
     49   }
     50 
     51   getMessage(data, channel, cache) {
     52     const id = data.message_id || data.id;
     53     return (
     54       data.message ||
     55       this.getPayload(
     56         {
     57           id,
     58           channel_id: channel.id,
     59           guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
     60         },
     61         channel.messages,
     62         id,
     63         PartialTypes.MESSAGE,
     64         cache,
     65       )
     66     );
     67   }
     68 
     69   getReaction(data, message, user) {
     70     const id = data.emoji.id || decodeURIComponent(data.emoji.name);
     71     return this.getPayload(
     72       {
     73         emoji: data.emoji,
     74         count: message.partial ? null : 0,
     75         me: user ? user.id === this.client.user.id : false,
     76       },
     77       message.reactions,
     78       id,
     79       PartialTypes.REACTION,
     80     );
     81   }
     82 
     83   getMember(data, guild) {
     84     const id = data.user.id;
     85     return this.getPayload(
     86       {
     87         user: {
     88           id,
     89         },
     90       },
     91       guild.members,
     92       id,
     93       PartialTypes.GUILD_MEMBER,
     94     );
     95   }
     96 
     97   getUser(data) {
     98     const id = data.user_id;
     99     return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
    100   }
    101 }
    102 
    103 module.exports = GenericAction;