    001package co.aikar.commands;
    002
    003import net.dv8tion.jda.core.Permission;
    004
    005import java.util.HashMap;
    006import java.util.Map;
    007
    008public class JDACommandPermissionResolver implements CommandPermissionResolver {
    009    private Map<String, Integer> discordPermissionOffsets;
    010
    011    public JDACommandPermissionResolver() {
    012        discordPermissionOffsets = new HashMap<>();
    013        for (Permission permission : Permission.values()) {
    014            discordPermissionOffsets.put(permission.name().toLowerCase().replaceAll("_", "-"), permission.getOffset());
    015        }
    016    }
    017
    018    @Override
    019    public boolean hasPermission(JDACommandManager manager, JDACommandEvent event, String permission) {
    020        // Explicitly return true if the issuer is the bot's owner. They are always allowed.
    021        if (manager.getBotOwnerId() == event.getIssuer().getAuthor().getIdLong()) {
    022            return true;
    023        }
    024
    025        // Return false on webhook messages, as they cannot have permissions defined.
    026        if (event.getIssuer().isWebhookMessage()) {
    027            return false;
    028        }
    029
    030        Integer permissionOffset = discordPermissionOffsets.get(permission);
    031        if (permissionOffset == null) {
    032            return false;
    033        }
    034
    035        return event.getIssuer().getMember().hasPermission(
    036                Permission.getFromOffset(permissionOffset)
    037        );
    038    }
    039}