buddy

node MVC discord bot
Log | Files | Refs | README

MessageReactionAdd.js (1433B)


      1 'use strict';
      2 
      3 const Action = require('./Action');
      4 const { Events } = require('../../util/Constants');
      5 const { PartialTypes } = require('../../util/Constants');
      6 
      7 /*
      8 { user_id: 'id',
      9      message_id: 'id',
     10      emoji: { name: '�', id: null },
     11      channel_id: 'id' } }
     12 */
     13 
     14 class MessageReactionAdd extends Action {
     15   handle(data) {
     16     if (!data.emoji) return false;
     17 
     18     const user = this.getUser(data);
     19     if (!user) return false;
     20 
     21     // Verify channel
     22     const channel = this.getChannel(data);
     23     if (!channel || channel.type === 'voice') return false;
     24 
     25     // Verify message
     26     const message = this.getMessage(data, channel);
     27     if (!message) return false;
     28 
     29     // Verify reaction
     30     if (message.partial && !this.client.options.partials.includes(PartialTypes.REACTION)) return false;
     31     const reaction = message.reactions.add({
     32       emoji: data.emoji,
     33       count: message.partial ? null : 0,
     34       me: user.id === this.client.user.id,
     35     });
     36     if (!reaction) return false;
     37     reaction._add(user);
     38     /**
     39      * Emitted whenever a reaction is added to a cached message.
     40      * @event Client#messageReactionAdd
     41      * @param {MessageReaction} messageReaction The reaction object
     42      * @param {User} user The user that applied the guild or reaction emoji
     43      */
     44     this.client.emit(Events.MESSAGE_REACTION_ADD, reaction, user);
     45 
     46     return { message, reaction, user };
     47   }
     48 }
     49 
     50 module.exports = MessageReactionAdd;