Command stuff fixed

Replies sanitized, userinfo converted
Manually removing spaces
Tested
This commit is contained in:
Norbi Peti 2019-02-16 00:39:48 +01:00
parent 13b168d238
commit 5a9986de4f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
8 changed files with 114 additions and 116 deletions

View file

@ -3,6 +3,7 @@ package buttondevteam.discordplugin;
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule; import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
import buttondevteam.discordplugin.commands.Command2DC; import buttondevteam.discordplugin.commands.Command2DC;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.commands.UserinfoCommand;
import buttondevteam.discordplugin.commands.VersionCommand; import buttondevteam.discordplugin.commands.VersionCommand;
import buttondevteam.discordplugin.exceptions.ExceptionListenerModule; import buttondevteam.discordplugin.exceptions.ExceptionListenerModule;
import buttondevteam.discordplugin.fun.FunModule; import buttondevteam.discordplugin.fun.FunModule;
@ -126,6 +127,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
DiscordCommandBase.registerCommands(); DiscordCommandBase.registerCommands();
getManager().registerCommand(new VersionCommand()); getManager().registerCommand(new VersionCommand());
getManager().registerCommand(new UserinfoCommand());
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

@ -22,7 +22,7 @@ public class Command2DC extends Command2<ICommand2DC, Command2DCSender> {
@Override @Override
public void registerCommand(ICommand2DC command) { public void registerCommand(ICommand2DC command) {
registerCommand(command, subcommands, DiscordPlugin.getPrefix()); registerCommand(command, subcommands, DiscordPlugin.getPrefix()); //Needs to be configurable for the helps
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.lib.chat.Command2Sender; import buttondevteam.lib.chat.Command2Sender;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -11,11 +12,11 @@ public class Command2DCSender implements Command2Sender {
@Override @Override
public void sendMessage(String message) { public void sendMessage(String message) {
this.message.reply(message); this.message.reply(DPUtils.sanitizeString(message));
} }
@Override @Override
public void sendMessage(String[] message) { public void sendMessage(String[] message) {
this.message.reply(String.join("\n", message)); this.message.reply(DPUtils.sanitizeString(String.join("\n", message)));
} }
} }

View file

@ -22,7 +22,6 @@ public abstract class DiscordCommandBase {
public static void registerCommands() { public static void registerCommands() {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands? commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
commands.put("userinfo", new UserinfoCommand());
commands.put("help", new HelpCommand()); commands.put("help", new HelpCommand());
commands.put("mcchat", new MCChatCommand()); commands.put("mcchat", new MCChatCommand());
commands.put("channelcon", new ChannelconCommand()); commands.put("channelcon", new ChannelconCommand());

View file

@ -6,7 +6,7 @@ import buttondevteam.lib.chat.ICommand2;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
public abstract class ICommand2DC extends ICommand2 { public abstract class ICommand2DC extends ICommand2<Command2DCSender> {
public <T extends ICommand2> ICommand2DC() { public <T extends ICommand2> ICommand2DC() {
super(DiscordPlugin.plugin.getManager()); super(DiscordPlugin.plugin.getManager());
val ann = getClass().getAnnotation(CommandClass.class); val ann = getClass().getAnnotation(CommandClass.class);

View file

@ -3,8 +3,11 @@ package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
import lombok.val;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
@ -12,17 +15,17 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UserinfoCommand extends DiscordCommandBase { @CommandClass(helpText = {
"User information", //
@Override "Shows some information about users, from Discord, from Minecraft or from Reddit if they have these accounts connected.", //
public String getCommandName() { "If used without args, shows your info.", //
return "userinfo"; })
} public class UserinfoCommand extends ICommand2DC {
@Command2.Subcommand
@Override public boolean def(Command2DCSender sender, @Command2.OptionalArg String args) {
public boolean run(IMessage message, String args) { val message = sender.getMessage();
IUser target = null; IUser target = null;
if (args.length() == 0) if (args == null || args.length() == 0)
target = message.getAuthor(); target = message.getAuthor();
else { else {
final Optional<IUser> firstmention = message.getMentions().stream() final Optional<IUser> firstmention = message.getMentions().stream()
@ -85,15 +88,4 @@ public class UserinfoCommand extends DiscordCommandBase {
return targets; return targets;
} }
@Override
public String[] getHelpText() {
return new String[] { //
"---- User information ----", //
"Shows some information about users, from Discord, from Minecraft or from Reddit if they have these accounts connected.", //
"If used without args, shows your info.", //
"Usage: " + DiscordPlugin.getPrefix() + "userinfo [username/nickname[#tag]/ping]", //
"Examples:\n" + DiscordPlugin.getPrefix() + "userinfo ChromaBot\n" + DiscordPlugin.getPrefix() + "userinfo ChromaBot#6338\n" + DiscordPlugin.getPrefix() + "userinfo @ChromaBot#6338" //
};
}
} }

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import lombok.val; import lombok.val;
@ -9,6 +10,7 @@ import lombok.val;
"Returns the plugin's version" "Returns the plugin's version"
}) })
public class VersionCommand extends ICommand2DC { public class VersionCommand extends ICommand2DC {
@Command2.Subcommand
public boolean def(Command2DCSender sender) { public boolean def(Command2DCSender sender) {
sender.sendMessage(getVersion()); sender.sendMessage(getVersion());
return true; return true;

View file

@ -21,7 +21,7 @@ public class CommandListener {
final IChannel channel = message.getChannel(); final IChannel channel = message.getChannel();
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
if (!message.getChannel().isPrivate() if (!message.getChannel().isPrivate()
&& !(message.getContent().charAt(0) == DiscordPlugin.getPrefix() && !(message.getContent().charAt(0) == DiscordPlugin.getPrefix()
&& channel.getStringID().equals(DiscordPlugin.plugin.CommandChannel().get().getStringID()))) // && channel.getStringID().equals(DiscordPlugin.plugin.CommandChannel().get().getStringID()))) //
return false; return false;
message.getChannel().setTypingStatus(true); // Fun message.getChannel().setTypingStatus(true); // Fun
@ -38,9 +38,10 @@ public class CommandListener {
return false; return false;
} }
message.getChannel().setTypingStatus(true); message.getChannel().setTypingStatus(true);
String cmdwithargsString = cmdwithargs.toString().trim(); //Remove spaces between mention and command String cmdwithargsString = cmdwithargs.toString();
try { try {
DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString); if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
message.reply("Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e); TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
} }
@ -61,18 +62,19 @@ public class CommandListener {
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) { private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1) if (cmdwithargs.length() > mention.length() + 1) {
cmdwithargs.delete(0, int x = mention.length(), px = x;
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length()); while ((x = cmdwithargs.indexOf(" ", px + 1)) != -1) //Removes any space before the command
else px = x;
cmdwithargs.replace(0, cmdwithargs.length(), "help"); cmdwithargs.delete(0, px + 1);
cmdwithargs.insert(0, DiscordPlugin.getPrefix()); //Always use the prefix for processing
} else
cmdwithargs.replace(0, cmdwithargs.length(), DiscordPlugin.getPrefix() + "help");
else { else {
if (cmdwithargs.length() > 0 && cmdwithargs.charAt(0) == '/') return false; //Don't treat / as mention, mentions can be used in public mcchat
cmdwithargs.deleteCharAt(0); //Don't treat / as mention, mentions can be used in public mcchat
return false;
} }
if (cmdwithargs.length() == 0) if (cmdwithargs.length() == 0)
cmdwithargs.replace(0, cmdwithargs.length(), "help"); cmdwithargs.replace(0, cmdwithargs.length(), DiscordPlugin.getPrefix() + "help");
return true; return true;
} }
} }