diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/AutoPruneHelperRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/AutoPruneHelperRoutine.java index b04e01776a..6ed381a36e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/AutoPruneHelperRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/AutoPruneHelperRoutine.java @@ -84,23 +84,36 @@ public void runRoutine(JDA jda) { } private void pruneForGuild(Guild guild) { - Instant now = Instant.now(); TextChannel selectRoleChannel = getSelectRolesChannelOptional(guild.getJDA()).orElse(null); + guild.loadMembers() + .onSuccess(members -> pruneCategories(guild, members, selectRoleChannel)) + .onError(throwable -> logger.error("Failed to request all members for auto prune.", + throwable)); + } + + private void pruneCategories(Guild guild, List members, + @Nullable TextChannel selectRoleChannel) { + Instant now = Instant.now(); allCategories.stream() .map(category -> helper.handleFindRoleForCategory(category, guild)) .filter(Optional::isPresent) .map(Optional::orElseThrow) - .forEach(role -> pruneRoleIfFull(role, selectRoleChannel, now)); + .forEach(role -> pruneRoleIfFull(members, role, selectRoleChannel, now)); } - private void pruneRoleIfFull(Role role, @Nullable TextChannel selectRoleChannel, Instant when) { - role.getGuild().findMembersWithRoles(role).onSuccess(members -> { - if (isRoleFull(members)) { - logger.debug("Helper role {} is full, starting to prune.", role.getName()); - pruneRole(role, members, selectRoleChannel, when); - } - }); + private void pruneRoleIfFull(List members, Role targetRole, + @Nullable TextChannel selectRoleChannel, Instant when) { + List withRole = filterForRole(members, targetRole); + + if (isRoleFull(withRole)) { + logger.debug("Helper role {} is full, starting to prune.", targetRole.getName()); + pruneRole(targetRole, withRole, selectRoleChannel, when); + } + } + + private List filterForRole(List members, Role role) { + return members.stream().filter(member -> member.getRoles().contains(role)).toList(); } private boolean isRoleFull(Collection members) {