Skip to content

Commit

Permalink
Actual v2.0.0 release. Added code to cleanup AudioManagers when guild…
Browse files Browse the repository at this point in the history
…s go unavailable.
  • Loading branch information
DV8FromTheWorld committed Apr 17, 2016
1 parent 937d57e commit fe3ff4f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
12 changes: 5 additions & 7 deletions src/main/java/net/dv8tion/jda/handle/GuildLeaveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<User> users = guild.getUsers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fe3ff4f

Please sign in to comment.