Converted all commands

Well, haven't finished the role command
This commit is contained in:
Norbi Peti 2019-02-22 00:44:13 +01:00
parent 0c9da49add
commit 325b094bf7
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
15 changed files with 130 additions and 267 deletions

View file

@ -16,7 +16,7 @@ import sx.blah.discord.handle.obj.IMessage;
import java.io.File;
import java.util.List;
public class AnnouncerModule extends Component {
public class AnnouncerModule extends Component<DiscordPlugin> {
public ConfigData<IChannel> channel() {
return DPUtils.channelData(getConfig(), "channel", 239519012529111040L);
}

View file

@ -1,15 +1,11 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
import buttondevteam.discordplugin.commands.Command2DC;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.commands.UserinfoCommand;
import buttondevteam.discordplugin.commands.VersionCommand;
import buttondevteam.discordplugin.commands.*;
import buttondevteam.discordplugin.exceptions.ExceptionListenerModule;
import buttondevteam.discordplugin.fun.FunModule;
import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.discordplugin.listeners.MCListener;
import buttondevteam.discordplugin.mcchat.ChannelconCommand;
import buttondevteam.discordplugin.mcchat.MCChatPrivate;
import buttondevteam.discordplugin.mcchat.MCChatUtils;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
@ -126,10 +122,11 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
Component.registerComponent(this, new FunModule());
new ChromaBot(this).updatePlayerList(); //Initialize ChromaBot - The MCCHatModule is tested to be enabled
DiscordCommandBase.registerCommands();
getManager().registerCommand(new VersionCommand());
getManager().registerCommand(new UserinfoCommand());
getManager().registerCommand(new ChannelconCommand());
getManager().registerCommand(new HelpCommand());
getManager().registerCommand(new DebugCommand());
getManager().registerCommand(new ConnectCommand());
if (ResetMCCommand.resetting) //These will only execute if the chat is enabled
ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.CYAN)
.withTitle("Discord plugin restarted - chat connected.").build(), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm

View file

@ -1,11 +1,12 @@
package buttondevteam.discordplugin.broadcaster;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
import lombok.Getter;
public class GeneralEventBroadcasterModule extends Component {
public class GeneralEventBroadcasterModule extends Component<DiscordPlugin> {
private static @Getter boolean hooked = false;
@Override

View file

@ -3,26 +3,10 @@ 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()); //Needs to be configurable for the helps
super.registerCommand(command, DiscordPlugin.getPrefix()); //Needs to be configurable for the helps
}
@Override

View file

@ -1,76 +1,61 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
import com.google.common.collect.HashBiMap;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import sx.blah.discord.handle.obj.IMessage;
public class ConnectCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "connect";
}
/**
* Key: Minecraft name<br>
* Value: Discord ID
*/
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
@Override
public boolean run(IMessage message, String args) {
if (args.length() == 0)
return false;
if (args.contains(" ")) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Too many arguments.\nUsage: " + DiscordPlugin.getPrefix() + "connect <Minecraftname>");
return true;
}
if (WaitingToConnect.inverse().containsKey(message.getAuthor().getStringID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Replacing " + WaitingToConnect.inverse().get(message.getAuthor().getStringID()) + " with " + args);
WaitingToConnect.inverse().remove(message.getAuthor().getStringID());
}
@SuppressWarnings("deprecation")
OfflinePlayer p = Bukkit.getOfflinePlayer(args);
if (p == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The specified Minecraft player cannot be found");
return true;
}
try (TBMCPlayer pl = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class)) {
DiscordPlayer dp = pl.getAs(DiscordPlayer.class);
if (dp != null && message.getAuthor().getStringID().equals(dp.getDiscordID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected.");
return true;
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while connecting a Discord account!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An internal error occured!\n" + e);
}
WaitingToConnect.put(p.getName(), message.getAuthor().getStringID());
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Alright! Now accept the connection in Minecraft from the account " + args
+ " before the next server restart. You can also adjust the Minecraft name you want to connect to with the same command.");
if (p.isOnline())
((Player) p).sendMessage("§bTo connect with the Discord account " + message.getAuthor().getName() + "#"
+ message.getAuthor().getDiscriminator() + " do /discord accept");
return true;
}
@Override
public String[] getHelpText() {
return new String[] { //
"---- Connect command ----", //
"This command lets you connect your account with a Minecraft account. This allows using the Minecraft chat and other things.", //
"Usage: /connect <Minecraftname>" //
};
}
}
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
import com.google.common.collect.HashBiMap;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import sx.blah.discord.handle.obj.IMessage;
@CommandClass(helpText = {
"Connect command", //
"This command lets you connect your account with a Minecraft account. This allows using the Minecraft chat and other things.", //
})
public class ConnectCommand extends ICommand2DC {
/**
* Key: Minecraft name<br>
* Value: Discord ID
*/
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
@Command2.Subcommand
public boolean def(IMessage message, String Minecraftname) {
if (WaitingToConnect.inverse().containsKey(message.getAuthor().getStringID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Replacing " + WaitingToConnect.inverse().get(message.getAuthor().getStringID()) + " with " + Minecraftname);
WaitingToConnect.inverse().remove(message.getAuthor().getStringID());
}
@SuppressWarnings("deprecation")
OfflinePlayer p = Bukkit.getOfflinePlayer(Minecraftname);
if (p == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The specified Minecraft player cannot be found");
return true;
}
try (TBMCPlayer pl = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class)) {
DiscordPlayer dp = pl.getAs(DiscordPlayer.class);
if (dp != null && message.getAuthor().getStringID().equals(dp.getDiscordID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected.");
return true;
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while connecting a Discord account!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An internal error occured!\n" + e);
}
WaitingToConnect.put(p.getName(), message.getAuthor().getStringID());
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Alright! Now accept the connection in Minecraft from the account " + Minecraftname
+ " before the next server restart. You can also adjust the Minecraft name you want to connect to with the same command.");
if (p.isOnline())
((Player) p).sendMessage("§bTo connect with the Discord account " + message.getAuthor().getName() + "#"
+ message.getAuthor().getDiscriminator() + " do /discord accept");
return true;
}
}

View file

@ -2,25 +2,20 @@ package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage;
public class DebugCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "debug";
}
@Override
public boolean run(IMessage message, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L)))
@CommandClass(helpText = {
"Switches debug mode."
})
public class DebugCommand extends ICommand2DC {
@Command2.Subcommand
public boolean def(IMessage message, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L))) //TODO: Make configurable
message.reply("Debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
else
message.reply("You need to be a moderator to use this command.");
return true;
}
@Override
public String[] getHelpText() {
return new String[]{"Switches debug mode."};
}
}

View file

@ -1,61 +0,0 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.mcchat.MCChatCommand;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.handle.obj.IMessage;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;
import static buttondevteam.discordplugin.listeners.CommonListeners.debug;
public abstract class DiscordCommandBase {
public abstract String getCommandName();
public abstract boolean run(IMessage message, String args);
public abstract String[] getHelpText();
static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
public static void registerCommands() {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
commands.put("help", new HelpCommand());
commands.put("mcchat", new MCChatCommand());
commands.put("debug", new DebugCommand());
}
public static void runCommand(String cmd, String args, IMessage message) {
debug("F"); //Not sure if needed
DiscordCommandBase command = commands.get(cmd);
//if(command==null)
// DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmd+" "+args); //TODO!
if (command == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Unknown command: " + cmd + " with args: " + args + "\nDo '"
+ (message.getChannel().isPrivate() ? "" : message.getClient().getOurUser().mention() + " ")
+ "help' for help");
return;
}
debug("G");
try {
if (!command.run(message, args))
DiscordPlugin.sendMessageToChannel(message.getChannel(), Arrays.stream(command.getHelpText()).collect(Collectors.joining("\n")));
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while executing command " + cmd + "!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"An internal error occured while executing this command. For more technical details see the server-issues channel on the dev Discord.");
}
debug("H");
}
protected String[] splitargs(String args) {
return args.split("\\s+");
}
public static void registerCommand(String name, DiscordCommandBase dcb) {
commands.put(name, dcb);
}
}

View file

@ -1,38 +1,18 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import sx.blah.discord.handle.obj.IMessage;
import java.util.stream.Collectors;
public class HelpCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "help";
}
@Override
public boolean run(IMessage message, String args) {
DiscordCommandBase argdc;
if (args.length() == 0)
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Available commands:\n" + DiscordCommandBase.commands.values().stream()
.map(dc -> DiscordPlugin.getPrefix() + dc.getCommandName()).collect(Collectors.joining("\n")));
else
DiscordPlugin.sendMessageToChannel(message.getChannel(),
(argdc = DiscordCommandBase.commands.get(args)) == null ? "Command not found: " + args
: String.join("\n", argdc.getHelpText()));
return true;
}
@Override
public String[] getHelpText() {
return new String[] { //
"---- Help command ----", //
"Shows some info about a command or lists the available commands.", //
"Usage: " + DiscordPlugin.getPrefix() + "help [command]"//
};
}
}
package buttondevteam.discordplugin.commands;
import buttondevteam.lib.chat.CommandClass;
@CommandClass(helpText = {
"Help command", //
"Shows some info about a command or lists the available commands.", //
})
public class HelpCommand extends ICommand2DC {
@Override
public boolean def(Command2DCSender sender, String args) {
if (args.length() == 0)
sender.sendMessage(getManager().getCommandsText());
else
sender.sendMessage("Soon:tm:"); //TODO
return true;
}
}

View file

@ -19,7 +19,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ExceptionListenerModule extends Component implements Listener {
public class ExceptionListenerModule extends Component<DiscordPlugin> implements Listener {
private List<Throwable> lastthrown = new ArrayList<>();
private List<String> lastsourcemsg = new ArrayList<>();

View file

@ -26,7 +26,7 @@ import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
public class FunModule extends Component implements Listener {
public class FunModule extends Component<DiscordPlugin> implements Listener {
private static FunModule mod;
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali

View file

@ -45,17 +45,6 @@ public class CommandListener {
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
}
/*int index = cmdwithargsString.indexOf(" ");
String cmd;
String args;
if (index == -1) {
cmd = cmdwithargsString;
args = "";
} else {
cmd = cmdwithargsString.substring(0, index);
args = cmdwithargsString.substring(index + 1).trim(); //In case there are multiple spaces
}
DiscordCommandBase.runCommand(cmd.toLowerCase(), args, message);*/
message.getChannel().setTypingStatus(false);
return true;
}

View file

@ -2,44 +2,38 @@ package buttondevteam.discordplugin.mcchat;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.commands.Command2DCSender;
import buttondevteam.discordplugin.commands.ICommand2DC;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.handle.obj.IMessage;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import lombok.val;
public class MCChatCommand extends DiscordCommandBase {
@CommandClass(helpText = {
"MC Chat",
"This command enables or disables the Minecraft chat in private messages.", //
"It can be useful if you don't want your messages to be visible, for example when talking in a private channel.", //
"You can also run all of the ingame commands you have access to using this command, if you have your accounts connected." //
})
public class MCChatCommand extends ICommand2DC {
@Override
public String getCommandName() {
return "mcchat";
}
@Override //TODO: Only register if module is enabled
public boolean run(IMessage message, String args) {
@Command2.Subcommand
public boolean def(Command2DCSender sender, String args) {
val message = sender.getMessage();
if (!message.getChannel().isPrivate()) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"This command can only be issued in a direct message with the bot.");
message.reply("this command can only be issued in a direct message with the bot.");
return true;
}
try (final DiscordPlayer user = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)) {
boolean mcchat = !user.isMinecraftChatEnabled();
MCChatPrivate.privateMCChat(message.getChannel(), mcchat, message.getAuthor(), user);
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Minecraft chat " + (mcchat //
? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." //
: "disabled."));
message.reply("Minecraft chat " + (mcchat //
? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." //
: "disabled."));
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while setting mcchat for user" + message.getAuthor().getName(), e);
}
return true;
}
@Override
public String[] getHelpText() {
return new String[] { //
DiscordPlugin.getPrefix() + "mcchat enables or disables the Minecraft chat in private messages.", //
"It can be useful if you don't want your messages to be visible, for example when talking in a private channel.", //
"You can also run all of the ingame commands you have access to using this command, if you have your accounts connected." //
}; // TODO: Pin channel switching to indicate the current channel
}
} // TODO: Pin channel switching to indicate the current channel
}

View file

@ -19,7 +19,7 @@ import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
public class MinecraftChatModule extends Component {
public class MinecraftChatModule extends Component<DiscordPlugin> {
private @Getter MCChatListener listener;
public MCChatListener getListener() { //It doesn't want to generate
@ -45,6 +45,8 @@ public class MinecraftChatModule extends Component {
DiscordPlugin.dc.getDispatcher().registerListener(listener);
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled
getPlugin().getManager().registerCommand(new MCChatCommand());
getPlugin().getManager().registerCommand(new ChannelconCommand());
val chcons = getConfig().getConfig().getConfigurationSection("chcons");
if (chcons == null) //Fallback to old place

View file

@ -3,7 +3,6 @@ package buttondevteam.discordplugin.role;
import buttondevteam.core.ComponentManager;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData;
import lombok.val;
@ -19,12 +18,12 @@ import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;
public class GameRoleModule extends Component {
public class GameRoleModule extends Component<DiscordPlugin> {
public List<String> GameRoles;
@Override
protected void enable() {
DiscordCommandBase.registerCommand("role", new RoleCommand(this));
getPlugin().getManager().registerCommand(new RoleCommand(this));
GameRoles = DiscordPlugin.mainServer.getRoles().stream().filter(this::isGameRole).map(IRole::getName).collect(Collectors.toList());
}

View file

@ -2,15 +2,18 @@ package buttondevteam.discordplugin.role;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.commands.ICommand2DC;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IRole;
import java.util.List;
import java.util.stream.Collectors;
public class RoleCommand extends DiscordCommandBase { //TODO: Use Command2's parser
@CommandClass
public class RoleCommand extends ICommand2DC { //TODO
private GameRoleModule grm;
@ -18,13 +21,8 @@ public class RoleCommand extends DiscordCommandBase { //TODO: Use Command2's par
this.grm = grm;
}
@Override
public String getCommandName() {
return "role";
}
@Override
public boolean run(IMessage message, String args) {
@Command2.Subcommand
public boolean def(IMessage message, String args) {
if (args.length() == 0)
return false;
String[] argsa = splitargs(args);