Search in sources :

Example 1 with AbstractGuildProperty

use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty 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 2 with AbstractGuildProperty

use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.

the class GuildPropertyInterceptor method beforeTransactionCompletionChained.

@Override
public void beforeTransactionCompletionChained(Transaction tx) {
    if (!tx.getRollbackOnly()) {
        for (AbstractGuildProperty property : changedProperties.keySet()) {
            if ("argumentPrefix".equals(property.getProperty())) {
                // loop of triggering itself
                try (Session session = sessionFactory.openSession()) {
                    HibernateInvoker.create(session).invoke(() -> {
                        Pair<Object, Object> previousWithNewValue = changedProperties.get(property);
                        // previous might be null
                        updatePresets(property, (Character) previousWithNewValue.getLeft(), (char) previousWithNewValue.getRight(), session);
                    });
                } catch (Exception e) {
                    messageService.sendException("Exception occurred while updating presets with new argument prefix. " + "Presets will have to be updated manually if necessary.", commandContext.getChannel());
                    LoggerFactory.getLogger(getClass()).error("Exception while updating presets", e);
                }
            }
        }
    }
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) Session(org.hibernate.Session)

Example 3 with AbstractGuildProperty

use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.

the class GuildPropertyInterceptor method afterTransactionCompletionChained.

@Override
public void afterTransactionCompletionChained(Transaction tx) {
    if (!tx.getRollbackOnly()) {
        if (!changedProperties.isEmpty()) {
            StringBuilder successMessageBuilder = new StringBuilder();
            for (AbstractGuildProperty property : changedProperties.keySet()) {
                Pair<Object, Object> previousWithNewValue = changedProperties.get(property);
                String updateMessage = property.getContribution().getUpdateMessage(previousWithNewValue.getRight());
                successMessageBuilder.append(updateMessage).append(System.lineSeparator());
            }
            messageService.sendSuccess(successMessageBuilder.toString(), commandContext.getChannel());
        }
    }
    changedProperties.clear();
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty)

Example 4 with AbstractGuildProperty

use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.

the class GuildManager method getDefaultTextChannelForGuild.

private TextChannel getDefaultTextChannelForGuild(Guild guild, GuildContext guildContext) {
    Aiode aiode = Aiode.get();
    Member selfMember = guild.getSelfMember();
    // fetch the default text channel from the customised property
    GuildPropertyManager guildPropertyManager = aiode.getGuildPropertyManager();
    AbstractGuildProperty defaultTextChannelProperty = guildPropertyManager.getProperty("defaultTextChannelId");
    if (defaultTextChannelProperty != null) {
        String defaultTextChannelId;
        try {
            defaultTextChannelId = (String) hibernateComponent.invokeWithSession(session -> defaultTextChannelProperty.get(guildContext.getSpecification(session)));
        } catch (NullPointerException e) {
            throw new RuntimeException(e);
        }
        if (!Strings.isNullOrEmpty(defaultTextChannelId)) {
            TextChannel textChannelById = guild.getTextChannelById(defaultTextChannelId);
            if (textChannelById != null && selfMember.hasAccess(textChannelById) && textChannelById.canTalk(selfMember)) {
                return textChannelById;
            }
        }
    }
    // check if the guild's playback has a current communication text channel
    MessageChannel playbackCommunicationChannel = guildContext.getPlayback().getCommunicationChannel();
    if (playbackCommunicationChannel instanceof TextChannel) {
        TextChannel textChannelFromPlayback = (TextChannel) playbackCommunicationChannel;
        if (selfMember.hasAccess(textChannelFromPlayback) && textChannelFromPlayback.canTalk(selfMember)) {
            return textChannelFromPlayback;
        }
    }
    // use guild default defined by discord
    TextChannel defaultChannel = guild.getDefaultChannel();
    if (defaultChannel != null && selfMember.hasAccess(defaultChannel) && defaultChannel.canTalk(selfMember)) {
        return defaultChannel;
    } else {
        TextChannel systemChannel = guild.getSystemChannel();
        if (systemChannel != null && selfMember.hasAccess(systemChannel) && systemChannel.canTalk()) {
            return systemChannel;
        }
    }
    List<TextChannel> availableChannels = guild.getTextChannels().stream().filter(channel -> selfMember.hasAccess(channel) && channel.canTalk(selfMember)).collect(Collectors.toList());
    if (availableChannels.isEmpty()) {
        return null;
    } else {
        return availableChannels.get(0);
    }
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) PlaylistItemTimestampInterceptor(net.robinfriedli.aiode.persist.interceptors.PlaylistItemTimestampInterceptor) PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) JxpBackend(net.robinfriedli.jxp.api.JxpBackend) LoggerFactory(org.slf4j.LoggerFactory) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Guild(net.dv8tion.jda.api.entities.Guild) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) Playlist(net.robinfriedli.aiode.entities.Playlist) Map(java.util.Map) Resource(org.springframework.core.io.Resource) EmbedDocumentContribution(net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) AudioManager(net.robinfriedli.aiode.audio.AudioManager) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Sets(com.google.common.collect.Sets) List(java.util.List) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) SnowflakeMap(net.robinfriedli.aiode.util.SnowflakeMap) Optional(java.util.Optional) InterceptorChain(net.robinfriedli.aiode.persist.interceptors.InterceptorChain) HibernateComponent(net.robinfriedli.aiode.boot.configurations.HibernateComponent) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) LocalDateTime(java.time.LocalDateTime) Session(org.hibernate.Session) MutexSync(net.robinfriedli.exec.MutexSync) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) Nullable(javax.annotation.Nullable) VerifyPlaylistInterceptor(net.robinfriedli.aiode.persist.interceptors.VerifyPlaylistInterceptor) Context(net.robinfriedli.jxp.persist.Context) Logger(org.slf4j.Logger) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) CommandHistory(net.robinfriedli.aiode.entities.CommandHistory) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Conditions(net.robinfriedli.jxp.queries.Conditions) HibernateInvoker(net.robinfriedli.aiode.function.HibernateInvoker) Aiode(net.robinfriedli.aiode.Aiode) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) Component(org.springframework.stereotype.Component) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager) Aiode(net.robinfriedli.aiode.Aiode) Member(net.dv8tion.jda.api.entities.Member)

Example 5 with AbstractGuildProperty

use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.

the class PropertyCommand method listProperties.

private void listProperties() {
    GuildPropertyManager guildPropertyManager = Aiode.get().getGuildPropertyManager();
    List<AbstractGuildProperty> properties = guildPropertyManager.getProperties();
    GuildSpecification specification = getContext().getGuildContext().getSpecification();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    EmbedTable table = new EmbedTable(embedBuilder);
    table.addColumn("Name", properties, AbstractGuildProperty::getName);
    table.addColumn("Default Value", properties, AbstractGuildProperty::getDefaultValue);
    table.addColumn("Set Value", properties, property -> property.display(specification));
    table.build();
    sendMessage(embedBuilder);
}
Also used : AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager) EmbedTable(net.robinfriedli.aiode.util.EmbedTable)

Aggregations

AbstractGuildProperty (net.robinfriedli.aiode.discord.property.AbstractGuildProperty)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 GuildPropertyManager (net.robinfriedli.aiode.discord.property.GuildPropertyManager)3 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)3 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 Session (org.hibernate.Session)2 Strings (com.google.common.base.Strings)1 Sets (com.google.common.collect.Sets)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 Guild (net.dv8tion.jda.api.entities.Guild)1