Started using new command system for DC cmds

Some of the new files are a couple days old
This commit is contained in:
Norbi Peti 2019-02-09 00:24:42 +01:00
parent abd4050786
commit d08d32f73f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 100 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package buttondevteam.discordplugin;
import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.architecture.IHaveConfig; import buttondevteam.lib.architecture.IHaveConfig;
import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IIDLinkedObject; import sx.blah.discord.handle.obj.IIDLinkedObject;
@ -112,8 +113,11 @@ public final class DPUtils {
return config.getDataPrimDef(key, defID, id -> DiscordPlugin.dc.getChannelByID((long) id), IIDLinkedObject::getLongID); //We can afford to search for the channel in the cache once (instead of using mainServer) return config.getDataPrimDef(key, defID, id -> DiscordPlugin.dc.getChannelByID((long) id), IIDLinkedObject::getLongID); //We can afford to search for the channel in the cache once (instead of using mainServer)
} }
public static ConfigData<IRole> roleData(IHaveConfig config, String key, long defID) { public static ConfigData<IRole> roleData(IHaveConfig config, String key, String defName) {
return config.getDataPrimDef(key, defID, id -> DiscordPlugin.dc.getRoleByID((long) id), IIDLinkedObject::getLongID); //We can afford to search for the channel in the cache once (instead of using mainServer) return config.getDataPrimDef(key, defName, name -> {
val roles = DiscordPlugin.mainServer.getRolesByName((String) name);
return roles.size() > 0 ? roles.get(0) : null; //TODO: May not handle null properly
}, IIDLinkedObject::getLongID); //We can afford to search for the channel in the cache once (instead of using mainServer)
} }
/** /**

View file

@ -1,7 +1,9 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule; import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
import buttondevteam.discordplugin.commands.Command2DC;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.commands.VersionCommand;
import buttondevteam.discordplugin.exceptions.ExceptionListenerModule; import buttondevteam.discordplugin.exceptions.ExceptionListenerModule;
import buttondevteam.discordplugin.listeners.CommonListeners; import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.discordplugin.listeners.MCListener; import buttondevteam.discordplugin.listeners.MCListener;
@ -18,6 +20,7 @@ import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
import com.google.common.io.Files; import com.google.common.io.Files;
import lombok.Getter;
import lombok.val; import lombok.val;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -46,6 +49,8 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
public static IDiscordClient dc; public static IDiscordClient dc;
public static DiscordPlugin plugin; public static DiscordPlugin plugin;
public static boolean SafeMode = true; public static boolean SafeMode = true;
@Getter
private Command2DC manager;
public ConfigData<Character> Prefix() { public ConfigData<Character> Prefix() {
return getIConfig().getData("prefix", '/', str -> ((String) str).charAt(0), Object::toString); return getIConfig().getData("prefix", '/', str -> ((String) str).charAt(0), Object::toString);
@ -64,11 +69,16 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
return DPUtils.channelData(getIConfig(), "commandChannel", 239519012529111040L); return DPUtils.channelData(getIConfig(), "commandChannel", 239519012529111040L);
} }
public ConfigData<IRole> ModRole() {
return DPUtils.roleData(getIConfig(), "modRole", "Moderator");
}
@Override @Override
public void pluginEnable() { public void pluginEnable() {
try { try {
Bukkit.getLogger().info("Initializing DiscordPlugin..."); Bukkit.getLogger().info("Initializing DiscordPlugin...");
plugin = this; plugin = this;
manager = new Command2DC();
ClientBuilder cb = new ClientBuilder(); ClientBuilder cb = new ClientBuilder();
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8)); cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
dc = cb.login(); dc = cb.login();
@ -146,6 +156,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
new ChromaBot(this).updatePlayerList(); //Initialize ChromaBot - The MCCHatModule is tested to be enabled new ChromaBot(this).updatePlayerList(); //Initialize ChromaBot - The MCCHatModule is tested to be enabled
DiscordCommandBase.registerCommands(); DiscordCommandBase.registerCommands();
getManager().registerCommand(new VersionCommand());
if (ResetMCCommand.resetting) //These will only execute if the chat is enabled if (ResetMCCommand.resetting) //These will only execute if the chat is enabled
ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.CYAN) ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.CYAN)
.withTitle("Discord plugin restarted - chat connected.").build(), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm .withTitle("Discord plugin restarted - chat connected.").build(), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm

View file

@ -0,0 +1,32 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.chat.Command2;
import java.util.HashMap;
import java.util.function.Function;
public class Command2DC extends Command2<ICommand2DC, Command2DCSender> {
private HashMap<String, SubcommandData<ICommand2DC>> subcommands = new HashMap<>();
private HashMap<Class<?>, ParamConverter<?>> paramConverters = new HashMap<>();
@Override
public <T> void addParamConverter(Class<T> cl, Function<String, T> converter, String errormsg) {
addParamConverter(cl, converter, errormsg, paramConverters);
}
@Override
public boolean handleCommand(Command2DCSender sender, String commandLine) throws Exception {
return handleCommand(sender, commandLine, subcommands, paramConverters);
}
@Override
public void registerCommand(ICommand2DC command) {
registerCommand(command, subcommands, DiscordPlugin.getPrefix());
}
@Override
public boolean hasPermission(Command2DCSender sender, ICommand2DC command) {
return !command.isModOnly() || sender.getMessage().getAuthor().hasRole(DiscordPlugin.plugin.ModRole().get());
}
}

View file

@ -0,0 +1,21 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.lib.chat.Command2Sender;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import sx.blah.discord.handle.obj.IMessage;
@RequiredArgsConstructor
public class Command2DCSender implements Command2Sender {
private final @Getter IMessage message;
@Override
public void sendMessage(String message) {
this.message.reply(message);
}
@Override
public void sendMessage(String[] message) {
this.message.reply(String.join("\n", message));
}
}

View file

@ -27,12 +27,13 @@ public abstract class DiscordCommandBase {
commands.put("mcchat", new MCChatCommand()); commands.put("mcchat", new MCChatCommand());
commands.put("channelcon", new ChannelconCommand()); commands.put("channelcon", new ChannelconCommand());
commands.put("debug", new DebugCommand()); commands.put("debug", new DebugCommand());
commands.put("version", new VersionCommand());
} }
public static void runCommand(String cmd, String args, IMessage message) { public static void runCommand(String cmd, String args, IMessage message) {
debug("F"); //Not sure if needed debug("F"); //Not sure if needed
DiscordCommandBase command = commands.get(cmd); DiscordCommandBase command = commands.get(cmd);
//if(command==null)
// DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmd+" "+args); //TODO!
if (command == null) { if (command == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Unknown command: " + cmd + " with args: " + args + "\nDo '" "Unknown command: " + cmd + " with args: " + args + "\nDo '"

View file

@ -0,0 +1,20 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.ICommand2;
import lombok.Getter;
import lombok.val;
public abstract class ICommand2DC extends ICommand2 {
public <T extends ICommand2> ICommand2DC() {
super(DiscordPlugin.plugin.getManager());
val ann = getClass().getAnnotation(CommandClass.class);
if (ann == null)
modOnly = false;
else
modOnly = ann.modOnly();
}
private final @Getter boolean modOnly;
}

View file

@ -1,26 +1,19 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.chat.CommandClass;
import lombok.val; import lombok.val;
import sx.blah.discord.handle.obj.IMessage;
public class VersionCommand extends DiscordCommandBase { @CommandClass(helpText = {
@Override "Version",
public String getCommandName() { "Returns the plugin's version"
return "version"; })
} public class VersionCommand extends ICommand2DC {
public boolean def(Command2DCSender sender) {
@Override sender.sendMessage(getVersion());
public boolean run(IMessage message, String args) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), String.join("\n", getVersion()));
return true; return true;
} }
@Override
public String[] getHelpText() {
return VersionCommand.getVersion(); //Heh
}
public static String[] getVersion() { public static String[] getVersion() {
val desc = DiscordPlugin.plugin.getDescription(); val desc = DiscordPlugin.plugin.getDescription();
return new String[]{ // return new String[]{ //