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

@ -1,99 +1,91 @@
package buttondevteam.discordplugin.commands; 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.player.ChromaGamerBase; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage; import buttondevteam.lib.player.ChromaGamerBase;
import sx.blah.discord.handle.obj.IUser; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
import lombok.val;
import java.util.List; import sx.blah.discord.handle.obj.IMessage;
import java.util.Optional; import sx.blah.discord.handle.obj.IUser;
import java.util.stream.Collectors;
import java.util.List;
public class UserinfoCommand extends DiscordCommandBase { import java.util.Optional;
import java.util.stream.Collectors;
@Override
public String getCommandName() { @CommandClass(helpText = {
return "userinfo"; "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.", //
@Override })
public boolean run(IMessage message, String args) { public class UserinfoCommand extends ICommand2DC {
IUser target = null; @Command2.Subcommand
if (args.length() == 0) public boolean def(Command2DCSender sender, @Command2.OptionalArg String args) {
target = message.getAuthor(); val message = sender.getMessage();
else { IUser target = null;
final Optional<IUser> firstmention = message.getMentions().stream() if (args == null || args.length() == 0)
.filter(m -> !m.getStringID().equals(DiscordPlugin.dc.getOurUser().getStringID())).findFirst(); target = message.getAuthor();
if (firstmention.isPresent()) else {
target = firstmention.get(); final Optional<IUser> firstmention = message.getMentions().stream()
else if (args.contains("#")) { .filter(m -> !m.getStringID().equals(DiscordPlugin.dc.getOurUser().getStringID())).findFirst();
String[] targettag = args.split("#"); if (firstmention.isPresent())
final List<IUser> targets = getUsers(message, targettag[0]); target = firstmention.get();
if (targets.size() == 0) { else if (args.contains("#")) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), String[] targettag = args.split("#");
"The user cannot be found (by name): " + args); final List<IUser> targets = getUsers(message, targettag[0]);
return true; if (targets.size() == 0) {
} DiscordPlugin.sendMessageToChannel(message.getChannel(),
for (IUser ptarget : targets) { "The user cannot be found (by name): " + args);
if (ptarget.getDiscriminator().equalsIgnoreCase(targettag[1])) { return true;
target = ptarget; }
break; for (IUser ptarget : targets) {
} if (ptarget.getDiscriminator().equalsIgnoreCase(targettag[1])) {
} target = ptarget;
if (target == null) { break;
DiscordPlugin.sendMessageToChannel(message.getChannel(), }
"The user cannot be found (by discriminator): " + args + "(Found " + targets.size() }
+ " users with the name.)"); if (target == null) {
return true; DiscordPlugin.sendMessageToChannel(message.getChannel(),
} "The user cannot be found (by discriminator): " + args + "(Found " + targets.size()
} else { + " users with the name.)");
final List<IUser> targets = getUsers(message, args); return true;
if (targets.size() == 0) { }
DiscordPlugin.sendMessageToChannel(message.getChannel(), } else {
"The user cannot be found on Discord: " + args); final List<IUser> targets = getUsers(message, args);
return true; if (targets.size() == 0) {
} DiscordPlugin.sendMessageToChannel(message.getChannel(),
if (targets.size() > 1) { "The user cannot be found on Discord: " + args);
DiscordPlugin.sendMessageToChannel(message.getChannel(), return true;
"Multiple users found with that (nick)name. Please specify the whole tag, like ChromaBot#6338 or use a ping."); }
return true; if (targets.size() > 1) {
} DiscordPlugin.sendMessageToChannel(message.getChannel(),
target = targets.get(0); "Multiple users found with that (nick)name. Please specify the whole tag, like ChromaBot#6338 or use a ping.");
} return true;
} }
try (DiscordPlayer dp = ChromaGamerBase.getUser(target.getStringID(), DiscordPlayer.class)) { target = targets.get(0);
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n"); }
uinfo.append(dp.getInfo(InfoTarget.Discord)); }
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString()); try (DiscordPlayer dp = ChromaGamerBase.getUser(target.getStringID(), DiscordPlayer.class)) {
} catch (Exception e) { StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n");
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while getting the user!"); uinfo.append(dp.getInfo(InfoTarget.Discord));
TBMCCoreAPI.SendException("Error while getting info about " + target.getName() + "!", e); DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
} } catch (Exception e) {
return true; DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while getting the user!");
} TBMCCoreAPI.SendException("Error while getting info about " + target.getName() + "!", e);
}
private List<IUser> getUsers(IMessage message, String args) { return true;
final List<IUser> targets; }
if (message.getChannel().isPrivate())
targets = DiscordPlugin.dc.getUsers().stream().filter(u -> u.getName().equalsIgnoreCase(args)) private List<IUser> getUsers(IMessage message, String args) {
.collect(Collectors.toList()); final List<IUser> targets;
else if (message.getChannel().isPrivate())
targets = message.getGuild().getUsersByName(args, true); targets = DiscordPlugin.dc.getUsers().stream().filter(u -> u.getName().equalsIgnoreCase(args))
return targets; .collect(Collectors.toList());
} else
targets = message.getGuild().getUsersByName(args, true);
@Override return targets;
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;
} }
} }