diff --git a/src/main/java/me/alexander/discordbot/Main.java b/src/main/java/me/alexander/discordbot/Main.java index 3a523fc..ac17724 100644 --- a/src/main/java/me/alexander/discordbot/Main.java +++ b/src/main/java/me/alexander/discordbot/Main.java @@ -5,6 +5,11 @@ public class Main { + /** + * The version of this bot + */ + public static final String version = "0.4"; + /** * SelfBot instance */ @@ -26,6 +31,7 @@ public static void main(final String[] args) throws InterruptedException { Main.logger.log("Please provide your Discord account token as a command line argument.", LogType.CRITICAL); return; } + Main.logger.log("Starting SelfBot version " + version, LogType.INFO); Main.bot = new SelfBot(args[0]); Main.bot.setup(); } diff --git a/src/main/java/me/alexander/discordbot/SelfBot/ICommandSelfBot.java b/src/main/java/me/alexander/discordbot/SelfBot/ICommandSelfBot.java index fcadaf0..2457c74 100644 --- a/src/main/java/me/alexander/discordbot/SelfBot/ICommandSelfBot.java +++ b/src/main/java/me/alexander/discordbot/SelfBot/ICommandSelfBot.java @@ -6,10 +6,7 @@ import de.btobastian.javacord.entities.User; import de.btobastian.javacord.entities.message.Message; import de.btobastian.javacord.entities.message.embed.EmbedBuilder; -import de.btobastian.javacord.entities.permissions.PermissionState; import de.btobastian.javacord.entities.permissions.PermissionType; -import de.btobastian.javacord.entities.permissions.Permissions; -import de.btobastian.javacord.entities.permissions.Role; import me.alexander.discordbot.SelfBot.Messages.AutoDeleteMessage; import me.alexander.discordbot.SelfBot.Messages.EmbeddedMessage; @@ -45,21 +42,14 @@ public static void doCmd(final String in, final Message message, final SelfBot b new AutoDeleteMessage("Invalid argument", 2, message.getChannelReceiver()); break; } - User user = bot.getAPI().getUserById(args).get(); + User user = Utils.getUser(args, message.getChannelReceiver().getServer(), bot); if (user == null) { new AutoDeleteMessage("User not found", 2, message.getChannelReceiver()); break; } // Make sure we have permission to ban in this discord server - boolean allowed = false; - for (Role r : bot.getAPI().getYourself().getRoles(message.getChannelReceiver().getServer())) { - Permissions p = r.getPermissions(); - if (p.getState(PermissionType.BAN_MEMBERS).equals(PermissionState.ALLOWED)) { - allowed = true; - break; - } - } - if (!allowed) { + if (!Utils.hasPermission(bot.getAPI().getYourself(), message.getChannelReceiver().getServer(), + PermissionType.BAN_MEMBERS)) { new AutoDeleteMessage("You are not authorized to ban", 2, message.getChannelReceiver()); break; } diff --git a/src/main/java/me/alexander/discordbot/SelfBot/Utils.java b/src/main/java/me/alexander/discordbot/SelfBot/Utils.java index cbc07ed..0987509 100644 --- a/src/main/java/me/alexander/discordbot/SelfBot/Utils.java +++ b/src/main/java/me/alexander/discordbot/SelfBot/Utils.java @@ -1,5 +1,14 @@ package me.alexander.discordbot.SelfBot; +import java.util.concurrent.ExecutionException; + +import de.btobastian.javacord.entities.Server; +import de.btobastian.javacord.entities.User; +import de.btobastian.javacord.entities.permissions.PermissionState; +import de.btobastian.javacord.entities.permissions.PermissionType; +import de.btobastian.javacord.entities.permissions.Permissions; +import de.btobastian.javacord.entities.permissions.Role; + /** * * A collection of functions used internally by the bot @@ -9,4 +18,56 @@ */ public class Utils { + public Utils() {} + + /** + * Returns a user by a mention tag, id or name + * + * @param id + * @param server + * @return User + * @throws ExecutionException + * @throws InterruptedException + */ + public static User getUser(String id, Server server, SelfBot bot) throws InterruptedException, ExecutionException { + User user = bot.getAPI().getUserById(id).get(); + if (user == null) { + // Not a user id, or invalid user id + for (User u : server.getMembers()) { + if (u.getMentionTag().equals(id)) { + user = u; + break; + } else if (u.getName().equals(id)) { + user = u; + break; + } else if (u.getNickname(server).equals(id)) { + user = u; + break; + } else if (u.getId().equals(id)) { + user = u; + break; + } + } + } + return user; + } + + /** + * Checks if a given user has a permission + * + * @param user + * @param permission + * + * @return Boolean + */ + public static boolean hasPermission(User user, Server server, PermissionType permission) { + for (Role r : user.getRoles(server)) { + Permissions p = r.getPermissions(); + if (p.getState(permission).equals(PermissionState.ALLOWED)) { + return true; + } + } + return false; + } + }