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:
parent
325b094bf7
commit
ca5fb90774
6 changed files with 80 additions and 83 deletions
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,9 +53,12 @@ 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());
|
||||||
//noinspection StatementWithEmptyBody
|
if (i == -1)
|
||||||
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
|
i = mention.length();
|
||||||
; //Removes any space before the command
|
else
|
||||||
|
//noinspection StatementWithEmptyBody
|
||||||
|
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
|
||||||
|
; //Removes any space before the command
|
||||||
cmdwithargs.delete(0, i);
|
cmdwithargs.delete(0, i);
|
||||||
cmdwithargs.insert(0, DiscordPlugin.getPrefix()); //Always use the prefix for processing
|
cmdwithargs.insert(0, DiscordPlugin.getPrefix()); //Always use the prefix for processing
|
||||||
} else
|
} else
|
||||||
|
|
|
@ -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,16 +36,20 @@ public class CommonListeners {
|
||||||
return;
|
return;
|
||||||
if (FunModule.executeMemes(event.getMessage()))
|
if (FunModule.executeMemes(event.getMessage()))
|
||||||
return;
|
return;
|
||||||
boolean handled = false;
|
try {
|
||||||
if (event.getChannel().getLongID() == DiscordPlugin.plugin.CommandChannel().get().getLongID() //If mentioned, that's higher than chat
|
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
|
|| event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels
|
||||||
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
|
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
|
||||||
if (handled) return;
|
if (handled) return;
|
||||||
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
|
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
|
||||||
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
|
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
|
||||||
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
|
||||||
|
|
|
@ -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(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
|
@Command2.Subcommand
|
||||||
public boolean def(IMessage message, String args) {
|
public void list(Command2DCSender sender) {
|
||||||
if (args.length() == 0)
|
sender.sendMessage("list of roles:\n" + grm.GameRoles.stream().sorted().collect(Collectors.joining("\n")));
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void listRoles(IMessage message) {
|
private IRole checkAndGetRole(Command2DCSender sender, String rolename) {
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
if (!grm.GameRoles.contains(rolename)) {
|
||||||
"List of roles:\n" + grm.GameRoles.stream().sorted().collect(Collectors.joining("\n")));
|
sender.sendMessage("that role cannot be found.");
|
||||||
}
|
list(sender);
|
||||||
|
|
||||||
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) {
|
|
||||||
if (argsa.length < 2) {
|
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
|
|
||||||
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", //
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue