diff --git a/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java b/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java index cf815b2..709fb65 100644 --- a/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java +++ b/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java @@ -45,7 +45,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener { lastannouncementtime = getConfig().getLong("lastannouncementtime"); lastseentime = getConfig().getLong("lastseentime"); GameRoles = (List) getConfig().getList("gameroles", new ArrayList()); - saveConfig(); ClientBuilder cb = new ClientBuilder(); cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8)); dc = cb.login(); diff --git a/src/main/java/buttondevteam/discordplugin/commands/RoleCommand.java b/src/main/java/buttondevteam/discordplugin/commands/RoleCommand.java index 6ebe986..ab9e133 100644 --- a/src/main/java/buttondevteam/discordplugin/commands/RoleCommand.java +++ b/src/main/java/buttondevteam/discordplugin/commands/RoleCommand.java @@ -24,23 +24,35 @@ public class RoleCommand extends DiscordCommandBase { } String[] argsa = args.split(" "); if (argsa[0].equalsIgnoreCase("add")) { - if (argsa.length < 2) { - DiscordPlugin.sendMessageToChannel(message.getChannel(), - "This command adds a game role to your account.\nUsage: add "); + final IRole role = checkAndGetRole(message, argsa, "This command adds a game role to your account."); + if (role == null) return; + try { + DiscordPlugin.perform(() -> message.getAuthor().addRole(role)); + DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added game role."); + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while adding role!", e); + DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role."); } - if (!DiscordPlugin.GameRoles.contains(argsa[1].toLowerCase())) { - DiscordPlugin.sendMessageToChannel(message.getChannel(), - "That game role cannot be found.\nList of game roles:\n" - + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n"))); + } else if (argsa[0].equalsIgnoreCase("remove")) { + final IRole role = checkAndGetRole(message, argsa, "This command removes a game role from your account."); + if (role == null) return; + try { + DiscordPlugin.perform(() -> message.getAuthor().removeRole(role)); + DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed game 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")) { + DiscordPlugin.sendMessageToChannel(message.getChannel(), + "List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n"))); + } else if (argsa[0].equalsIgnoreCase("addrole")) { final List roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer) .getRolesByName(argsa[1]); if (roles.size() == 0) { - DiscordPlugin.sendMessageToChannel(message.getChannel(), - "The specified role cannot be found on Discord! Removing from the list."); - DiscordPlugin.GameRoles.remove(argsa[1].toLowerCase()); + DiscordPlugin.sendMessageToChannel(message.getChannel(), "That role cannot be found on Discord."); return; } if (roles.size() > 1) { @@ -48,30 +60,44 @@ public class RoleCommand extends DiscordCommandBase { "There are more roles with this name. Why are there more roles with this name?"); return; } - try { - DiscordPlugin.perform(() -> message.getAuthor().addRole(roles.get(0))); - } 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")) - - { - if (argsa.length < 2) { - DiscordPlugin.sendMessageToChannel(message.getChannel(), - "This command removes a game role from your account.\nUsage: remove "); - return; - } - } else if (argsa[0].equalsIgnoreCase("list")) { - DiscordPlugin.sendMessageToChannel(message.getChannel(), - "List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n"))); + DiscordPlugin.GameRoles.add(roles.get(0).getName()); + DiscordPlugin.sendMessageToChannel(message.getChannel(), "Game role added."); } } + private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) { + if (argsa.length < 2) { + DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " "); + return null; + } + if (!DiscordPlugin.GameRoles.contains(argsa[1])) { + DiscordPlugin.sendMessageToChannel(message.getChannel(), + "That game role cannot be found.\nList of game roles:\n" + + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n"))); + return null; + } + final List roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer) + .getRolesByName(argsa[1]); + if (roles.size() == 0) { + DiscordPlugin.sendMessageToChannel(message.getChannel(), + "The specified role cannot be found on Discord! Removing from the list."); + DiscordPlugin.GameRoles.remove(argsa[1].toLowerCase()); + 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?"); + return null; + } + return roles.get(0); + } + @Override public String[] getHelpText() { - // TODO Auto-generated method stub - return null; + return new String[] { // + "Add or remove game roles from yourself.", // + "Usage: role add|remove or role list", // + "Mods can use role addrole to add a role as a game role" }; } }