Search in sources :

Example 1 with GuildSpecification

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

the class AccessConfigurationPartitionInterceptor method intercept.

@Override
public void intercept(QueryBuilder<?, ?, ?, ?> queryBuilder) {
    Optional<ExecutionContext> executionContext = ExecutionContext.Current.optional();
    if (executionContext.isPresent() || (session != null && guildId != null)) {
        Session session = executionContext.map(ExecutionContext::getSession).orElse(this.session);
        String id = executionContext.map(ctx -> ctx.getGuild().getId()).orElse(guildId);
        queryBuilder.where((cb, root, subQueryFactory) -> cb.equal(root.get("guildSpecification"), subQueryFactory.createUncorrelatedSubQuery(GuildSpecification.class, "pk").where((cb1, root1, subQueryFactory1) -> cb1.equal(root1.get("guildId"), id)).build(session)));
    }
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Component(org.springframework.stereotype.Component) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) QueryInterceptor(net.robinfriedli.aiode.persist.qb.interceptor.QueryInterceptor) Optional(java.util.Optional) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryBuilder(net.robinfriedli.aiode.persist.qb.QueryBuilder) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Session(org.hibernate.Session)

Example 2 with GuildSpecification

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

the class GuildPropertyInterceptor method onFlushDirty.

// use onFlushDirty instead of onFlushDirtyChained as exceptions should get thrown
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
    if (entity instanceof GuildSpecification) {
        for (int i = 0; i < currentState.length; i++) {
            String propertyName = propertyNames[i];
            Object current = currentState[i];
            Object previous = previousState[i];
            if (current != null && !current.equals(previous)) {
                AbstractGuildProperty property = guildPropertyManager.getProperty(propertyName);
                if (property != null) {
                    changedProperties.put(property, Pair.of(previous, current));
                    property.validate(current);
                }
            }
        }
    }
    return next().onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification)

Example 3 with GuildSpecification

use of net.robinfriedli.aiode.entities.GuildSpecification 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 4 with GuildSpecification

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

the class GuildContext method getBotName.

public String getBotName() {
    return StaticSessionProvider.invokeWithSession(session -> {
        GuildPropertyManager guildPropertyManager = Aiode.get().getGuildPropertyManager();
        GuildSpecification specification = getSpecification(session);
        return guildPropertyManager.getPropertyValueOptional("botName", String.class, specification).orElse(BotNameProperty.DEFAULT_FALLBACK);
    });
}
Also used : GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager)

Example 5 with GuildSpecification

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

the class GuildContext method getPrefix.

public String getPrefix() {
    return StaticSessionProvider.invokeWithSession(session -> {
        GuildPropertyManager guildPropertyManager = Aiode.get().getGuildPropertyManager();
        GuildSpecification specification = getSpecification(session);
        return guildPropertyManager.getPropertyValueOptional("prefix", String.class, specification).orElse(PrefixProperty.DEFAULT_FALLBACK);
    });
}
Also used : GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager)

Aggregations

GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)24 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)9 GuildPropertyManager (net.robinfriedli.aiode.discord.property.GuildPropertyManager)7 Session (org.hibernate.Session)7 Guild (net.dv8tion.jda.api.entities.Guild)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)5 Role (net.dv8tion.jda.api.entities.Role)4 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)4 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)4 Aiode (net.robinfriedli.aiode.Aiode)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)3 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)3 AbstractGuildProperty (net.robinfriedli.aiode.discord.property.AbstractGuildProperty)3 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)3 Color (java.awt.Color)2 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)2 SpringPropertiesConfig (net.robinfriedli.aiode.boot.SpringPropertiesConfig)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)2 ArgumentController (net.robinfriedli.aiode.command.argument.ArgumentController)2 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2