Add initial version of command structure.
This commit is contained in:
parent
2bd51ea856
commit
a4e4093f40
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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.events.message.guild.GuildMessageReceivedEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jimj316
|
||||
*/
|
||||
public abstract class Command
|
||||
{
|
||||
|
||||
protected final Main bot;
|
||||
protected final String keyword;
|
||||
protected String documentation;
|
||||
|
||||
public Command(Main bot, String keyword)
|
||||
{
|
||||
this.bot = bot;
|
||||
this.keyword = keyword;
|
||||
}
|
||||
|
||||
public abstract void call(GuildMessageReceivedEvent event, List<String> arg);
|
||||
|
||||
public String getKeyword()
|
||||
{
|
||||
return keyword;
|
||||
}
|
||||
|
||||
public String getDocumentation()
|
||||
{
|
||||
return documentation;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* 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()!
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.events.message.guild.GuildMessageReceivedEvent;
|
||||
|
||||
public class LeaveCommand extends Command
|
||||
{
|
||||
|
||||
public LeaveCommand(Main bot)
|
||||
{
|
||||
super(bot, "leave");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void call(GuildMessageReceivedEvent event, List<String> arg)
|
||||
{
|
||||
if (bot.getCurrentVoiceChannel() != null)
|
||||
{
|
||||
bot.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue