diff --git a/aurora/aurora.py b/aurora/aurora.py index 3cac57d..6cbfff5 100644 --- a/aurora/aurora.py +++ b/aurora/aurora.py @@ -1490,8 +1490,6 @@ class Aurora(commands.Cog): async def handle_expiry(self): await self.bot.wait_until_red_ready() current_time = time.time() - database = await connect() - cursor = await database.cursor() global_unban_num = 0 global_addrole_num = 0 global_removerole_num = 0 @@ -1501,20 +1499,21 @@ class Aurora(commands.Cog): if not await self.bot.cog_disabled_in_guild(self, guild): time_per_guild = time.time() - tempban_query = f"SELECT target_id, moderation_id FROM moderation_{guild.id} WHERE end_timestamp IS NOT NULL 0 AND end_timestamp <= ? AND moderation_type = 'TEMPBAN' AND expired = 0" + tempban_query = f"SELECT * FROM moderation_{guild.id} WHERE end_timestamp IS NOT NULL 0 AND end_timestamp <= ? AND moderation_type = 'TEMPBAN' AND expired = 0" try: - await cursor.execute(tempban_query, (time.time(),)) - result = cursor.fetchall() + tempbans = await Moderation.execute(bot=self.bot, guild_id=guild.id, query=tempban_query, parameters=(time.time(),)) except aiosqlite.OperationalError: continue - target_ids = [row[0] for row in result] - moderation_ids = [row[1] for row in result] - unban_num = 0 - for target_id, moderation_id in zip(target_ids, moderation_ids): - user: discord.User = await self.bot.fetch_user(target_id) + for moderation in tempbans: + user = self.bot.get_user(moderation.target_id) + if user is None: + try: + user = self.bot.fetch_user(moderation.target_id) + except discord.errors.NotFound: + continue name = ( f"{user.name}#{user.discriminator}" if user.discriminator != "0" @@ -1522,14 +1521,14 @@ class Aurora(commands.Cog): ) try: await guild.unban( - user, reason=f"Automatic unban from case #{moderation_id}" + user, reason=f"Automatic unban from case #{moderation.id}" ) embed = await message_factory( bot=self.bot, color=await self.bot.get_embed_color(guild.channels[0]), guild=guild, - reason=f"Automatic unban from case #{moderation_id}", + reason=f"Automatic unban from case #{moderation.id}", moderation_type="unbanned", ) @@ -1538,14 +1537,14 @@ class Aurora(commands.Cog): except discord.errors.HTTPException: pass - logger.debug( + logger.trace( "Unbanned %s (%s) from %s (%s)", name, user.id, guild.name, guild.id, ) - unban_num = unban_num + 1 + unban_num += 1 except ( discord.errors.NotFound, discord.errors.Forbidden, @@ -1561,26 +1560,23 @@ class Aurora(commands.Cog): ) removerole_num = 0 - addrole_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp IS NOT NULL AND end_timestamp <= ? AND moderation_type = 'ADDROLE' AND expired = 0" - try: - await cursor.execute(addrole_query, (time.time(),)) - result = await cursor.fetchall() - except aiosqlite.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] + addrole_query = f"SELECT * FROM moderation_{guild.id} WHERE end_timestamp IS NOT NULL AND end_timestamp <= ? AND moderation_type = 'ADDROLE' AND expired = 0" + addroles = await Moderation.execute(bot=self.bot, guild_id=guild.id, query=addrole_query, parameters=(time.time(),)) - for target_id, moderation_id, role_id in zip( - target_ids, moderation_ids, role_ids - ): + for moderation in addroles: try: - member = await guild.fetch_member(target_id) + member = await guild.fetch_member(moderation.target_id) await member.remove_roles( - Object(role_id), reason=f"Automatic role removal from case #{moderation_id}" + Object(moderation.role_id), reason=f"Automatic role removal from case #{moderation.id}" ) + logger.trace( + "Removed role %s from %s (%s)", + moderation.role_id, + member.name, + member.id, + ) removerole_num = removerole_num + 1 except ( discord.errors.NotFound, @@ -1589,44 +1585,36 @@ class Aurora(commands.Cog): ) as e: logger.error( "Removing the role %s from user %s failed due to: \n%s", - role_id, - target_id, + moderation.role_id, + moderation.target_id, e, ) continue addrole_num = 0 removerole_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp IS NOT NULL 0 AND end_timestamp <= ? AND moderation_type = 'REMOVEROLE' AND expired = 0" - try: - await cursor.execute(removerole_query, (time.time(),)) - result = await cursor.fetchall() - except aiosqlite.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] + removeroles = await Moderation.execute(bot=self.bot, guild_id=guild.id, query=removerole_query, parameters=(time.time(),)) - for target_id, moderation_id, role_id in zip( - target_ids, moderation_ids, role_ids - ): + for moderation in removeroles: try: - member = await guild.fetch_member(target_id) + member = await guild.fetch_member(moderation.target_id) await member.add_roles( - Object(role_id), reason=f"Automatic role addition from case #{moderation_id}" + Object(moderation.role_id), reason=f"Automatic role addition from case #{moderation.id}" ) + logger.trace("Added role %s to %s (%s)", moderation.role_id, member.name, member.id) addrole_num = addrole_num + 1 except ( discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException, ) as e: - logger.error("Adding the role %s to user %s failed due to: \n%s", role_id, target_id, e) + logger.error("Adding the role %s to user %s failed due to: \n%s", moderation.role_id, moderation.target_id, e) continue expiry_query = f"UPDATE `moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp IS NOT NULL AND end_timestamp <= ? AND expired = 0) OR (expired = 0 AND resolved = 1);" - await cursor.execute(expiry_query, (time.time(),)) + await Moderation.execute(bot=self.bot, guild_id=guild.id, query=expiry_query, parameters=(time.time(),)) per_guild_completion_time = (time.time() - time_per_guild) * 1000 logger.debug( @@ -1642,9 +1630,6 @@ class Aurora(commands.Cog): global_addrole_num = global_addrole_num + addrole_num global_removerole_num = global_removerole_num + removerole_num - await database.commit() - await cursor.close() - await database.close() completion_time = (time.time() - current_time) * 1000 logger.debug(