Skip to content

Commit 3a37a34

Browse files
committed
refactor blocked command to use new block system
1 parent e1de369 commit 3a37a34

File tree

1 file changed

+23
-47
lines changed

1 file changed

+23
-47
lines changed

cogs/modmail.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import datetime
32
import re
43
from datetime import timezone
54
from itertools import zip_longest
@@ -12,7 +11,7 @@
1211
from discord.ext.commands.cooldowns import BucketType
1312
from discord.ext.commands.view import StringView
1413

15-
from core import checks
14+
from core import blocklist, checks
1615
from core.blocklist import BlockType
1716
from core.models import DMDisabled, PermissionLevel, SimilarCategoryConverter, getLogger
1817
from core.paginator import EmbedPaginatorSession
@@ -1646,57 +1645,34 @@ async def contact(
16461645
async def blocked(self, ctx):
16471646
"""Retrieve a list of blocked users."""
16481647

1649-
roles, users, now = [], [], discord.utils.utcnow()
1648+
roles, users = [], []
16501649

1651-
blocked_users = list(self.bot.blocked_users.items())
1652-
for id_, data in blocked_users:
1653-
blocked_by_id = data["blocked_by"]
1654-
blocked_at = parser.parse(data["blocked_at"])
1655-
human_blocked_at = discord.utils.format_dt(blocked_at, style="R")
1656-
if "until" in data:
1657-
blocked_until = parser.parse(data["until"])
1658-
human_blocked_until = discord.utils.format_dt(blocked_until, style="R")
1659-
else:
1660-
blocked_until = human_blocked_until = "Permanent"
1661-
1662-
if isinstance(blocked_until, datetime.datetime) and blocked_until < now:
1663-
self.bot.blocked_users.pop(str(id_))
1664-
logger.debug("No longer blocked, user %s.", id_)
1665-
continue
1666-
1667-
string = f"<@{id_}> ({human_blocked_until})"
1668-
string += f"\n- Issued {human_blocked_at} by <@{blocked_by_id}>"
1650+
blocked: list[blocklist.BlocklistItem] = await self.bot.blocklist.get_all_blocks()
16691651

1670-
reason = data.get("reason")
1671-
if reason:
1672-
string += f"\n- Blocked for {reason}"
1673-
1674-
users.append(string + "\n")
1652+
for item in blocked:
1653+
human_blocked_at = discord.utils.format_dt(item.timestamp, style="R")
1654+
if item.expires_at is not None:
1655+
human_blocked_until = discord.utils.format_dt(item.expires_at, style="R")
1656+
else:
1657+
human_blocked_until = "Permanent"
16751658

1676-
blocked_roles = list(self.bot.blocked_roles.items())
1677-
for id_, data in blocked_roles:
1678-
blocked_by_id = data["blocked_by"]
1679-
blocked_at = parser.parse(data["blocked_at"])
1680-
human_blocked_at = discord.utils.format_dt(blocked_at, style="R")
1681-
if "until" in data:
1682-
blocked_until = parser.parse(data["until"])
1683-
human_blocked_until = discord.utils.format_dt(blocked_until, style="R")
1659+
if item.type == blocklist.BlockType.USER:
1660+
string = f"<@{item.id}>"
16841661
else:
1685-
blocked_until = human_blocked_until = "Permanent"
1662+
string = f"<@&{item.id}>"
16861663

1687-
if isinstance(blocked_until, datetime.datetime) and blocked_until < now:
1688-
self.bot.blocked_users.pop(str(id_))
1689-
logger.debug("No longer blocked, user %s.", id_)
1690-
continue
1664+
string += f" ({human_blocked_until})"
16911665

1692-
string = f"<@&{id_}> ({human_blocked_until})"
1693-
string += f"\n- Issued {human_blocked_at} by <@{blocked_by_id}>"
1666+
string += f"\n- Issued {human_blocked_at} by <@{item.blocking_user_id}>"
16941667

1695-
reason = data.get("reason")
1696-
if reason:
1697-
string += f"\n- Blocked for {reason}"
1668+
if item.reason is not None:
1669+
string += f"\n- Blocked for {item.reason}"
1670+
string += "\n"
16981671

1699-
roles.append(string + "\n")
1672+
if item.type == blocklist.BlockType.USER:
1673+
users.append(string)
1674+
elif item.type == blocklist.BlockType.ROLE:
1675+
roles.append(string)
17001676

17011677
user_embeds = [discord.Embed(title="Blocked Users", color=self.bot.main_color, description="")]
17021678

@@ -1714,7 +1690,7 @@ async def blocked(self, ctx):
17141690
else:
17151691
embed.description += line
17161692
else:
1717-
user_embeds[0].description = "Currently there are no blocked users."
1693+
user_embeds[0].description = "No users are currently blocked."
17181694

17191695
if len(user_embeds) > 1:
17201696
for n, em in enumerate(user_embeds):
@@ -1737,7 +1713,7 @@ async def blocked(self, ctx):
17371713
else:
17381714
embed.description += line
17391715
else:
1740-
role_embeds[-1].description = "Currently there are no blocked roles."
1716+
role_embeds[-1].description = "No roles are currently blocked."
17411717

17421718
if len(role_embeds) > 1:
17431719
for n, em in enumerate(role_embeds):

0 commit comments

Comments
 (0)