Added static and discord permission handlers
This commit is contained in:
parent
60f9b4e3b2
commit
74d4c3c4ce
@ -5,32 +5,43 @@
|
||||
|
||||
package dev.salmonllama.fsbot.guthix;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
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 PermissionManager() {
|
||||
}
|
||||
|
||||
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:
|
||||
return true;
|
||||
case ROLE:
|
||||
// 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:
|
||||
// If the author is the owner, yes.
|
||||
return ownerHandler(ctx);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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");
|
||||
return false;
|
||||
}
|
||||
@ -41,6 +52,27 @@ public class PermissionManager {
|
||||
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) {
|
||||
return !author.isBotUser() && !author.isWebhook();
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package dev.salmonllama.fsbot.guthix;
|
||||
|
||||
public enum PermissionType {
|
||||
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
|
||||
NONE // No requirement
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user