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

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

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.lib.chat.Command2Sender;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@ -11,11 +12,11 @@ public class Command2DCSender implements Command2Sender {
@Override
public void sendMessage(String message) {
this.message.reply(message);
this.message.reply(DPUtils.sanitizeString(message));
}
@Override
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() {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
commands.put("userinfo", new UserinfoCommand());
commands.put("help", new HelpCommand());
commands.put("mcchat", new MCChatCommand());
commands.put("channelcon", new ChannelconCommand());

View file

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

View file

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

View file

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

View file

@ -21,7 +21,7 @@ public class CommandListener {
final IChannel channel = message.getChannel();
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
if (!message.getChannel().isPrivate()
&& !(message.getContent().charAt(0) == DiscordPlugin.getPrefix()
&& !(message.getContent().charAt(0) == DiscordPlugin.getPrefix()
&& channel.getStringID().equals(DiscordPlugin.plugin.CommandChannel().get().getStringID()))) //
return false;
message.getChannel().setTypingStatus(true); // Fun
@ -38,9 +38,10 @@ public class CommandListener {
return false;
}
message.getChannel().setTypingStatus(true);
String cmdwithargsString = cmdwithargs.toString().trim(); //Remove spaces between mention and command
String cmdwithargsString = cmdwithargs.toString();
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) {
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) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1)
cmdwithargs.delete(0,
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
else
cmdwithargs.replace(0, cmdwithargs.length(), "help");
if (cmdwithargs.length() > mention.length() + 1) {
int x = mention.length(), px = x;
while ((x = cmdwithargs.indexOf(" ", px + 1)) != -1) //Removes any space before the command
px = x;
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 {
if (cmdwithargs.length() > 0 && cmdwithargs.charAt(0) == '/')
cmdwithargs.deleteCharAt(0); //Don't treat / as mention, mentions can be used in public mcchat
return false;
return false; //Don't treat / as mention, mentions can be used in public mcchat
}
if (cmdwithargs.length() == 0)
cmdwithargs.replace(0, cmdwithargs.length(), "help");
cmdwithargs.replace(0, cmdwithargs.length(), DiscordPlugin.getPrefix() + "help");
return true;
}
}