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