From 0ff139928b9754d717711400baae05974d3ecd28 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 15 Nov 2016 20:46:36 +0100 Subject: [PATCH] Added support for role mentions After a while of trying to test it locally while my PC is already busy, I decided on continuing to test in production. This is alpha stage afterall. --- .../discordplugin/CommandListener.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/buttondevteam/discordplugin/CommandListener.java b/src/main/java/buttondevteam/discordplugin/CommandListener.java index 62c98cf..ce08bd5 100644 --- a/src/main/java/buttondevteam/discordplugin/CommandListener.java +++ b/src/main/java/buttondevteam/discordplugin/CommandListener.java @@ -1,9 +1,5 @@ package buttondevteam.discordplugin; -import java.util.stream.Collectors; - -import org.bukkit.Bukkit; - import buttondevteam.discordplugin.commands.DiscordCommandBase; import sx.blah.discord.api.events.IListener; import sx.blah.discord.handle.impl.events.MentionEvent; @@ -41,16 +37,10 @@ public class CommandListener { String cmdwithargs = message.getContent(); final String mention = DiscordPlugin.dc.getOurUser().mention(false); final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true); - if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text - if (cmdwithargs.length() > mention.length() + 1) - cmdwithargs = cmdwithargs.substring(mention.length() + 1); - else - cmdwithargs = "help"; - if (message.getContent().startsWith(mentionNick)) - if (cmdwithargs.length() > mentionNick.length() + 1) - cmdwithargs = cmdwithargs.substring(mentionNick.length() + 1); - else - cmdwithargs = "help"; + checkanddeletemention(cmdwithargs, mention, message); + checkanddeletemention(cmdwithargs, mentionNick, message); + for (String mentionRole : (Iterable) message.getRoleMentions().stream().map(r -> r.mention())::iterator) + cmdwithargs = checkanddeletemention(cmdwithargs, mentionRole, message); int index = cmdwithargs.indexOf(' '); String cmd; String args; @@ -64,4 +54,14 @@ public class CommandListener { DiscordCommandBase.runCommand(cmd, args, message); message.getChannel().setTypingStatus(false); } + + private static String checkanddeletemention(String 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 = cmdwithargs.substring( + cmdwithargs.charAt(mention.length() + 1) == ' ' ? mention.length() + 1 : mention.length()); + else + cmdwithargs = "help"; + return cmdwithargs; + } }