From 17591cd6a7c382eb5bc3a0d922359157ff2864e5 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Fri, 19 Nov 2021 19:55:10 +0000 Subject: [PATCH] Added leave command. --- src/main/java/moe/nekojimi/chords/Main.java | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 98b8289..f378919 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -41,6 +41,8 @@ public class Main extends ListenerAdapter private final Searcher searcher; private JDA jda; + private VoiceChannel currentVoiceChannel = null; + private List lastSearchResults; private int trackNumber = 1; @@ -127,6 +129,8 @@ public class Main extends ListenerAdapter if (content.startsWith("!join ")) onJoinCommand(event, guild, arg); + else if (content.equals("!leave")) + onLeaveCommand(event); else if (content.equals("!join")) onJoinCommand(event); else if (content.startsWith("!play ")) @@ -201,6 +205,15 @@ public class Main extends ListenerAdapter onConnecting(channel, textChannel); // Let the user know, we were successful! } + private void onLeaveCommand(GuildMessageReceivedEvent event) + { + if (currentVoiceChannel != null) + { + disconnect(); + + } + } + private void onPlayCommand(GuildMessageReceivedEvent event, Guild guild, String arg) { try @@ -384,7 +397,9 @@ public class Main extends ListenerAdapter { String help = "Commands available:\n" + "!join - Joins a voice channel\n" + + "!leave - Leaves the current voice channel\n" + "!play - Downloads the track at that URL and adds it to the queue.\n" + + "!play - Searches for a track and displays a selection menu.\n" + "!queue - Show the songs currently playing and the current queue.\n" + "!remove - Remove the song at position from the queue.\n" + "!skip - Skip the current song and play the next one.\n" @@ -442,6 +457,20 @@ public class Main extends ListenerAdapter // audioManager.setReceivingHandler(handler); // Connect to the voice channel audioManager.openAudioConnection(channel); + currentVoiceChannel = channel; + } + + private void disconnect() + { + if (currentVoiceChannel != null) + { + Guild guild = currentVoiceChannel.getGuild(); + AudioManager audioManager = guild.getAudioManager(); + audioManager.setSendingHandler(null); + audioManager.closeAudioConnection(); + musicHandler = null; + currentVoiceChannel = null; + } } }