Search in sources :

Example 1 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class GuildManagementListener method onRoleDelete.

@Override
public void onRoleDelete(@Nonnull RoleDeleteEvent event) {
    EventHandlerPool.execute(() -> {
        Role role = event.getRole();
        String roleId = role.getId();
        hibernateComponent.consumeSession(session -> {
            CriteriaBuilder cb = session.getCriteriaBuilder();
            CriteriaDelete<GrantedRole> deleteQuery = cb.createCriteriaDelete(GrantedRole.class);
            Root<GrantedRole> queryRoot = deleteQuery.from(GrantedRole.class);
            deleteQuery.where(cb.equal(queryRoot.get("id"), roleId));
            int rowCount = session.createQuery(deleteQuery).executeUpdate();
            if (rowCount > 0) {
                Guild guild = event.getGuild();
                GuildContext guildContext = guildManager.getContextForGuild(guild);
                logger.info(String.format("Deleted %s GrantedRole entities upon deletion of role %s on guild %s", rowCount, roleId, guild));
                EmbedBuilder embedBuilder = new EmbedBuilder();
                embedBuilder.setTitle("Deletion of role referenced by your permission configuration");
                embedBuilder.setDescription(String.format("The deleted role '%s' was referenced by the permission configuration. Check the current permissions by using the permission command.", role.getName()));
                embedBuilder.setColor(ColorSchemeProperty.getColor(guildContext.getSpecification(session)));
                TextChannel textChannel = guildManager.getDefaultTextChannelForGuild(guild);
                if (textChannel != null) {
                    messageService.sendTemporary(embedBuilder.build(), textChannel);
                }
            }
        });
    });
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GuildContext(net.robinfriedli.aiode.discord.GuildContext) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) Guild(net.dv8tion.jda.api.entities.Guild)

Example 2 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class PermissionCommand method listPermissions.

private void listPermissions() {
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    GuildContext guildContext = context.getGuildContext();
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    List<PermissionTarget> targets = Lists.newArrayList();
    for (PermissionTarget.TargetType targetType : PermissionTarget.TargetType.values()) {
        if (!targetType.isChildTargetOnly()) {
            Set<? extends PermissionTarget> permissionTargetsInCategory = targetType.getAllPermissionTargetsInCategory();
            permissionTargetsInCategory.stream().sorted(Comparator.comparingInt((ToIntFunction<PermissionTarget>) permissionTarget -> permissionTarget.getPermissionTypeCategory().getSorting()).thenComparing(PermissionTarget::getPermissionTargetIdentifier)).forEach(permissionTarget -> {
                List<PermissionTarget> flattenedPermissionHierarchy = Lists.newArrayList();
                LinkedList<List<PermissionTarget>> stack = new LinkedList<>();
                stack.push(flattenedPermissionHierarchy);
                flattenPermissionTargetHierarchy(permissionTarget, stack, securityManager, guild, true);
                targets.addAll(flattenedPermissionHierarchy);
            });
        }
    }
    PermissionListPaginationWidget permissionListPaginationWidget = new PermissionListPaginationWidget(guildContext.getWidgetRegistry(), guild, context.getChannel(), targets, securityManager);
    permissionListPaginationWidget.initialise();
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) Guild(net.dv8tion.jda.api.entities.Guild) LinkedList(java.util.LinkedList) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PermissionListPaginationWidget(net.robinfriedli.aiode.command.widget.widgets.PermissionListPaginationWidget) ToIntFunction(java.util.function.ToIntFunction) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class AbortCommand method doRun.

@Override
public void doRun() {
    CommandExecutionTask commandExecutionTask = getTask();
    Thread abortThread = new Thread(() -> {
        if (commandExecutionTask != null) {
            try {
                commandExecutionTask.await();
            } catch (InterruptedException ignored) {
            }
        }
        CommandExecutionQueueManager executionQueueManager = Aiode.get().getExecutionQueueManager();
        GuildContext guildContext = getContext().getGuildContext();
        ThreadExecutionQueue executionQueue = executionQueueManager.getForGuild(getContext().getGuild());
        PooledTrackLoadingExecutor pooledTrackLoadingExecutor = guildContext.getPooledTrackLoadingExecutor();
        ReplaceableTrackLoadingExecutor replaceableTrackLoadingExecutor = guildContext.getReplaceableTrackLoadingExecutor();
        if (executionQueue.isIdle() && pooledTrackLoadingExecutor.isIdle() && replaceableTrackLoadingExecutor.isIdle()) {
            EmbedBuilder embedBuilder = new EmbedBuilder();
            embedBuilder.setDescription("No commands are currently running");
            getMessageService().sendTemporary(embedBuilder, getContext().getChannel());
            setFailed(true);
        } else {
            executionQueue.abortAll();
            pooledTrackLoadingExecutor.abortAll();
            replaceableTrackLoadingExecutor.abort();
            sendSuccess("Sent all currently running commands an interrupt signal and cancelled queued commands.");
        }
    });
    abortThread.setName("aiode abort thread");
    abortThread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
    abortThread.start();
}
Also used : CommandExecutionTask(net.robinfriedli.aiode.concurrent.CommandExecutionTask) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PooledTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.PooledTrackLoadingExecutor) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ReplaceableTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.ReplaceableTrackLoadingExecutor) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Example 4 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class ClearAbandonedGuildContextsTask method run.

@Override
protected void run(JobExecutionContext jobExecutionContext) {
    Aiode aiode = Aiode.get();
    ShardManager shardManager = aiode.getShardManager();
    GuildManager guildManager = aiode.getGuildManager();
    CommandExecutionQueueManager executionQueueManager = aiode.getExecutionQueueManager();
    int removedGuilds = 0;
    for (GuildContext guildContext : guildManager.getGuildContexts()) {
        Guild guild = guildContext.retrieveGuild();
        if (guild == null || shardManager.getGuildById(guild.getIdLong()) == null) {
            guildManager.removeGuild(guild);
            executionQueueManager.removeGuild(guild);
            ++removedGuilds;
        }
    }
    if (removedGuilds > 0) {
        LoggerFactory.getLogger(getClass()).info("Destroyed context for " + removedGuilds + " missing guilds");
    }
}
Also used : GuildContext(net.robinfriedli.aiode.discord.GuildContext) GuildManager(net.robinfriedli.aiode.discord.GuildManager) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Example 5 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class PlaybackCleanupTask method run.

@Override
public void run(JobExecutionContext jobExecutionContext) {
    Logger logger = LoggerFactory.getLogger(getClass());
    Aiode aiode = Aiode.get();
    GuildManager guildManager = aiode.getGuildManager();
    Set<GuildContext> guildContexts = guildManager.getGuildContexts();
    Set<Guild> activeGuilds = StaticSessionProvider.invokeWithSession(session -> guildManager.getActiveGuilds(session, 3600000));
    int clearedAlone = 0;
    int playbacksCleared = 0;
    for (GuildContext guildContext : guildContexts) {
        try {
            AudioPlayback playback = guildContext.getPlayback();
            LocalDateTime aloneSince = playback.getAloneSince();
            if (aloneSince != null) {
                Duration aloneSinceDuration = Duration.between(aloneSince, LocalDateTime.now());
                Duration oneHour = Duration.ofHours(1);
                if (aloneSinceDuration.compareTo(oneHour) > 0) {
                    if (clearLonePlayback(guildContext, playback)) {
                        ++clearedAlone;
                        continue;
                    }
                }
            }
            if (!activeGuilds.contains(playback.getGuild()) && !playback.isPlaying()) {
                if (playback.clear()) {
                    ++playbacksCleared;
                }
            }
        } catch (DiscordEntityInitialisationException e) {
            // guild could not be loaded anymore, skip
            continue;
        }
    }
    if (clearedAlone > 0 || playbacksCleared > 0) {
        logger.info(String.format("Cleared %d stale playbacks and stopped %d lone playbacks", playbacksCleared, clearedAlone));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) GuildContext(net.robinfriedli.aiode.discord.GuildContext) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) GuildManager(net.robinfriedli.aiode.discord.GuildManager) Duration(java.time.Duration) Logger(org.slf4j.Logger) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) DiscordEntityInitialisationException(net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException)

Aggregations

GuildContext (net.robinfriedli.aiode.discord.GuildContext)7 Guild (net.dv8tion.jda.api.entities.Guild)6 TextChannel (net.dv8tion.jda.api.entities.TextChannel)3 Aiode (net.robinfriedli.aiode.Aiode)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandExecutionQueueManager (net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)2 GuildManager (net.robinfriedli.aiode.discord.GuildManager)2 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ToIntFunction (java.util.function.ToIntFunction)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 JDA (net.dv8tion.jda.api.JDA)1 Permission (net.dv8tion.jda.api.Permission)1 Member (net.dv8tion.jda.api.entities.Member)1 Message (net.dv8tion.jda.api.entities.Message)1 Role (net.dv8tion.jda.api.entities.Role)1 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)1