Search in sources :

Example 1 with InvalidPropertyValueException

use of net.robinfriedli.aiode.exceptions.InvalidPropertyValueException in project aiode by robinfriedli.

the class SanitizingEntityInterceptor method onSave.

@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (entity instanceof SanitizedEntity) {
        SanitizedEntity sanitizedEntity = (SanitizedEntity) entity;
        Set<SanitizedEntity.IdentifierFormattingRule> identifierFormattingRules = sanitizedEntity.getIdentifierFormattingRules();
        for (SanitizedEntity.IdentifierFormattingRule identifierFormattingRule : identifierFormattingRules) {
            String identifier = sanitizedEntity.getIdentifier();
            if (!identifierFormattingRule.getPredicate().test(identifier)) {
                String errorMessage = String.format(identifierFormattingRule.getErrorMessage(), identifier);
                throw new InvalidPropertyValueException(errorMessage);
            }
        }
    }
    return super.onSave(entity, id, state, propertyNames, types);
}
Also used : SanitizedEntity(net.robinfriedli.aiode.entities.SanitizedEntity) InvalidPropertyValueException(net.robinfriedli.aiode.exceptions.InvalidPropertyValueException)

Example 2 with InvalidPropertyValueException

use of net.robinfriedli.aiode.exceptions.InvalidPropertyValueException in project aiode by robinfriedli.

the class DefaultTextChannelProperty method setValue.

@Override
public void setValue(String value, GuildSpecification guildSpecification) {
    ShardManager shardManager = Aiode.get().getShardManager();
    Guild guild = guildSpecification.getGuild(shardManager);
    TextChannel textChannelById = null;
    try {
        textChannelById = guild.getTextChannelById(value);
    } catch (NumberFormatException ignored) {
    }
    if (textChannelById != null) {
        guildSpecification.setDefaultTextChannelId(value);
    } else {
        List<TextChannel> textChannelsByName = guild.getTextChannelsByName(value, true);
        if (textChannelsByName.size() == 1) {
            guildSpecification.setDefaultTextChannelId(textChannelsByName.get(0).getId());
        } else if (textChannelsByName.size() > 1) {
            throw new AmbiguousCommandException(textChannelsByName, c -> {
                TextChannel channel = (TextChannel) c;
                return channel.getName() + " (" + channel.getId() + ")";
            });
        } else {
            throw new InvalidPropertyValueException(String.format("No text channel found for '%s'. Either copy the id of the channel or its name.", value));
        }
    }
}
Also used : AmbiguousCommandException(net.robinfriedli.aiode.exceptions.AmbiguousCommandException) Aiode(net.robinfriedli.aiode.Aiode) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildPropertyContribution(net.robinfriedli.aiode.entities.xml.GuildPropertyContribution) InvalidPropertyValueException(net.robinfriedli.aiode.exceptions.InvalidPropertyValueException) AmbiguousCommandException(net.robinfriedli.aiode.exceptions.AmbiguousCommandException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) InvalidPropertyValueException(net.robinfriedli.aiode.exceptions.InvalidPropertyValueException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild)

Aggregations

InvalidPropertyValueException (net.robinfriedli.aiode.exceptions.InvalidPropertyValueException)2 List (java.util.List)1 Guild (net.dv8tion.jda.api.entities.Guild)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)1 Aiode (net.robinfriedli.aiode.Aiode)1 AbstractGuildProperty (net.robinfriedli.aiode.discord.property.AbstractGuildProperty)1 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)1 SanitizedEntity (net.robinfriedli.aiode.entities.SanitizedEntity)1 GuildPropertyContribution (net.robinfriedli.aiode.entities.xml.GuildPropertyContribution)1 AmbiguousCommandException (net.robinfriedli.aiode.exceptions.AmbiguousCommandException)1