feat(aurora): added expiry handling for addrole and removerole

This commit is contained in:
Seaswimmer 2024-05-03 20:09:48 -04:00
parent b8a4d247f8
commit f6b827c64f
Signed by untrusted user: cswimr
GPG key ID: 5D671B5D03D65A7F

View file

@ -1670,12 +1670,9 @@ class Aurora(commands.Cog):
e,
)
expiry_query = f"UPDATE `moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= ? AND expired = 0 AND moderation_type != 'BLACKLIST') OR (expired = 0 AND resolved = 1 AND moderation_type != 'BLACKLIST')"
cursor.execute(expiry_query, (time.time(),))
blacklist_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= ? AND moderation_type = 'BLACKLIST' AND expired = 0"
addrole_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= ? AND moderation_type = 'ADDROLE' AND expired = 0"
try:
cursor.execute(blacklist_query, (time.time(),))
cursor.execute(addrole_query, (time.time(),))
result = cursor.fetchall()
except sqlite3.OperationalError:
continue
@ -1687,11 +1684,15 @@ class Aurora(commands.Cog):
target_ids, moderation_ids, role_ids
):
try:
# member: discord.Member = await guild.fetch_member(target_id)
member = await guild.fetch_member(target_id)
role: discord.Role = guild.get_role(role_id)
role = guild.get_role(role_id)
if role is None:
raise discord.errors.NotFound
await member.remove_roles(
role, reason=f"Automatic role removal from case #{moderation_id}"
)
except (
discord.errors.NotFound,
discord.errors.Forbidden,
@ -1699,6 +1700,39 @@ class Aurora(commands.Cog):
):
continue
removerole_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= ? AND moderation_type = 'REMOVEROLE' AND expired = 0"
try:
cursor.execute(removerole_query, (time.time(),))
result = cursor.fetchall()
except sqlite3.OperationalError:
continue
target_ids = [row[0] for row in result]
moderation_ids = [row[1] for row in result]
role_ids = [row[2] for row in result]
for target_id, moderation_id, role_id in zip(
target_ids, moderation_ids, role_ids
):
try:
member = await guild.fetch_member(target_id)
role = guild.get_role(role_id)
if role is None:
raise discord.errors.NotFound
await member.add_roles(
role, reason=f"Automatic role addition from case #{moderation_id}"
)
except (
discord.errors.NotFound,
discord.errors.Forbidden,
discord.errors.HTTPException,
):
continue
expiry_query = f"UPDATE `moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= ? AND expired = 0) OR (expired = 0 AND resolved = 1"
cursor.execute(expiry_query, (time.time(),))
per_guild_completion_time = (time.time() - time_per_guild) * 1000
logger.debug(
"Completed expiry loop for %s (%s) in %sms with %s users unbanned",