From ab1fe085c2da8083fc083f4452026f6d2eec30b3 Mon Sep 17 00:00:00 2001
From: Matt Kohls <mattkohls13@gmail.com>
Date: Sat, 4 May 2019 19:27:40 -0400
Subject: Adding in bits needed to clean up when people 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.
---
 .../java/soundchan/BotListener/BotListener.java    | 36 ++++++++++++++++++++++
 src/main/java/soundchan/BotListener/Commands.java  |  3 +-
 src/main/java/soundchan/TrackScheduler.java        |  8 +++++
 3 files changed, 46 insertions(+), 1 deletion(-)

(limited to 'src')

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);
     }
 
@@ -166,6 +172,25 @@ public class BotListener extends ListenerAdapter{
         super.onGuildVoiceMove(event);
     }
 
+    @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)
-- 
cgit v1.2.3