WIP: add logic for joining the user's current voice channel.

crossfading
Nekojimi 3 years ago
parent e9ee8a575d
commit 983b4b0123
  1. 26
      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
@ -23,11 +21,26 @@ public class JoinCommand extends Command
@Override
public void call(GuildMessageReceivedEvent event, List<String> args)
{
TextChannel textChannel = event.getChannel();
VoiceChannel channel = null;
if (args.isEmpty() || args.get(0).isBlank())
{
GuildVoiceState voiceState = event.getMember().getVoiceState();
if (voiceState != null && voiceState.inVoiceChannel())
channel = voiceState.getChannel();
else
{
Guild guild = event.getMessage().getGuild();
List<VoiceChannel> voiceChannels = guild.getVoiceChannels();
Optional<VoiceChannel> findFirst = voiceChannels.stream().filter((t) -> !t.getMembers().isEmpty()).findFirst();
channel = findFirst.orElseThrow(() -> new RuntimeException("Cannot find any voice channels with people in!"));
}
} else
{
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
VoiceChannel channel = null;
if (isNumber) // The input is an id?
channel = guild.getVoiceChannelById(arg0);
if (channel == null) // Then the input must be a name?
@ -37,12 +50,13 @@ public class JoinCommand extends Command
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
{
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!
}

Loading…
Cancel
Save