Search in sources :

Example 1 with AccessConfiguration

use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.

the class AlertAccessConfigurationModificationInterceptor method getPermissionTypePrefix.

private String getPermissionTypePrefix(List<AccessConfiguration> accessConfigurations, boolean capitalize) {
    boolean allOfSameType = true;
    PermissionTarget.TargetType type = null;
    for (AccessConfiguration deletedAccessConfiguration : accessConfigurations) {
        PermissionTarget.TargetType targetType = deletedAccessConfiguration.getPermissionType().asEnum();
        if (type != null && targetType != type) {
            allOfSameType = false;
            break;
        } else if (type == null) {
            type = targetType;
        }
    }
    String prefix;
    if (allOfSameType) {
        // type is not null since deletedAccessConfigurations is not empty
        // noinspection ConstantConditions
        String typeName = type.getName();
        if (capitalize) {
            if (typeName.length() > 1) {
                prefix = typeName.substring(0, 1).toUpperCase() + typeName.substring(1);
            } else {
                prefix = typeName.toUpperCase();
            }
        } else {
            prefix = typeName;
        }
    } else if (capitalize) {
        prefix = "Permission";
    } else {
        prefix = "permission";
    }
    return prefix;
}
Also used : AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 2 with AccessConfiguration

use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.

the class DeleteGrantedRolesForDeletedRolesTask method run.

@Override
protected void run(JobExecutionContext jobExecutionContext) {
    Aiode aiode = Aiode.get();
    ShardManager shardManager = aiode.getShardManager();
    QueryBuilderFactory queryBuilderFactory = aiode.getQueryBuilderFactory();
    StaticSessionProvider.consumeSession(session -> {
        List<GuildSpecification> guildSpecifications = queryBuilderFactory.find(GuildSpecification.class).build(session).getResultList();
        int deletionCounter = 0;
        for (GuildSpecification guildSpecification : guildSpecifications) {
            Guild guild = guildSpecification.getGuild(shardManager);
            if (guild == null) {
                continue;
            }
            for (AccessConfiguration accessConfiguration : guildSpecification.getAccessConfigurations()) {
                Set<GrantedRole> grantedRoles = accessConfiguration.getRoles();
                for (GrantedRole grantedRole : grantedRoles) {
                    Role guildRole = grantedRole.getRole(guild);
                    if (guildRole == null) {
                        // role has been deleted
                        accessConfiguration.removeRole(grantedRole);
                        session.delete(grantedRole);
                        ++deletionCounter;
                    }
                }
            }
        }
        if (deletionCounter > 0) {
            logger.info("Deleted " + deletionCounter + " GrantedRole entities for roles that no longer exist");
        }
    });
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 3 with AccessConfiguration

use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.

the class GuildManager method initializeGuild.

private GuildContext initializeGuild(Guild guild) {
    GuildContext createdContext = hibernateComponent.invokeWithSession(session -> {
        AudioPlayer player = audioManager.getPlayerManager().createPlayer();
        Optional<Long> existingSpecification = queryBuilderFactory.select(GuildSpecification.class, "pk", Long.class).where((cb, root) -> cb.equal(root.get("guildId"), guild.getId())).build(session).uniqueResultOptional();
        if (existingSpecification.isPresent()) {
            return new GuildContext(guild, new AudioPlayback(player, guild), existingSpecification.get());
        } else {
            GuildSpecification newSpecification = new GuildSpecification(guild);
            session.persist(newSpecification);
            commandManager.getCommandContributionContext().query(attribute("restrictedAccess").is(true), CommandContribution.class).getResultStream().forEach(restrictedCommand -> {
                AccessConfiguration permissionConfiguration = new AccessConfiguration(restrictedCommand, session);
                session.persist(permissionConfiguration);
                newSpecification.addAccessConfiguration(permissionConfiguration);
            });
            session.flush();
            GuildContext guildContext = new GuildContext(guild, new AudioPlayback(player, guild), newSpecification.getPk());
            handleNewGuild(guild, guildContext);
            return guildContext;
        }
    });
    guildContexts.put(guild, createdContext);
    return createdContext;
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 4 with AccessConfiguration

use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.

the class PermissionListPaginationWidget method getColumns.

@SuppressWarnings("unchecked")
@Override
protected AbstractPaginationWidget.Column<PermissionTarget>[] getColumns() {
    return new AbstractPaginationWidget.Column[] { new Column<PermissionTarget>("Permission", permissionTarget -> {
        int childTargetLevel = 0;
        PermissionTarget currentParent = permissionTarget.getParentTarget();
        while (currentParent != null) {
            childTargetLevel++;
            currentParent = currentParent.getParentTarget();
        }
        if (childTargetLevel == 0) {
            return permissionTarget.getPermissionTargetIdentifier();
        } else {
            return "-".repeat(childTargetLevel) + " *" + permissionTarget.getPermissionTargetIdentifier() + "*";
        }
    }, permissionTarget -> {
        PermissionTarget rootTarget = permissionTarget;
        PermissionTarget parent;
        while ((parent = rootTarget.getParentTarget()) != null) {
            rootTarget = parent;
        }
        PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
        int categorySorting = permissionTypeCategory.getSorting();
        return EmbedTable.Group.namedGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
    }), new Column<PermissionTarget>("Available to", permissionTarget -> {
        Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getGuild().getIdString());
        if (accessConfiguration.isPresent()) {
            List<Role> roles = accessConfiguration.get().getRoles(getGuild().get());
            if (roles.isEmpty()) {
                return "Guild owner and administrator roles only";
            } else {
                return StringList.create(roles, Role::getName).toSeparatedString(", ");
            }
        } else {
            return "Available to everyone";
        }
    }, permissionTarget -> {
        PermissionTarget rootTarget = permissionTarget;
        PermissionTarget parent;
        while ((parent = rootTarget.getParentTarget()) != null) {
            rootTarget = parent;
        }
        PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
        int categorySorting = permissionTypeCategory.getSorting();
        return EmbedTable.Group.silentGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
    }) };
}
Also used : Role(net.dv8tion.jda.api.entities.Role) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget)

Example 5 with AccessConfiguration

use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.

the class HelpCommand method showCommandHelp.

private void showCommandHelp() {
    getManager().getCommand(getContext(), getCommandInput()).ifPresentOrElse(command -> {
        String prefix;
        GuildSpecification specification = getContext().getGuildContext().getSpecification();
        String setPrefix = specification.getPrefix();
        String botName = specification.getBotName();
        if (!Strings.isNullOrEmpty(setPrefix)) {
            prefix = setPrefix;
        } else if (!Strings.isNullOrEmpty(botName)) {
            prefix = botName + " ";
        } else {
            prefix = PrefixProperty.DEFAULT_FALLBACK + " ";
        }
        char argumentPrefix = ArgumentPrefixProperty.getForCurrentContext().getArgumentPrefix();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle("Command " + command.getIdentifier() + ":");
        String descriptionFormat = command.getDescription();
        String descriptionText = String.format(descriptionFormat, prefix, argumentPrefix);
        embedBuilder.setDescription(descriptionText);
        Guild guild = getContext().getGuild();
        Optional<AccessConfiguration> accessConfiguration = Aiode.get().getSecurityManager().getAccessConfiguration(command.getPermissionTarget(), guild);
        if (accessConfiguration.isPresent()) {
            String title = "Available to roles: ";
            String text;
            List<Role> roles = accessConfiguration.get().getRoles(guild);
            if (!roles.isEmpty()) {
                text = StringList.create(roles, Role::getName).toSeparatedString(", ");
            } else {
                text = "Guild owner and administrator roles only";
            }
            embedBuilder.addField(title, text, false);
        }
        ArgumentController argumentController = command.getArgumentController();
        if (argumentController.hasArguments()) {
            embedBuilder.addField("__Arguments__", "Keywords that alter the command behavior or define a search scope.", false);
            argumentController.getArguments().values().stream().sorted(Comparator.comparing(CommandArgument::getIdentifier)).forEach(argument -> embedBuilder.addField(argumentPrefix + argument.getIdentifier(), String.format(argument.getDescription(), prefix, argumentPrefix), false));
        }
        List<XmlElement> examples = command.getCommandContribution().query(tagName("example")).collect();
        if (!examples.isEmpty()) {
            embedBuilder.addField("__Examples__", "Practical usage examples for this command.", false);
            for (XmlElement example : examples) {
                String exampleText = String.format(example.getTextContent(), prefix, argumentPrefix);
                String titleText = String.format(example.getAttribute("title").getValue(), prefix, argumentPrefix);
                embedBuilder.addField(titleText, exampleText, false);
            }
        }
        sendMessage(embedBuilder);
    }, () -> {
        throw new InvalidCommandException(String.format("No command found for '%s'", getCommandInput()));
    });
}
Also used : CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) Guild(net.dv8tion.jda.api.entities.Guild) Role(net.dv8tion.jda.api.entities.Role) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) XmlElement(net.robinfriedli.jxp.api.XmlElement) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Aggregations

AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)13 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)8 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)8 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)7 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)7 Role (net.dv8tion.jda.api.entities.Role)6 Session (org.hibernate.Session)6 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)5 CommandContext (net.robinfriedli.aiode.command.CommandContext)4 Guild (net.dv8tion.jda.api.entities.Guild)3 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 XmlElement (net.robinfriedli.jxp.api.XmlElement)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)1