diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2019-05-04 19:27:40 -0400 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2019-05-04 19:27:40 -0400 |
commit | ab1fe085c2da8083fc083f4452026f6d2eec30b3 (patch) | |
tree | f086bb1f40ec994ab8d6d349e06117826cef5214 /src/main | |
parent | ffe6f521c4525479d1c1fb19f1e2b06cc955ee75 (diff) | |
download | SoundChan-ab1fe085c2da8083fc083f4452026f6d2eec30b3.tar.gz SoundChan-ab1fe085c2da8083fc083f4452026f6d2eec30b3.tar.bz2 SoundChan-ab1fe085c2da8083fc083f4452026f6d2eec30b3.zip |
Adding in bits needed to clean up when people leave channelfeature/auto-leave-channel
SoundChan will leave a channel after 10 seconds if it is the only member
left. When it leaves it will empty the queue of audio to play.
When SoundChan is the last member in a channel, it will pause any
playing audio.
New command to drop play queue and stop current audio.
Diffstat (limited to 'src/main')
-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) |