Search in sources :

Example 1 with DiscordEntityInitialisationException

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

the class DiscordEntity method retrieve.

@Nullable
public T retrieve() {
    T cached = getCached();
    if (cached != null) {
        return cached;
    }
    if (!canFetch()) {
        return null;
    }
    RestAction<? extends T> fetch;
    try {
        fetch = fetch();
    } catch (InsufficientPermissionException e) {
        throw e;
    } catch (Exception e) {
        throw new DiscordEntityInitialisationException(e);
    }
    if (fetch == null) {
        return null;
    }
    try {
        return fetch.complete();
    } catch (ErrorResponseException e) {
        LOGGER.warn("Received error response trying to fetch discord entity", e);
        return null;
    } catch (InsufficientPermissionException e) {
        throw e;
    } catch (Exception e) {
        throw new DiscordEntityInitialisationException(e);
    }
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) DiscordEntityInitialisationException(net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) DiscordEntityInitialisationException(net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with DiscordEntityInitialisationException

use of net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException 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

DiscordEntityInitialisationException (net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException)2 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 Guild (net.dv8tion.jda.api.entities.Guild)1 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)1 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)1 Aiode (net.robinfriedli.aiode.Aiode)1 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)1 GuildContext (net.robinfriedli.aiode.discord.GuildContext)1 GuildManager (net.robinfriedli.aiode.discord.GuildManager)1 Nullable (org.jetbrains.annotations.Nullable)1 Logger (org.slf4j.Logger)1