From d0ee564a7c2d5c16d56ab30de968a9d3d841f114 Mon Sep 17 00:00:00 2001 From: "U-NIFLHEIM\\Lax" Date: Sun, 13 May 2018 18:53:43 -0700 Subject: [PATCH] Role commands --- src/main/java/org/moss/discord/Constants.java | 3 + src/main/java/org/moss/discord/Main.java | 4 +- .../moss/discord/commands/RoleCommand.java | 80 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/moss/discord/commands/RoleCommand.java diff --git a/src/main/java/org/moss/discord/Constants.java b/src/main/java/org/moss/discord/Constants.java index 4ec267e..3b9ec9b 100644 --- a/src/main/java/org/moss/discord/Constants.java +++ b/src/main/java/org/moss/discord/Constants.java @@ -16,6 +16,9 @@ public class Constants { // Roles public static final String ROLE_MODERATOR = "390944427071242240"; + public static final String ROLE_ADMIN = "390970629261623296"; + public static final String ROLE_PROJECT_LEAD = " 390943316335788034"; + public static final String ROLE_CONTRIBUTOR = "395277849877151767"; public static final String ROLE_PURCHASER = "401686955336663041"; public static final String ROLE_PURCHASER_PVX = "401687126242230274"; diff --git a/src/main/java/org/moss/discord/Main.java b/src/main/java/org/moss/discord/Main.java index 7f67010..d1c3400 100644 --- a/src/main/java/org/moss/discord/Main.java +++ b/src/main/java/org/moss/discord/Main.java @@ -5,6 +5,7 @@ import org.javacord.api.DiscordApi; import org.javacord.api.DiscordApiBuilder; import org.moss.discord.commands.BStatsCommand; +import org.moss.discord.commands.RoleCommand; import org.moss.discord.listeners.StarboardListener; import org.moss.discord.commands.GithubCommand; import org.moss.discord.commands.MojangCommand; @@ -47,7 +48,8 @@ public static void main(String[] args) { commandHandler.registerCommand(new PruneCommand()); commandHandler.registerCommand(new MojangCommand()); commandHandler.registerCommand(new SpigetCommand()); - + commandHandler.registerCommand(new RoleCommand()); + // Register listeners api.addListener(new ModLogListeners(api)); api.addReactionAddListener(new StarboardListener(api)); diff --git a/src/main/java/org/moss/discord/commands/RoleCommand.java b/src/main/java/org/moss/discord/commands/RoleCommand.java new file mode 100644 index 0000000..f4f0a81 --- /dev/null +++ b/src/main/java/org/moss/discord/commands/RoleCommand.java @@ -0,0 +1,80 @@ +package org.moss.discord.commands; + +import de.btobastian.sdcf4j.Command; +import de.btobastian.sdcf4j.CommandExecutor; +import org.javacord.api.DiscordApi; +import org.javacord.api.entity.channel.TextChannel; +import org.javacord.api.entity.message.Message; +import org.javacord.api.entity.message.embed.EmbedBuilder; +import org.javacord.api.entity.permission.Role; +import org.javacord.api.entity.server.Server; +import org.javacord.api.entity.user.User; +import org.moss.discord.Constants; + +import java.awt.*; +import java.util.List; + +public class RoleCommand implements CommandExecutor { + + @Command(aliases = {"!role", ".role"}, usage = "!role ", description = "Give user a role") + public void onRole(DiscordApi api, TextChannel channel, User user, Server server, Message message, String[] args) { + if (args.length >= 3 && hasPermission(user.getRoles(server))) { + User target = message.getMentionedUsers().get(0); + Role role = server.getRolesByNameIgnoreCase(args[1]).get(0); + if (target != null && role != null) { + if (args[0].equalsIgnoreCase("give")) { + target.addRole(role, "Role given by " + user.getName()); + channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setTitle("Role given")); + } + if (args[0].equalsIgnoreCase("take")) { + target.removeRole(role, "Role taken by " + user.getName()); + channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setTitle("Role Taken")); + } + } else { + channel.sendMessage(new EmbedBuilder().setColor(Color.RED).setTitle("No user/role found")); + } + } + } + + @Command(aliases = {"!contributor", ".contributor"}, usage = ".contributor ", description = "Allows project leads to give contributor roles.") + public void onContributor(DiscordApi api, TextChannel channel, User user, Server server, Message message, String[] args) { + if (args.length >= 1 && hasRole(user, user.getRoles(server), "Project Lead")) { + User target = message.getMentionedUsers().get(0); + if (target != null) { + target.addRole(server.getRoleById(Constants.ROLE_CONTRIBUTOR).get(), "Role given by " + user.getName()); + channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setTitle("Role given")); + } else { + channel.sendMessage(new EmbedBuilder().setColor(Color.RED).setTitle("No user found")); + } + + } + } + + public Boolean hasPermission(List roles) { //TODO 1 stream class + for (Role role : roles) { + String roleId = role.getIdAsString(); + if ((roleId.equals(Constants.ROLE_MODERATOR) || roleId.equals(Constants.ROLE_ADMIN))) { + return true; + } + } + return false; + } + + public Boolean hasRole(User user, List roles, String role) { //TODO 1 class + for (Role roll : roles) { + if (roll.getName().equalsIgnoreCase(role)) { + return true; + } + } + return false; + } + + public Boolean isRole(List roles, String role) { //TODO 1 class + for (Role roll : roles) { + if (roll.getName().equalsIgnoreCase(role)) { + return true; + } + } + return false; + } +}