Skip to content

Commit a5b4c66

Browse files
authored
Merge branch 'main' into main
2 parents 39a3653 + a3a7d4d commit a5b4c66

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

bot/exts/moderation/watchchannels/_watchchannel.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(
6262
self.log = logger # Logger of the child cog for a correct name in the logs
6363

6464
self._consume_task = None
65-
self.watched_users = defaultdict(dict)
65+
self.watched_users = {}
6666
self.message_queue = defaultdict(lambda: defaultdict(deque))
6767
self.consumption_queue = {}
6868
self.retries = 5
@@ -154,7 +154,7 @@ async def fetch_user_cache(self) -> bool:
154154
self.log.exception("Failed to fetch the watched users from the API", exc_info=err)
155155
return False
156156

157-
self.watched_users = defaultdict(dict)
157+
self.watched_users.clear()
158158

159159
for entry in data:
160160
user_id = entry.pop("user")
@@ -185,13 +185,16 @@ async def consume_messages(self, delay_consumption: bool = True) -> None:
185185
self.consumption_queue = self.message_queue.copy()
186186
self.message_queue.clear()
187187

188-
for user_channel_queues in self.consumption_queue.values():
189-
for channel_queue in user_channel_queues.values():
188+
for user_id, channel_queues in self.consumption_queue.items():
189+
for channel_queue in channel_queues.values():
190190
while channel_queue:
191191
msg = channel_queue.popleft()
192192

193-
self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)")
194-
await self.relay_message(msg)
193+
if watch_info := self.watched_users.get(user_id, None):
194+
self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)")
195+
await self.relay_message(msg, watch_info)
196+
else:
197+
self.log.trace(f"Not consuming message {msg.id} as user {user_id} is no longer watched.")
195198

196199
self.consumption_queue.clear()
197200

@@ -218,7 +221,7 @@ async def webhook_send(
218221
exc_info=exc
219222
)
220223

221-
async def relay_message(self, msg: Message) -> None:
224+
async def relay_message(self, msg: Message, watch_info: dict) -> None:
222225
"""Relays the message to the relevant watch channel."""
223226
limit = BigBrotherConfig.header_message_limit
224227

@@ -229,7 +232,7 @@ async def relay_message(self, msg: Message) -> None:
229232
):
230233
self.message_history = MessageHistory(last_author=msg.author.id, last_channel=msg.channel.id)
231234

232-
await self.send_header(msg)
235+
await self.send_header(msg, watch_info)
233236

234237
if DiscordTokenFilter.find_token_in_message(msg.content) or WEBHOOK_URL_RE.search(msg.content):
235238
cleaned_content = "Content is censored because it contains a bot or webhook token."
@@ -268,21 +271,19 @@ async def relay_message(self, msg: Message) -> None:
268271

269272
self.message_history.message_count += 1
270273

271-
async def send_header(self, msg: Message) -> None:
274+
async def send_header(self, msg: Message, watch_info: dict) -> None:
272275
"""Sends a header embed with information about the relayed messages to the watch channel."""
273276
if self.disable_header:
274277
return
275278

276-
user_id = msg.author.id
277-
278279
guild = self.bot.get_guild(GuildConfig.id)
279-
actor = await get_or_fetch_member(guild, self.watched_users[user_id]["actor"])
280-
actor = actor.display_name if actor else self.watched_users[user_id]["actor"]
280+
actor = await get_or_fetch_member(guild, watch_info["actor"])
281+
actor = actor.display_name if actor else watch_info["actor"]
281282

282-
inserted_at = self.watched_users[user_id]["inserted_at"]
283+
inserted_at = watch_info["inserted_at"]
283284
time_delta = time.format_relative(inserted_at)
284285

285-
reason = self.watched_users[user_id]["reason"]
286+
reason = watch_info["reason"]
286287

287288
if isinstance(msg.channel, DMChannel):
288289
# If a watched user DMs the bot there won't be a channel name or jump URL
@@ -343,12 +344,13 @@ async def prepare_watched_users_data(
343344
update_cache = False
344345
list_data["updated"] = update_cache
345346

346-
watched_iter = self.watched_users.items()
347+
# Copy into list to prevent issues if it is modified elsewhere while it's being iterated over.
348+
watched_list = list(self.watched_users.items())
347349
if oldest_first:
348-
watched_iter = reversed(watched_iter)
350+
watched_list.reverse()
349351

350352
list_data["info"] = {}
351-
for user_id, user_data in watched_iter:
353+
for user_id, user_data in watched_list:
352354
member = await get_or_fetch_member(ctx.guild, user_id)
353355
line = f"- `{user_id}`"
354356
if member:
@@ -366,8 +368,6 @@ async def prepare_watched_users_data(
366368
def _remove_user(self, user_id: int) -> None:
367369
"""Removes a user from a watch channel."""
368370
self.watched_users.pop(user_id, None)
369-
self.message_queue.pop(user_id, None)
370-
self.consumption_queue.pop(user_id, None)
371371

372372
async def cog_unload(self) -> None:
373373
"""Takes care of unloading the cog and canceling the consumption task."""

poetry.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "MIT"
99
python = "3.12.*"
1010

1111
# See https://bot-core.pythondiscord.com/ for docs.
12-
pydis_core = { version = "11.2.0", extras = ["all"] }
12+
pydis_core = { version = "11.3.0", extras = ["all"] }
1313

1414
aiohttp = "3.9.5"
1515
arrow = "1.3.0"
@@ -37,7 +37,7 @@ pre-commit = "3.7.1"
3737
pip-licenses = "4.5.1"
3838
pytest = "8.2.2"
3939
pytest-cov = "5.0.0"
40-
pytest-subtests = "0.13.0"
40+
pytest-subtests = "0.13.1"
4141
pytest-xdist = "3.6.1"
4242
ruff = "0.5.2"
4343
taskipy = "1.13.0"

0 commit comments

Comments
 (0)