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
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
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.TBMCPlayerBase;
import com.google.common.collect.HashBiMap;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import sx.blah.discord.handle.obj.IMessage;
@CommandClass(helpText = {
"Connect command", //
@ -26,7 +26,8 @@ public class ConnectCommand extends ICommand2DC {
public static HashBiMap<String, String> WaitingToConnect = HashBiMap.create();
@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())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"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.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import sx.blah.discord.handle.obj.IMessage;
@CommandClass(helpText = {
"Switches debug mode."
})
public class DebugCommand extends ICommand2DC {
@Command2.Subcommand
public boolean def(IMessage message, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L))) //TODO: Make configurable
message.reply("Debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
public boolean def(Command2DCSender sender, String args) {
if (sender.getMessage().getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L))) //TODO: Make configurable
sender.sendMessage("debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
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;
}
}

View file

@ -53,9 +53,12 @@ public class CommandListener {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1) {
int i = cmdwithargs.indexOf(" ", mention.length());
//noinspection StatementWithEmptyBody
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
; //Removes any space before the command
if (i == -1)
i = mention.length();
else
//noinspection StatementWithEmptyBody
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
; //Removes any space before the command
cmdwithargs.delete(0, i);
cmdwithargs.insert(0, DiscordPlugin.getPrefix()); //Always use the prefix for processing
} else

View file

@ -5,6 +5,7 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.fun.FunModule;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.role.GameRoleModule;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
import lombok.val;
import sx.blah.discord.api.events.IListener;
@ -35,16 +36,20 @@ public class CommonListeners {
return;
if (FunModule.executeMemes(event.getMessage()))
return;
boolean handled = false;
if (event.getChannel().getLongID() == DiscordPlugin.plugin.CommandChannel().get().getLongID() //If mentioned, that's higher than chat
try {
boolean handled = false;
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
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
if (handled) return;
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
if (!handled)
handled = CommandListener.runCommand(event.getMessage(), false);
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
if (handled) return;
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
if (!handled)
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>() {
@Override

View file

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