Added static and discord permission handlers

This commit is contained in:
Aleksei 2020-07-23 14:21:34 -04:00
parent 60f9b4e3b2
commit 74d4c3c4ce
2 changed files with 40 additions and 6 deletions

View File

@ -5,32 +5,43 @@
package dev.salmonllama.fsbot.guthix; package dev.salmonllama.fsbot.guthix;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import org.javacord.api.entity.message.MessageAuthor; import org.javacord.api.entity.message.MessageAuthor;
import dev.salmonllama.fsbot.database.controllers.StaticPermissionController;
import dev.salmonllama.fsbot.database.models.StaticPermission;
public class PermissionManager { public class PermissionManager {
public PermissionManager() { public PermissionManager() {
} }
public boolean hasPermission(CommandPermission reqPerm, CommandContext ctx) { public boolean hasPermission(CommandPermission reqPerm, CommandContext ctx) {
PermissionType permtype = reqPerm.getType(); PermissionType permType = reqPerm.getType();
String permValue = reqPerm.getValue();
switch (permtype) { switch (permType) {
case NONE: case NONE:
return true; return true;
case ROLE: case ROLE:
// If the author has the role, yes. Doesn't work in DM // If the author has the role, yes. Doesn't work in DM
return roleHandler(reqPerm.getValue(), ctx); return roleHandler(permValue, ctx);
case STATIC:
return staticHandler(permValue, ctx);
case PERMISSION:
return permissionHandler(ctx);
case OWNER: case OWNER:
// If the author is the owner, yes. // If the author is the owner, yes.
return ownerHandler(ctx); return ownerHandler(ctx);
default:
return false;
} }
return false;
} }
private boolean roleHandler(String roleId, CommandContext ctx) { private boolean roleHandler(String roleId, CommandContext ctx) {
if (!ctx.getUserRoles().isPresent()) { if (ctx.getUserRoles().isEmpty()) {
ctx.reply("This command can only be used in a server"); ctx.reply("This command can only be used in a server");
return false; return false;
} }
@ -41,6 +52,27 @@ public class PermissionManager {
return ctx.isUserOwner(); return ctx.isUserOwner();
} }
private boolean permissionHandler(CommandContext ctx) {
// Not implemented yet
return false;
}
private boolean staticHandler(String staticPerm, CommandContext ctx) {
AtomicBoolean ret = new AtomicBoolean(false);
StaticPermissionController.getByUser(ctx.getAuthor().getIdAsString()).thenAccept(possiblePerms -> {
possiblePerms.ifPresent(
staticPermissions -> ret.set(
staticPermissions.stream()
.map(StaticPermission::getPermission)
.anyMatch(staticPerm::equals)
)
);
});
return ret.get();
}
boolean sourceIsValid(MessageAuthor author) { boolean sourceIsValid(MessageAuthor author) {
return !author.isBotUser() && !author.isWebhook(); return !author.isBotUser() && !author.isWebhook();
} }

View File

@ -7,6 +7,8 @@ package dev.salmonllama.fsbot.guthix;
public enum PermissionType { public enum PermissionType {
ROLE, // User has a specific role inside the server ROLE, // User has a specific role inside the server
PERMISSION, // User has a specific Discord permission bit
STATIC, // User has a pre-defined permission stored in the database
OWNER, // User is the bot owner OWNER, // User is the bot owner
NONE // No requirement NONE // No requirement
} }