WIP: add logic for joining the user's current voice channel.
This commit is contained in:
parent
e9ee8a575d
commit
983b4b0123
|
@ -6,11 +6,9 @@
|
||||||
package moe.nekojimi.chords.commands;
|
package moe.nekojimi.chords.commands;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import moe.nekojimi.chords.Main;
|
import moe.nekojimi.chords.Main;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
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.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
|
||||||
public class JoinCommand extends Command
|
public class JoinCommand extends Command
|
||||||
|
@ -24,24 +22,40 @@ public class JoinCommand extends Command
|
||||||
@Override
|
@Override
|
||||||
public void call(GuildMessageReceivedEvent event, List<String> args)
|
public void call(GuildMessageReceivedEvent event, List<String> 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
|
|
||||||
VoiceChannel channel = null;
|
|
||||||
if (isNumber) // The input is an id?
|
|
||||||
channel = guild.getVoiceChannelById(arg0);
|
|
||||||
if (channel == null) // Then the input must be a name?
|
|
||||||
{
|
|
||||||
List<VoiceChannel> 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();
|
TextChannel textChannel = event.getChannel();
|
||||||
if (channel == null) // I have no idea what you want mr user
|
VoiceChannel channel = null;
|
||||||
|
if (args.isEmpty() || args.get(0).isBlank())
|
||||||
{
|
{
|
||||||
onUnknownChannel(textChannel, arg0); // Let the user know about our failure
|
GuildVoiceState voiceState = event.getMember().getVoiceState();
|
||||||
return;
|
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
|
||||||
|
if (isNumber) // The input is an id?
|
||||||
|
channel = guild.getVoiceChannelById(arg0);
|
||||||
|
if (channel == null) // Then the input must be a name?
|
||||||
|
{
|
||||||
|
List<VoiceChannel> 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!
|
bot.connectTo(channel); // We found a channel to connect to!
|
||||||
onConnecting(channel, textChannel); // Let the user know, we were successful!
|
onConnecting(channel, textChannel); // Let the user know, we were successful!
|
||||||
|
|
Loading…
Reference in New Issue