buddy

node MVC discord bot
Log | Files | Refs | README

PresenceUpdate.js (1518B)


      1 'use strict';
      2 
      3 const Action = require('./Action');
      4 const { Events } = require('../../util/Constants');
      5 
      6 class PresenceUpdateAction extends Action {
      7   handle(data) {
      8     let user = this.client.users.cache.get(data.user.id);
      9     if (!user && data.user.username) user = this.client.users.add(data.user);
     10     if (!user) return;
     11 
     12     if (data.user && data.user.username) {
     13       if (!user.equals(data.user)) this.client.actions.UserUpdate.handle(data.user);
     14     }
     15 
     16     const guild = this.client.guilds.cache.get(data.guild_id);
     17     if (!guild) return;
     18 
     19     let oldPresence = guild.presences.cache.get(user.id);
     20     if (oldPresence) oldPresence = oldPresence._clone();
     21     let member = guild.members.cache.get(user.id);
     22     if (!member && data.status !== 'offline') {
     23       member = guild.members.add({
     24         user,
     25         roles: data.roles,
     26         deaf: false,
     27         mute: false,
     28       });
     29       this.client.emit(Events.GUILD_MEMBER_AVAILABLE, member);
     30     }
     31     guild.presences.add(Object.assign(data, { guild }));
     32     if (member && this.client.listenerCount(Events.PRESENCE_UPDATE)) {
     33       /**
     34        * Emitted whenever a guild member's presence (e.g. status, activity) is changed.
     35        * @event Client#presenceUpdate
     36        * @param {?Presence} oldPresence The presence before the update, if one at all
     37        * @param {Presence} newPresence The presence after the update
     38        */
     39       this.client.emit(Events.PRESENCE_UPDATE, oldPresence, member.presence);
     40     }
     41   }
     42 }
     43 
     44 module.exports = PresenceUpdateAction;