diff options
author | Brandon <bwaggone@umich.edu> | 2019-08-10 21:49:20 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-10 21:49:20 -0400 |
commit | 5ac0ded5bfcfcd00e5385bc78c9ce24ba604f04a (patch) | |
tree | f086bb1f40ec994ab8d6d349e06117826cef5214 | |
parent | ffe6f521c4525479d1c1fb19f1e2b06cc955ee75 (diff) | |
parent | ab1fe085c2da8083fc083f4452026f6d2eec30b3 (diff) | |
download | SoundChan-5ac0ded5bfcfcd00e5385bc78c9ce24ba604f04a.tar.gz SoundChan-5ac0ded5bfcfcd00e5385bc78c9ce24ba604f04a.tar.bz2 SoundChan-5ac0ded5bfcfcd00e5385bc78c9ce24ba604f04a.zip |
Merge pull request #14 from bwaggone/feature/auto-leave-channel
Adding in bits needed to clean up when people leave channel
-rw-r--r-- | src/main/java/soundchan/BotListener/BotListener.java | 36 | ||||
-rw-r--r-- | src/main/java/soundchan/BotListener/Commands.java | 3 | ||||
-rw-r--r-- | src/main/java/soundchan/TrackScheduler.java | 8 |
3 files changed, 46 insertions, 1 deletions
diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index b8b4bad..4864ae2 100644 --- a/src/main/java/soundchan/BotListener/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -13,6 +13,7 @@ import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.MessageChannel; import net.dv8tion.jda.core.entities.VoiceChannel; import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent; +import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent; import net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.hooks.ListenerAdapter; @@ -35,6 +36,8 @@ public class BotListener extends ListenerAdapter{ private final Map<Long, GuildMusicManager> musicManagers; private BotListenerHelpers helper = new BotListenerHelpers(); private Map<String, Future<?> > otherTasks; + private boolean hasAudience; + private Timer timer; // From configuration file private static String followingUser; @@ -48,6 +51,8 @@ public class BotListener extends ListenerAdapter{ AudioSourceManagers.registerRemoteSources(playerManager); AudioSourceManagers.registerLocalSource(playerManager); + hasAudience = false; + timer = new Timer(); loadProperties(properties); } @@ -151,6 +156,7 @@ public class BotListener extends ListenerAdapter{ }); } } + hasAudience = true; super.onGuildVoiceJoin(event); } @@ -167,6 +173,25 @@ public class BotListener extends ListenerAdapter{ } @Override + public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) { + if(event.getChannelLeft().getMembers().size() == 1) { // If only member in chat is SoundChan + hasAudience = false; + getGuildAudioPlayer().player.setPaused(true); + timer.schedule(new TimerTask() { + @Override + public void run() { + if(!hasAudience) { + clearQueue(); + AudioManager audioManager = monitoredGuild.getAudioManager(); + audioManager.closeAudioConnection(); + } + } + }, 10000); // Wait 10 seconds before leaving channel + } + super.onGuildVoiceLeave(event); + } + + @Override public void onMessageReceived(MessageReceivedEvent event) { String[] command = event.getMessage().getContentRaw().split(" ", 2); @@ -236,9 +261,13 @@ public class BotListener extends ListenerAdapter{ // Print currently playing song with extra info printCurrentlyPlaying(channel, true); }else if(enumCommand == Commands.summon){ + hasAudience = true; connectToUserVoiceChannel(monitoredGuild.getAudioManager(), event.getMember().getEffectiveName()); }else if(enumCommand == Commands.help){ help(channel); + }else if(enumCommand == Commands.dropqueue) { + clearQueue(); + channel.sendMessage("Queue has been cleared").queue(); } } @@ -471,4 +500,11 @@ public class BotListener extends ListenerAdapter{ executorService.shutdown(); } + /** + * Empty out the queue of things to play and stops currently playing audio + */ + private void clearQueue() { + getGuildAudioPlayer().scheduler.emptyQueue(); + } + } diff --git a/src/main/java/soundchan/BotListener/Commands.java b/src/main/java/soundchan/BotListener/Commands.java index 046bcab..2295fda 100644 --- a/src/main/java/soundchan/BotListener/Commands.java +++ b/src/main/java/soundchan/BotListener/Commands.java @@ -13,5 +13,6 @@ public enum Commands { playingnow, status, summon, - help + help, + dropqueue } diff --git a/src/main/java/soundchan/TrackScheduler.java b/src/main/java/soundchan/TrackScheduler.java index cf7b1f8..7c14ebc 100644 --- a/src/main/java/soundchan/TrackScheduler.java +++ b/src/main/java/soundchan/TrackScheduler.java @@ -80,6 +80,14 @@ public class TrackScheduler extends AudioEventAdapter { player.startTrack(queue.poll(), false); } + /** + * Cleans out the queue of tracks and stops any playing track + */ + public void emptyQueue() { + player.stopTrack(); + queue.clear(); + } + @Override public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) { // Only start the next track if the end reason is suitable for it (FINISHED or LOAD_FAILED) |