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.
78 lines
2.7 KiB
78 lines
2.7 KiB
3 years ago
|
/*
|
||
|
* 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.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.events.message.guild.GuildMessageReceivedEvent;
|
||
|
|
||
|
public class JoinCommand extends Command
|
||
|
{
|
||
|
|
||
|
public JoinCommand(Main bot)
|
||
|
{
|
||
|
super(bot, "join");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
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();
|
||
|
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!
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Inform user about successful connection.
|
||
|
*
|
||
|
* @param channel
|
||
|
* The voice channel we connected to
|
||
|
* @param textChannel
|
||
|
* The text channel to send the message in
|
||
|
*/
|
||
|
public void onConnecting(VoiceChannel channel, TextChannel textChannel)
|
||
|
{
|
||
|
textChannel.sendMessage("Connecting to " + channel.getName()).queue(); // never forget to queue()!
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The channel to connect to is not known to us.
|
||
|
*
|
||
|
* @param channel
|
||
|
* The message channel (text channel abstraction) to send failure
|
||
|
* information to
|
||
|
* @param comment
|
||
|
* The information of this channel
|
||
|
*/
|
||
|
public void onUnknownChannel(MessageChannel channel, String comment)
|
||
|
{
|
||
|
channel.sendMessage("Unable to connect to ``" + comment + "``, no such channel!").queue(); // never forget to queue()!
|
||
|
}
|
||
|
|
||
|
}
|