From 983b4b0123f19a042f03774a9d3cce1158aef670 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Tue, 19 Apr 2022 23:40:47 +0100 Subject: [PATCH] WIP: add logic for joining the user's current voice channel. --- .../nekojimi/chords/commands/JoinCommand.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java index e952257..c22e161 100644 --- a/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java @@ -6,11 +6,9 @@ package moe.nekojimi.chords.commands; import java.util.List; +import java.util.Optional; import moe.nekojimi.chords.Main; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.MessageChannel; -import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.entities.VoiceChannel; +import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class JoinCommand extends Command @@ -24,24 +22,40 @@ public class JoinCommand extends Command @Override public void call(GuildMessageReceivedEvent event, List args) { - String arg0 = args.get(0); - Guild guild = event.getGuild(); - boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits + TextChannel textChannel = event.getChannel(); VoiceChannel channel = null; - if (isNumber) // The input is an id? - channel = guild.getVoiceChannelById(arg0); - if (channel == null) // Then the input must be a name? + if (args.isEmpty() || args.get(0).isBlank()) { - List channels = guild.getVoiceChannelsByName(arg0, true); - if (!channels.isEmpty()) // Make sure we found at least one exact match - channel = channels.get(0); // We found a channel! This cannot be null. - } - - TextChannel textChannel = event.getChannel(); - if (channel == null) // I have no idea what you want mr user + GuildVoiceState voiceState = event.getMember().getVoiceState(); + if (voiceState != null && voiceState.inVoiceChannel()) + channel = voiceState.getChannel(); + else + { + Guild guild = event.getMessage().getGuild(); + List voiceChannels = guild.getVoiceChannels(); + Optional findFirst = voiceChannels.stream().filter((t) -> !t.getMembers().isEmpty()).findFirst(); + channel = findFirst.orElseThrow(() -> new RuntimeException("Cannot find any voice channels with people in!")); + } + } else { - onUnknownChannel(textChannel, arg0); // Let the user know about our failure - return; + String arg0 = args.get(0); + Guild guild = event.getGuild(); + boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits + if (isNumber) // The input is an id? + channel = guild.getVoiceChannelById(arg0); + if (channel == null) // Then the input must be a name? + { + List channels = guild.getVoiceChannelsByName(arg0, true); + if (!channels.isEmpty()) // Make sure we found at least one exact match + channel = channels.get(0); // We found a channel! This cannot be null. + } + + if (channel == null) // I have no idea what you want mr user + { + onUnknownChannel(textChannel, arg0); // Let the user know about our failure + return; + } + } bot.connectTo(channel); // We found a channel to connect to! onConnecting(channel, textChannel); // Let the user know, we were successful!