Skip to content

Commit

Permalink
Added better functions to find a user and check user permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Moudoux committed May 15, 2017
1 parent 562ad77 commit a2fc9f8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/main/java/me/alexander/discordbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

public class Main {

/**
* The version of this bot
*/
public static final String version = "0.4";

/**
* SelfBot instance
*/
Expand All @@ -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();
}
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/me/alexander/discordbot/SelfBot/ICommandSelfBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/me/alexander/discordbot/SelfBot/Utils.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

}

0 comments on commit a2fc9f8

Please sign in to comment.