diff --git a/litecord/blueprints/guild/mod.py b/litecord/blueprints/guild/mod.py index 4a255e8..b80a98c 100644 --- a/litecord/blueprints/guild/mod.py +++ b/litecord/blueprints/guild/mod.py @@ -133,36 +133,29 @@ async def get_prune(guild_id: int, days: int) -> list: - did not login in ``days`` days. - don't have any roles. """ - # a good solution would be in pure sql. + + # @everyone role is always counted as a role in the db + # (its not implicit to every member as one would think) + # (that is probably the best solution in the future) + # (but we live with what we got) member_ids = await app.db.fetch( f""" SELECT id FROM users JOIN members ON members.guild_id = $1 AND members.user_id = users.id - WHERE users.last_session < (now() - (interval '{days} days')) + WHERE + users.last_session < (now() - (interval '{days} days')) + AND ( + SELECT COUNT(member_roles.role_id) + FROM member_roles + WHERE member_roles.user_id = members.user_id + ) <= 1 """, guild_id, ) - member_ids = [r["id"] for r in member_ids] - members = [] - - for member_id in member_ids: - role_count = await app.db.fetchval( - """ - SELECT COUNT(*) - FROM member_roles - WHERE guild_id = $1 AND user_id = $2 - """, - guild_id, - member_id, - ) - - if role_count == 1: # only the @everyone role - members.append(member_id) - - return members + return [r["id"] for r in member_ids] @bp.route("//prune", methods=["GET"])