From d91e81334e885d6002a4e1da1b99dabc49a69f50 Mon Sep 17 00:00:00 2001 From: Khakers <22665282+khakers@users.noreply.github.com> Date: Mon, 17 Jul 2023 13:30:50 -0700 Subject: [PATCH] refactor blocked command to use new block system --- cogs/modmail.py | 67 ++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 45 deletions(-) diff --git a/cogs/modmail.py b/cogs/modmail.py index dc87f37577a..8447bf5219c 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -12,7 +12,7 @@ from discord.ext.commands.cooldowns import BucketType from discord.ext.commands.view import StringView -from core import checks +from core import blocklist, checks from core.blocklist import BlockType from core.models import DMDisabled, PermissionLevel, SimilarCategoryConverter, getLogger from core.paginator import EmbedPaginatorSession @@ -1648,55 +1648,32 @@ async def blocked(self, ctx): roles, users, now = [], [], discord.utils.utcnow() - blocked_users = list(self.bot.blocked_users.items()) - for id_, data in blocked_users: - blocked_by_id = data["blocked_by"] - blocked_at = parser.parse(data["blocked_at"]) - human_blocked_at = discord.utils.format_dt(blocked_at, style="R") - if "until" in data: - blocked_until = parser.parse(data["until"]) - human_blocked_until = discord.utils.format_dt(blocked_until, style="R") - else: - blocked_until = human_blocked_until = "Permanent" - - if isinstance(blocked_until, datetime.datetime) and blocked_until < now: - self.bot.blocked_users.pop(str(id_)) - logger.debug("No longer blocked, user %s.", id_) - continue - - string = f"<@{id_}> ({human_blocked_until})" - string += f"\n- Issued {human_blocked_at} by <@{blocked_by_id}>" + blocked: list[blocklist.BlocklistItem] = await self.bot.blocklist.get_all_blocks() - reason = data.get("reason") - if reason: - string += f"\n- Blocked for {reason}" - - users.append(string + "\n") + for item in blocked: + human_blocked_at = discord.utils.format_dt(item.timestamp, style="R") + if item.expires_at is not None: + human_blocked_until = discord.utils.format_dt(item.expires_at, style="R") + else: + human_blocked_until = "Permanent" - blocked_roles = list(self.bot.blocked_roles.items()) - for id_, data in blocked_roles: - blocked_by_id = data["blocked_by"] - blocked_at = parser.parse(data["blocked_at"]) - human_blocked_at = discord.utils.format_dt(blocked_at, style="R") - if "until" in data: - blocked_until = parser.parse(data["until"]) - human_blocked_until = discord.utils.format_dt(blocked_until, style="R") + if item.type == blocklist.BlockType.USER: + string = f"<@{item.id}>" else: - blocked_until = human_blocked_until = "Permanent" + string = f"<@&{item.id}>" - if isinstance(blocked_until, datetime.datetime) and blocked_until < now: - self.bot.blocked_users.pop(str(id_)) - logger.debug("No longer blocked, user %s.", id_) - continue + string += f" ({human_blocked_until})" - string = f"<@&{id_}> ({human_blocked_until})" - string += f"\n- Issued {human_blocked_at} by <@{blocked_by_id}>" + string += f"\n- Issued {human_blocked_at} by <@{item.blocking_user_id}>" - reason = data.get("reason") - if reason: - string += f"\n- Blocked for {reason}" + if item.reason is not None: + string += f"\n- Blocked for {item.reason}" + string += "\n" - roles.append(string + "\n") + if item.type == blocklist.BlockType.USER: + users.append(string) + elif item.type == blocklist.BlockType.ROLE: + roles.append(string) user_embeds = [discord.Embed(title="Blocked Users", color=self.bot.main_color, description="")] @@ -1714,7 +1691,7 @@ async def blocked(self, ctx): else: embed.description += line else: - user_embeds[0].description = "Currently there are no blocked users." + user_embeds[0].description = "No users are currently blocked." if len(user_embeds) > 1: for n, em in enumerate(user_embeds): @@ -1737,7 +1714,7 @@ async def blocked(self, ctx): else: embed.description += line else: - role_embeds[-1].description = "Currently there are no blocked roles." + role_embeds[-1].description = "No roles are currently blocked." if len(role_embeds) > 1: for n, em in enumerate(role_embeds):