buddy

node MVC discord bot
Log | Files | Refs | README

VoiceStateUpdate.js (1526B)


      1 'use strict';
      2 
      3 const Action = require('./Action');
      4 const VoiceState = require('../../structures/VoiceState');
      5 const { Events } = require('../../util/Constants');
      6 
      7 class VoiceStateUpdate extends Action {
      8   handle(data) {
      9     const client = this.client;
     10     const guild = client.guilds.cache.get(data.guild_id);
     11     if (guild) {
     12       // Update the state
     13       const oldState = guild.voiceStates.cache.has(data.user_id)
     14         ? guild.voiceStates.cache.get(data.user_id)._clone()
     15         : new VoiceState(guild, { user_id: data.user_id });
     16 
     17       const newState = guild.voiceStates.add(data);
     18 
     19       // Get the member
     20       let member = guild.members.cache.get(data.user_id);
     21       if (member && data.member) {
     22         member._patch(data.member);
     23       } else if (data.member && data.member.user && data.member.joined_at) {
     24         member = guild.members.add(data.member);
     25       }
     26 
     27       // Emit event
     28       if (member && member.user.id === client.user.id) {
     29         client.emit('debug', `[VOICE] received voice state update: ${JSON.stringify(data)}`);
     30         client.voice.onVoiceStateUpdate(data);
     31       }
     32 
     33       /**
     34        * Emitted whenever a member changes voice state - e.g. joins/leaves a channel, mutes/unmutes.
     35        * @event Client#voiceStateUpdate
     36        * @param {VoiceState} oldState The voice state before the update
     37        * @param {VoiceState} newState The voice state after the update
     38        */
     39       client.emit(Events.VOICE_STATE_UPDATE, oldState, newState);
     40     }
     41   }
     42 }
     43 
     44 module.exports = VoiceStateUpdate;