diff options
author | Brandon <bwaggone@umich.edu> | 2018-04-14 13:43:39 -0400 |
---|---|---|
committer | Brandon <bwaggone@umich.edu> | 2018-04-14 13:43:39 -0400 |
commit | 3bc325f7d249cd25002f32ba8b516c7644d8c506 (patch) | |
tree | 681dc836617447d21a9376b702d54824d277047a | |
parent | fadf16cd3186406264dc974cea23d9aaa798757e (diff) | |
download | SoundChan-3bc325f7d249cd25002f32ba8b516c7644d8c506.tar.gz SoundChan-3bc325f7d249cd25002f32ba8b516c7644d8c506.tar.bz2 SoundChan-3bc325f7d249cd25002f32ba8b516c7644d8c506.zip |
Split up commands to enum
-rw-r--r-- | src/main/java/soundchan/BotListener.java | 68 | ||||
-rw-r--r-- | src/main/java/soundchan/BotListenerHelpers.java | 20 | ||||
-rw-r--r-- | src/main/java/soundchan/Commands.java | 14 |
3 files changed, 74 insertions, 28 deletions
diff --git a/src/main/java/soundchan/BotListener.java b/src/main/java/soundchan/BotListener.java index d1d1d81..8e43ca9 100644 --- a/src/main/java/soundchan/BotListener.java +++ b/src/main/java/soundchan/BotListener.java @@ -77,17 +77,12 @@ public class BotListener extends ListenerAdapter{ @Override public void onMessageReceived(MessageReceivedEvent event) { String[] command = event.getMessage().getContentRaw().split(" ", 2); + BotListenerHelpers helper = new BotListenerHelpers(); + Guild guild = event.getGuild(); - MessageChannel channel = null; - - // This means SoundChan was DM'd - if (guild == null){ - channel = event.getPrivateChannel(); - }else{ - // This means SoundChan was referred to in a TextChannel - channel = event.getTextChannel(); - } + MessageChannel channel = helper.GetReplyChannel(event); + // If we haven't set the Monitored Guild yet, set the value if(monitoredGuildId == -1 && guild != null){ monitoredGuildId = Long.parseLong(guild.getId()); monitoredGuild = guild; @@ -106,26 +101,43 @@ public class BotListener extends ListenerAdapter{ } } - // "~" Signifies that you're looking to play a song/sound from a url - if(command[0].startsWith("~") && command[0].length() > 1){ - if ("~play".equals(command[0]) && command.length == 2) { - loadAndPlay(channel, command[1]); - } else if ("~skip".equals(command[0])) { - skipTrack(channel); - } else if ("~volume".equals(command[0]) && command.length == 2) { - changeVolume(channel, command[1]); - } else if ("~pause".equals(command[0])) { - pauseTrack(channel); - } else if ("~unpause".equals(command[0])) { - unpauseTrack(channel); - } else if ("~list".equals(command[0])) { - if(command.length == 2){ - if(command[1].equals("queue")){ - listTracks(channel); - } - else if(command[1].equals("sounds")){ - localManager.ListSounds(channel); + if(command[0].startsWith("~")){ + switch (Commands.valueOf(command[0].substring(1))){ + case play: { + if(command.length == 2) + loadAndPlay(channel, command[1]); + break; + } + case skip: { + skipTrack(channel); + break; + } + case volume: { + if(command.length == 2) + changeVolume(channel, command[1]); + break; + } + case list: { + if(command.length == 2){ + if(command[1].equals("queue")){ + listTracks(channel); + } + else if(command[1].equals("sounds")){ + localManager.ListSounds(channel); + } } + break; + } + case pause: { + pauseTrack(channel); + break; + } + case unpause: { + unpauseTrack(channel); + break; + } + case playingnow: { + break; } } } diff --git a/src/main/java/soundchan/BotListenerHelpers.java b/src/main/java/soundchan/BotListenerHelpers.java new file mode 100644 index 0000000..600e4b0 --- /dev/null +++ b/src/main/java/soundchan/BotListenerHelpers.java @@ -0,0 +1,20 @@ +package soundchan; + +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.MessageChannel; +import net.dv8tion.jda.core.events.message.MessageReceivedEvent; + +public class BotListenerHelpers { + + /** + * Given a MessageReceivedEvent, determines the correct reply channel for SoundChan. + * @param event a MessageReceivedEvent from the JDA ListenerAdapter + * @return Either a PrivateChannel (if SoundChan was DM'd) or a TextChannel (If SoundChan was commanded in one) + */ + public MessageChannel GetReplyChannel(MessageReceivedEvent event){ + Guild guild = event.getGuild(); + return (guild == null) ? event.getPrivateChannel() : event.getTextChannel(); + } + + +} diff --git a/src/main/java/soundchan/Commands.java b/src/main/java/soundchan/Commands.java new file mode 100644 index 0000000..3f96d37 --- /dev/null +++ b/src/main/java/soundchan/Commands.java @@ -0,0 +1,14 @@ +package soundchan; + +/** + * An enum representing the possible commands SoundChan might receive + */ +public enum Commands { + play, + volume, + pause, + unpause, + skip, + list, + playingnow +} |