You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.4 KiB
86 lines
3.4 KiB
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package moe.nekojimi.chords.commands;
|
|
|
|
import java.util.List;
|
|
import moe.nekojimi.chords.Chords;
|
|
import net.dv8tion.jda.api.entities.*;
|
|
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
|
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
|
|
|
public class JoinCommand extends Command
|
|
{
|
|
|
|
public JoinCommand(Chords bot)
|
|
{
|
|
super(bot, "join");
|
|
}
|
|
|
|
@Override
|
|
public void call(Invocation invocation)
|
|
{
|
|
MessageReceivedEvent event = invocation.getEvent();
|
|
List<String> args = invocation.getArgs();
|
|
MessageChannel textChannel = event.getChannel();
|
|
VoiceChannel channel = null;
|
|
if (args.isEmpty() || args.get(0).isBlank())
|
|
{
|
|
final Member member = event.getMessage().getMember();
|
|
if (member != null)
|
|
{
|
|
GuildVoiceState voiceState = member.getVoiceState();
|
|
if (voiceState != null && voiceState.inAudioChannel())
|
|
channel = voiceState.getChannel().asVoiceChannel();
|
|
}
|
|
if (channel == null)
|
|
{
|
|
Guild guild = event.getMessage().getGuild();
|
|
List<VoiceChannel> voiceChannels = guild.getVoiceChannels();
|
|
System.out.println("Finding channel to join...");
|
|
for (VoiceChannel voiceChannel : voiceChannels)
|
|
{
|
|
List<Member> members = voiceChannel.getMembers();
|
|
if (!members.isEmpty())
|
|
channel = voiceChannel;
|
|
System.out.println("\t" + voiceChannel.getName() + " " + members.size());
|
|
}
|
|
}
|
|
if (channel == null)
|
|
throw 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
|
|
throw new RuntimeException("Unable to connect to \"" + channel.getName() + "\", no such channel!");
|
|
}
|
|
|
|
}
|
|
bot.connectTo(channel); // We found a channel to connect to!
|
|
invocation.respond("Connecting to " + channel.getName() + "...");
|
|
// onConnecting(channel, textChannel); // Let the user know, we were successful!
|
|
}
|
|
|
|
@Override
|
|
public String argumentDescription()
|
|
{
|
|
return "<Channel?>";
|
|
}
|
|
|
|
}
|
|
|