diff options
| author | Matt Kohls <mattkohls13@gmail.com> | 2019-11-10 14:56:57 -0500 | 
|---|---|---|
| committer | Matt Kohls <mattkohls13@gmail.com> | 2019-11-10 14:56:57 -0500 | 
| commit | e0bf4679553065ee76b6250f28c268bc804e6fdc (patch) | |
| tree | 2c1be2683b5a17054cdad965714e3693238edc8d | |
| parent | c50216132981a993fb827315c7c4d6fb3d5857a1 (diff) | |
| download | SoundChan-e0bf4679553065ee76b6250f28c268bc804e6fdc.tar.gz SoundChan-e0bf4679553065ee76b6250f28c268bc804e6fdc.tar.bz2 SoundChan-e0bf4679553065ee76b6250f28c268bc804e6fdc.zip  | |
Having methods that get called from an event make sure MonitoredGuild is
set
| -rw-r--r-- | src/main/java/soundchan/BotListener/BotListener.java | 28 | ||||
| -rw-r--r-- | src/main/java/soundchan/BotListener/BotListenerHelpers.java | 5 | 
2 files changed, 25 insertions, 8 deletions
diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index 0bd920d..1e84a54 100644 --- a/src/main/java/soundchan/BotListener/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -14,6 +14,7 @@ import net.dv8tion.jda.api.entities.VoiceChannel;  import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;  import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;  import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent; +import net.dv8tion.jda.api.events.message.GenericMessageEvent;  import net.dv8tion.jda.api.events.message.MessageReceivedEvent;  import net.dv8tion.jda.api.hooks.ListenerAdapter;  import net.dv8tion.jda.api.managers.AudioManager; @@ -21,7 +22,6 @@ import org.jetbrains.annotations.NotNull;  import soundchan.*;  import java.nio.file.WatchEvent; -import java.sql.SQLOutput;  import java.util.*;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors; @@ -95,6 +95,18 @@ public class BotListener extends ListenerAdapter{      } +    /** +     * Sets the monitored guild to the given guild if SoundChan doesn't already have a guild set +     * @param guild The guild to monitor if not monitoring anything at the moment +     */ +    private void setMonitoredGuild(Guild guild) { +        // If we haven't set the Monitored Guild yet, set the value +        if(monitoredGuildId == -1 && guild != null){ +            monitoredGuildId = Long.parseLong(guild.getId()); +            monitoredGuild = guild; +        } +    } +      private synchronized GuildMusicManager getGuildAudioPlayer() {          long guildId = monitoredGuildId;          GuildMusicManager musicManager = musicManagers.get(guildId); @@ -117,6 +129,8 @@ public class BotListener extends ListenerAdapter{      @Override      public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {          if(audioOnUserJoin) { +            Guild guild = event.getGuild(); +            setMonitoredGuild(guild);              String filepath = localManager.GetFilePath(event.getMember().getEffectiveName());              if (!filepath.contentEquals("")) {                  GuildMusicManager musicManager = getGuildAudioPlayer(); @@ -159,6 +173,8 @@ public class BotListener extends ListenerAdapter{      @Override      public void onGuildVoiceMove(GuildVoiceMoveEvent event) {          if(event.getMember().getEffectiveName().compareTo(followingUser) == 0) { +            Guild guild = event.getGuild(); +            setMonitoredGuild(guild);              AudioManager audioManager = monitoredGuild.getAudioManager();              if(!audioManager.isAttemptingToConnect()) {                  audioManager.openAudioConnection(event.getChannelJoined()); @@ -170,6 +186,8 @@ public class BotListener extends ListenerAdapter{      @Override      public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {          if(event.getChannelLeft().getMembers().size() == 1) {   // If only member in chat is SoundChan +            Guild guild = event.getGuild(); +            setMonitoredGuild(guild);              hasAudience = false;              getGuildAudioPlayer().player.setPaused(true);              timer.schedule(new TimerTask() { @@ -194,13 +212,9 @@ public class BotListener extends ListenerAdapter{          if(event.isFromGuild()) {              guild = event.getGuild();          } -        MessageChannel channel = helper.GetReplyChannel(event); +        setMonitoredGuild(guild); -        // If we haven't set the Monitored Guild yet, set the value -        if(monitoredGuildId == -1 && guild != null){ -            monitoredGuildId = Long.parseLong(guild.getId()); -            monitoredGuild = guild; -        } +        MessageChannel channel = helper.GetReplyChannel(event);          if(monitoredGuild != null){ diff --git a/src/main/java/soundchan/BotListener/BotListenerHelpers.java b/src/main/java/soundchan/BotListener/BotListenerHelpers.java index 4c589a3..c8bcba6 100644 --- a/src/main/java/soundchan/BotListener/BotListenerHelpers.java +++ b/src/main/java/soundchan/BotListener/BotListenerHelpers.java @@ -13,7 +13,10 @@ public class BotListenerHelpers {       * @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(); +        Guild guild = null; +        if(event.isFromGuild()) { +            guild = event.getGuild(); +        }          return (guild == null) ? event.getPrivateChannel() : event.getTextChannel();      }  | 
