Search in sources :

Example 1 with GuildUnavailableEvent

use of net.dv8tion.jda.api.events.guild.GuildUnavailableEvent in project JDA by DV8FromTheWorld.

the class GuildDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("id");
    GuildSetupController setupController = getJDA().getGuildSetupController();
    boolean wasInit = setupController.onDelete(id, content);
    if (wasInit || setupController.isUnavailable(id))
        return null;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    boolean unavailable = content.getBoolean("unavailable");
    if (guild == null) {
        // getJDA().getEventCache().cache(EventCache.Type.GUILD, id, () -> handle(responseNumber, allContent));
        WebSocketClient.LOG.debug("Received GUILD_DELETE for a Guild that is not currently cached. ID: {} unavailable: {}", id, unavailable);
        return null;
    }
    // ignore the event
    if (setupController.isUnavailable(id) && unavailable)
        return null;
    // Remove everything from global cache
    // this prevents some race-conditions for getting audio managers from guilds
    SnowflakeCacheViewImpl<Guild> guildView = getJDA().getGuildsView();
    SnowflakeCacheViewImpl<StageChannel> stageView = getJDA().getStageChannelView();
    SnowflakeCacheViewImpl<TextChannel> textView = getJDA().getTextChannelsView();
    SnowflakeCacheViewImpl<ThreadChannel> threadView = getJDA().getThreadChannelsView();
    SnowflakeCacheViewImpl<NewsChannel> newsView = getJDA().getNewsChannelView();
    SnowflakeCacheViewImpl<VoiceChannel> voiceView = getJDA().getVoiceChannelsView();
    SnowflakeCacheViewImpl<Category> categoryView = getJDA().getCategoriesView();
    guildView.remove(id);
    try (UnlockHook hook = stageView.writeLock()) {
        guild.getStageChannelCache().forEachUnordered(chan -> stageView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = textView.writeLock()) {
        guild.getTextChannelCache().forEachUnordered(chan -> textView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = threadView.writeLock()) {
        guild.getThreadChannelsView().forEachUnordered(chan -> threadView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = newsView.writeLock()) {
        guild.getNewsChannelCache().forEachUnordered(chan -> newsView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = voiceView.writeLock()) {
        guild.getVoiceChannelCache().forEachUnordered(chan -> voiceView.getMap().remove(chan.getIdLong()));
    }
    try (UnlockHook hook = categoryView.writeLock()) {
        guild.getCategoryCache().forEachUnordered(chan -> categoryView.getMap().remove(chan.getIdLong()));
    }
    // Clear audio connection
    getJDA().getClient().removeAudioConnection(id);
    final AbstractCacheView<AudioManager> audioManagerView = getJDA().getAudioManagersView();
    // read-lock access/release
    final AudioManagerImpl manager = (AudioManagerImpl) audioManagerView.get(id);
    if (manager != null)
        // connection-lock access/release
        manager.closeAudioConnection(ConnectionStatus.DISCONNECTED_REMOVED_FROM_GUILD);
    // write-lock access/release
    audioManagerView.remove(id);
    // cleaning up all users that we do not share a guild with anymore
    // Anything left in memberIds will be removed from the main userMap
    // Use a new HashSet so that we don't actually modify the Member map so it doesn't affect Guild#getMembers for the leave event.
    // copies keys
    TLongSet memberIds = guild.getMembersView().keySet();
    getJDA().getGuildCache().stream().map(GuildImpl.class::cast).forEach(g -> memberIds.removeAll(g.getMembersView().keySet()));
    // Remember, everything left in memberIds is removed from the userMap
    SnowflakeCacheViewImpl<User> userView = getJDA().getUsersView();
    try (UnlockHook hook = userView.writeLock()) {
        long selfId = getJDA().getSelfUser().getIdLong();
        memberIds.forEach(memberId -> {
            if (memberId == selfId)
                // don't remove selfUser from cache
                return true;
            userView.remove(memberId);
            getJDA().getEventCache().clear(EventCache.Type.USER, memberId);
            return true;
        });
    }
    if (unavailable) {
        setupController.onUnavailable(id);
        getJDA().handleEvent(new GuildUnavailableEvent(getJDA(), responseNumber, guild));
    } else {
        getJDA().handleEvent(new GuildLeaveEvent(getJDA(), responseNumber, guild));
    }
    getJDA().getEventCache().clear(EventCache.Type.GUILD, id);
    return null;
}
Also used : GuildLeaveEvent(net.dv8tion.jda.api.events.guild.GuildLeaveEvent) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildUnavailableEvent(net.dv8tion.jda.api.events.guild.GuildUnavailableEvent) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) TLongSet(gnu.trove.set.TLongSet)

Aggregations

TLongSet (gnu.trove.set.TLongSet)1 GuildLeaveEvent (net.dv8tion.jda.api.events.guild.GuildLeaveEvent)1 GuildUnavailableEvent (net.dv8tion.jda.api.events.guild.GuildUnavailableEvent)1 AudioManager (net.dv8tion.jda.api.managers.AudioManager)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 AudioManagerImpl (net.dv8tion.jda.internal.managers.AudioManagerImpl)1 UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)1