From fe3ff4f2b79bd67a9f32ecccb5f7a721263f18f3 Mon Sep 17 00:00:00 2001 From: Austin Keener Date: Mon, 18 Apr 2016 00:40:40 +0900 Subject: [PATCH] Actual v2.0.0 release. Added code to cleanup AudioManagers when guilds go unavailable. --- .../jda/exceptions/GuildUnavailableException.java | 7 ++++++- .../net/dv8tion/jda/handle/GuildLeaveHandler.java | 12 +++++------- .../dv8tion/jda/managers/impl/AudioManagerImpl.java | 4 ++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/exceptions/GuildUnavailableException.java b/src/main/java/net/dv8tion/jda/exceptions/GuildUnavailableException.java index f74ea33a6c..48edfa7f7c 100644 --- a/src/main/java/net/dv8tion/jda/exceptions/GuildUnavailableException.java +++ b/src/main/java/net/dv8tion/jda/exceptions/GuildUnavailableException.java @@ -19,6 +19,11 @@ public class GuildUnavailableException extends RuntimeException { public GuildUnavailableException() { - super("This operation is not possible due to the Guild being temporarily unavailable"); + this("This operation is not possible due to the Guild being temporarily unavailable"); + } + + public GuildUnavailableException(String reason) + { + super(reason); } } diff --git a/src/main/java/net/dv8tion/jda/handle/GuildLeaveHandler.java b/src/main/java/net/dv8tion/jda/handle/GuildLeaveHandler.java index bfad88bd50..3764fb3351 100644 --- a/src/main/java/net/dv8tion/jda/handle/GuildLeaveHandler.java +++ b/src/main/java/net/dv8tion/jda/handle/GuildLeaveHandler.java @@ -47,6 +47,9 @@ protected String handleInternally(JSONObject content) } Guild guild = api.getGuildMap().get(content.getString("id")); + AudioManager manager = api.getAudioManagersMap().get(guild); + if (manager != null) + manager.closeAudioConnection(); if (content.has("unavailable") && content.getBoolean("unavailable")) { ((GuildImpl) guild).setAvailable(false); @@ -56,13 +59,8 @@ protected String handleInternally(JSONObject content) return null; } - //We use this instead of getAudioManager(Guild) so we don't create a new instance. Efficiency! - AudioManager manager = api.getAudioManagersMap().get(guild); - if (manager != null && manager.isConnected() - && manager.getConnectedChannel().getGuild().getId().equals(guild.getId())) - { - manager.closeAudioConnection(); - } + if (manager != null) + api.getAudioManagersMap().remove(guild); //cleaning up all users that we do not share a guild with anymore List users = guild.getUsers(); diff --git a/src/main/java/net/dv8tion/jda/managers/impl/AudioManagerImpl.java b/src/main/java/net/dv8tion/jda/managers/impl/AudioManagerImpl.java index 7399ff257d..6c26d44cfd 100644 --- a/src/main/java/net/dv8tion/jda/managers/impl/AudioManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/managers/impl/AudioManagerImpl.java @@ -23,6 +23,7 @@ import net.dv8tion.jda.entities.Guild; import net.dv8tion.jda.entities.VoiceChannel; import net.dv8tion.jda.entities.impl.JDAImpl; +import net.dv8tion.jda.exceptions.GuildUnavailableException; import net.dv8tion.jda.managers.AudioManager; import net.dv8tion.jda.utils.NativeUtils; import net.dv8tion.jda.utils.ServiceUtil; @@ -67,6 +68,9 @@ public void openAudioConnection(VoiceChannel channel) if (queuedAudioConnection != null) throw new IllegalStateException("Already attempting to start an AudioConnection with a VoiceChannel!\n" + "Currently Attempting Channel ID: " + queuedAudioConnection.getId() + " | New Attempt Channel ID: " + channel.getId()); + if (!guild.isAvailable()) + throw new GuildUnavailableException("Cannot open an Audio Connection with an unavailable guild. " + + "Please wait until this Guild is available to open a connection."); queuedAudioConnection = channel; JSONObject obj = new JSONObject() .put("op", 4)