Command system fixes

In encounter order:
Lowercasing message first character on the sender
Command sender fixes
Mention deleting fixed if there is no space
Added error handler to the message receive event
Converted role command
This commit is contained in:
Norbi Peti 2019-02-23 00:33:10 +01:00
parent 325b094bf7
commit ca5fb90774
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 80 additions and 83 deletions

View file

@ -12,11 +12,14 @@ public class Command2DCSender implements Command2Sender {
@Override @Override
public void sendMessage(String message) { public void sendMessage(String message) {
this.message.reply(DPUtils.sanitizeString(message)); if (message.length() == 0) return;
message = DPUtils.sanitizeString(message);
message = Character.toLowerCase(message.charAt(0)) + message.substring(1);
this.message.reply(message);
} }
@Override @Override
public void sendMessage(String[] message) { public void sendMessage(String[] message) {
this.message.reply(DPUtils.sanitizeString(String.join("\n", message))); sendMessage(String.join("\n", message));
} }
} }

View file

@ -8,10 +8,10 @@ import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase; import buttondevteam.lib.player.TBMCPlayerBase;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import sx.blah.discord.handle.obj.IMessage;
@CommandClass(helpText = { @CommandClass(helpText = {
"Connect command", // "Connect command", //
@ -26,7 +26,8 @@ public class ConnectCommand extends ICommand2DC {
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create(); public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
@Command2.Subcommand @Command2.Subcommand
public boolean def(IMessage message, String Minecraftname) { public boolean def(Command2DCSender sender, String Minecraftname) {
val message = sender.getMessage();
if (WaitingToConnect.inverse().containsKey(message.getAuthor().getStringID())) { if (WaitingToConnect.inverse().containsKey(message.getAuthor().getStringID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Replacing " + WaitingToConnect.inverse().get(message.getAuthor().getStringID()) + " with " + Minecraftname); "Replacing " + WaitingToConnect.inverse().get(message.getAuthor().getStringID()) + " with " + Minecraftname);

View file

@ -4,18 +4,17 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.listeners.CommonListeners; import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage;
@CommandClass(helpText = { @CommandClass(helpText = {
"Switches debug mode." "Switches debug mode."
}) })
public class DebugCommand extends ICommand2DC { public class DebugCommand extends ICommand2DC {
@Command2.Subcommand @Command2.Subcommand
public boolean def(IMessage message, String args) { public boolean def(Command2DCSender sender, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L))) //TODO: Make configurable if (sender.getMessage().getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L))) //TODO: Make configurable
message.reply("Debug " + (CommonListeners.debug() ? "enabled" : "disabled")); sender.sendMessage("debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
else else
message.reply("You need to be a moderator to use this command."); sender.sendMessage("you need to be a moderator to use this command.");
return true; return true;
} }
} }

View file

@ -53,6 +53,9 @@ public class CommandListener {
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) {
int i = cmdwithargs.indexOf(" ", mention.length()); int i = cmdwithargs.indexOf(" ", mention.length());
if (i == -1)
i = mention.length();
else
//noinspection StatementWithEmptyBody //noinspection StatementWithEmptyBody
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++) for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
; //Removes any space before the command ; //Removes any space before the command

View file

@ -5,6 +5,7 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.fun.FunModule; import buttondevteam.discordplugin.fun.FunModule;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule; import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.role.GameRoleModule; import buttondevteam.discordplugin.role.GameRoleModule;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import lombok.val; import lombok.val;
import sx.blah.discord.api.events.IListener; import sx.blah.discord.api.events.IListener;
@ -35,6 +36,7 @@ public class CommonListeners {
return; return;
if (FunModule.executeMemes(event.getMessage())) if (FunModule.executeMemes(event.getMessage()))
return; return;
try {
boolean handled = false; boolean handled = false;
if (event.getChannel().getLongID() == DiscordPlugin.plugin.CommandChannel().get().getLongID() //If mentioned, that's higher than chat if (event.getChannel().getLongID() == DiscordPlugin.plugin.CommandChannel().get().getLongID() //If mentioned, that's higher than chat
|| event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels || event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels
@ -45,6 +47,9 @@ public class CommonListeners {
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
if (!handled) if (!handled)
handled = CommandListener.runCommand(event.getMessage(), false); handled = CommandListener.runCommand(event.getMessage(), false);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling a message!", e);
}
} }
}, new IListener<sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent>() { }, new IListener<sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent>() {
@Override @Override

View file

@ -2,18 +2,18 @@ package buttondevteam.discordplugin.role;
import buttondevteam.discordplugin.DPUtils; import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.Command2DCSender;
import buttondevteam.discordplugin.commands.ICommand2DC; import buttondevteam.discordplugin.commands.ICommand2DC;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IRole; import sx.blah.discord.handle.obj.IRole;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@CommandClass @CommandClass
public class RoleCommand extends ICommand2DC { //TODO public class RoleCommand extends ICommand2DC {
private GameRoleModule grm; private GameRoleModule grm;
@ -21,78 +21,64 @@ public class RoleCommand extends ICommand2DC { //TODO
this.grm = grm; this.grm = grm;
} }
@Command2.Subcommand @Command2.Subcommand(helpText = {
public boolean def(IMessage message, String args) { "Add role",
if (args.length() == 0) "This command adds a role to your account."
return false; })
String[] argsa = splitargs(args); public boolean add(Command2DCSender sender, @Command2.TextArg String rolename) {
if (argsa[0].equalsIgnoreCase("add")) { final IRole role = checkAndGetRole(sender, rolename);
final IRole role = checkAndGetRole(message, argsa, "This command adds a role to your account.");
if (role == null) if (role == null)
return true; return true;
try { try {
DPUtils.perform(() -> message.getAuthor().addRole(role)); DPUtils.perform(() -> sender.getMessage().getAuthor().addRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added role."); sender.sendMessage("added role.");
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e); TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role."); sender.sendMessage("an error occured while adding the role.");
} }
} else if (argsa[0].equalsIgnoreCase("remove")) { return true;
final IRole role = checkAndGetRole(message, argsa, "This command removes a role from your account."); }
@Command2.Subcommand(helpText = {
"Remove role",
"This command removes a role from your account."
})
public boolean remove(Command2DCSender sender, @Command2.TextArg String rolename) {
final IRole role = checkAndGetRole(sender, rolename);
if (role == null) if (role == null)
return true; return true;
try { try {
DPUtils.perform(() -> message.getAuthor().removeRole(role)); DPUtils.perform(() -> sender.getMessage().getAuthor().removeRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed role."); sender.sendMessage("removed role.");
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while removing role!", e); TBMCCoreAPI.SendException("Error while removing role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while removing the role."); sender.sendMessage("an error occured while removing the role.");
} }
} else if (argsa[0].equalsIgnoreCase("list")) {
listRoles(message);
} else return false;
return true; return true;
} }
private void listRoles(IMessage message) { @Command2.Subcommand
DiscordPlugin.sendMessageToChannel(message.getChannel(), public void list(Command2DCSender sender) {
"List of roles:\n" + grm.GameRoles.stream().sorted().collect(Collectors.joining("\n"))); sender.sendMessage("list of roles:\n" + grm.GameRoles.stream().sorted().collect(Collectors.joining("\n")));
} }
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) { private IRole checkAndGetRole(Command2DCSender sender, String rolename) {
if (argsa.length < 2) { if (!grm.GameRoles.contains(rolename)) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>"); sender.sendMessage("that role cannot be found.");
list(sender);
return null; return null;
} }
StringBuilder rolename = new StringBuilder(argsa[1]); final List<IRole> roles = DiscordPlugin.mainServer.getRolesByName(rolename);
for (int i = 2; i < argsa.length; i++)
rolename.append(" ").append(argsa[i]);
if (!grm.GameRoles.contains(rolename.toString())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "That role cannot be found.");
listRoles(message);
return null;
}
final List<IRole> roles = DiscordPlugin.mainServer.getRolesByName(rolename.toString());
if (roles.size() == 0) { if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), sender.sendMessage("the specified role cannot be found on Discord! Removing from the list.");
"The specified role cannot be found on Discord! Removing from the list."); grm.GameRoles.remove(rolename);
grm.GameRoles.remove(rolename.toString());
return null; return null;
} }
if (roles.size() > 1) { if (roles.size() > 1) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), sender.sendMessage("there are multiple roles with this name. Why are there multiple roles with this name?");
"There are more roles with this name. Why are there more roles with this name?");
return null; return null;
} }
return roles.get(0); return roles.get(0);
} }
@Override
public String[] getHelpText() {
return new String[]{ //
"Add or remove roles from yourself.", //
"Usage: " + DiscordPlugin.getPrefix() + "role add|remove <name> or role list", //
};
}
} }