|
|
@ -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); |
|
|
|
TextChannel textChannel = event.getChannel(); |
|
|
|
Guild guild = event.getGuild(); |
|
|
|
|
|
|
|
boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
|
|
|
|
|
|
|
|
VoiceChannel channel = null; |
|
|
|
VoiceChannel channel = null; |
|
|
|
if (isNumber) // The input is an id?
|
|
|
|
if (args.isEmpty() || args.get(0).isBlank()) |
|
|
|
channel = guild.getVoiceChannelById(arg0); |
|
|
|
|
|
|
|
if (channel == null) // Then the input must be a name?
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg0, true); |
|
|
|
GuildVoiceState voiceState = event.getMember().getVoiceState(); |
|
|
|
if (!channels.isEmpty()) // Make sure we found at least one exact match
|
|
|
|
if (voiceState != null && voiceState.inVoiceChannel()) |
|
|
|
channel = channels.get(0); // We found a channel! This cannot be null.
|
|
|
|
channel = voiceState.getChannel(); |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
TextChannel textChannel = event.getChannel(); |
|
|
|
Guild guild = event.getMessage().getGuild(); |
|
|
|
if (channel == null) // I have no idea what you want mr user
|
|
|
|
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 |
|
|
|
{ |
|
|
|
{ |
|
|
|
onUnknownChannel(textChannel, arg0); // Let the user know about our failure
|
|
|
|
String arg0 = args.get(0); |
|
|
|
return; |
|
|
|
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!
|
|
|
|