buddy

node MVC discord bot
Log | Files | Refs | README

GuildDelete.js (1904B)


      1 'use strict';
      2 
      3 const Action = require('./Action');
      4 const { Events } = require('../../util/Constants');
      5 
      6 class GuildDeleteAction extends Action {
      7   constructor(client) {
      8     super(client);
      9     this.deleted = new Map();
     10   }
     11 
     12   handle(data) {
     13     const client = this.client;
     14 
     15     let guild = client.guilds.cache.get(data.id);
     16     if (guild) {
     17       for (const channel of guild.channels.cache.values()) {
     18         if (channel.type === 'text') channel.stopTyping(true);
     19       }
     20 
     21       if (data.unavailable) {
     22         // Guild is unavailable
     23         guild.available = false;
     24 
     25         /**
     26          * Emitted whenever a guild becomes unavailable, likely due to a server outage.
     27          * @event Client#guildUnavailable
     28          * @param {Guild} guild The guild that has become unavailable
     29          */
     30         client.emit(Events.GUILD_UNAVAILABLE, guild);
     31 
     32         // Stops the GuildDelete packet thinking a guild was actually deleted,
     33         // handles emitting of event itself
     34         return {
     35           guild: null,
     36         };
     37       }
     38 
     39       for (const channel of guild.channels.cache.values()) this.client.channels.remove(channel.id);
     40       if (guild.voice && guild.voice.connection) guild.voice.connection.disconnect();
     41 
     42       // Delete guild
     43       client.guilds.cache.delete(guild.id);
     44       guild.deleted = true;
     45 
     46       /**
     47        * Emitted whenever a guild kicks the client or the guild is deleted/left.
     48        * @event Client#guildDelete
     49        * @param {Guild} guild The guild that was deleted
     50        */
     51       client.emit(Events.GUILD_DELETE, guild);
     52 
     53       this.deleted.set(guild.id, guild);
     54       this.scheduleForDeletion(guild.id);
     55     } else {
     56       guild = this.deleted.get(data.id) || null;
     57     }
     58 
     59     return { guild };
     60   }
     61 
     62   scheduleForDeletion(id) {
     63     this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout);
     64   }
     65 }
     66 
     67 module.exports = GuildDeleteAction;