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);
}
}
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));
}
}
Aggregations