diff options
-rw-r--r-- | bot/exts/moderation/watchchannels/_watchchannel.py | 40 | ||||
-rw-r--r-- | poetry.lock | 14 | ||||
-rw-r--r-- | pyproject.toml | 4 |
3 files changed, 29 insertions, 29 deletions
diff --git a/bot/exts/moderation/watchchannels/_watchchannel.py b/bot/exts/moderation/watchchannels/_watchchannel.py index 71600e9df..44c0be2a7 100644 --- a/bot/exts/moderation/watchchannels/_watchchannel.py +++ b/bot/exts/moderation/watchchannels/_watchchannel.py @@ -62,7 +62,7 @@ class WatchChannel(metaclass=CogABCMeta): self.log = logger # Logger of the child cog for a correct name in the logs self._consume_task = None - self.watched_users = defaultdict(dict) + self.watched_users = {} self.message_queue = defaultdict(lambda: defaultdict(deque)) self.consumption_queue = {} self.retries = 5 @@ -154,7 +154,7 @@ class WatchChannel(metaclass=CogABCMeta): self.log.exception("Failed to fetch the watched users from the API", exc_info=err) return False - self.watched_users = defaultdict(dict) + self.watched_users.clear() for entry in data: user_id = entry.pop("user") @@ -185,13 +185,16 @@ class WatchChannel(metaclass=CogABCMeta): self.consumption_queue = self.message_queue.copy() self.message_queue.clear() - for user_channel_queues in self.consumption_queue.values(): - for channel_queue in user_channel_queues.values(): + for user_id, channel_queues in self.consumption_queue.items(): + for channel_queue in channel_queues.values(): while channel_queue: msg = channel_queue.popleft() - self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)") - await self.relay_message(msg) + if watch_info := self.watched_users.get(user_id, None): + self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)") + await self.relay_message(msg, watch_info) + else: + self.log.trace(f"Not consuming message {msg.id} as user {user_id} is no longer watched.") self.consumption_queue.clear() @@ -218,7 +221,7 @@ class WatchChannel(metaclass=CogABCMeta): exc_info=exc ) - async def relay_message(self, msg: Message) -> None: + async def relay_message(self, msg: Message, watch_info: dict) -> None: """Relays the message to the relevant watch channel.""" limit = BigBrotherConfig.header_message_limit @@ -229,7 +232,7 @@ class WatchChannel(metaclass=CogABCMeta): ): self.message_history = MessageHistory(last_author=msg.author.id, last_channel=msg.channel.id) - await self.send_header(msg) + await self.send_header(msg, watch_info) if DiscordTokenFilter.find_token_in_message(msg.content) or WEBHOOK_URL_RE.search(msg.content): cleaned_content = "Content is censored because it contains a bot or webhook token." @@ -268,21 +271,19 @@ class WatchChannel(metaclass=CogABCMeta): self.message_history.message_count += 1 - async def send_header(self, msg: Message) -> None: + async def send_header(self, msg: Message, watch_info: dict) -> None: """Sends a header embed with information about the relayed messages to the watch channel.""" if self.disable_header: return - user_id = msg.author.id - guild = self.bot.get_guild(GuildConfig.id) - actor = await get_or_fetch_member(guild, self.watched_users[user_id]["actor"]) - actor = actor.display_name if actor else self.watched_users[user_id]["actor"] + actor = await get_or_fetch_member(guild, watch_info["actor"]) + actor = actor.display_name if actor else watch_info["actor"] - inserted_at = self.watched_users[user_id]["inserted_at"] + inserted_at = watch_info["inserted_at"] time_delta = time.format_relative(inserted_at) - reason = self.watched_users[user_id]["reason"] + reason = watch_info["reason"] if isinstance(msg.channel, DMChannel): # If a watched user DMs the bot there won't be a channel name or jump URL @@ -343,12 +344,13 @@ class WatchChannel(metaclass=CogABCMeta): update_cache = False list_data["updated"] = update_cache - watched_iter = self.watched_users.items() + # Copy into list to prevent issues if it is modified elsewhere while it's being iterated over. + watched_list = list(self.watched_users.items()) if oldest_first: - watched_iter = reversed(watched_iter) + watched_list.reverse() list_data["info"] = {} - for user_id, user_data in watched_iter: + for user_id, user_data in watched_list: member = await get_or_fetch_member(ctx.guild, user_id) line = f"- `{user_id}`" if member: @@ -366,8 +368,6 @@ class WatchChannel(metaclass=CogABCMeta): def _remove_user(self, user_id: int) -> None: """Removes a user from a watch channel.""" self.watched_users.pop(user_id, None) - self.message_queue.pop(user_id, None) - self.consumption_queue.pop(user_id, None) async def cog_unload(self) -> None: """Takes care of unloading the cog and canceling the consumption task.""" diff --git a/poetry.lock b/poetry.lock index de69b5bac..f66ef66f1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1568,13 +1568,13 @@ yaml = ["pyyaml (>=6.0.1)"] [[package]] name = "pydis-core" -version = "11.2.0" +version = "11.3.0" description = "PyDis core provides core functionality and utility to the bots of the Python Discord community." optional = false python-versions = "<4.0,>=3.11" files = [ - {file = "pydis_core-11.2.0-py3-none-any.whl", hash = "sha256:e06adeb17b25c88e8531712ea3910a7c35b977e0b04711cdecd2089efce1b546"}, - {file = "pydis_core-11.2.0.tar.gz", hash = "sha256:8ecdc3a564ef7f34c4688c1bc064e4871555f22f239845fb2d9cb74df9006ae8"}, + {file = "pydis_core-11.3.0-py3-none-any.whl", hash = "sha256:0a1c37f56df0f639f252e71504d98599679b64fe282331db027ae63dc441a845"}, + {file = "pydis_core-11.3.0.tar.gz", hash = "sha256:6c1141a05b067cf272cb816d4bee18a0e5d8ca264b5a94490be4001d074b2ac1"}, ] [package.dependencies] @@ -1641,13 +1641,13 @@ testing = ["fields", "hunter", "process-tests", "pytest-xdist", "virtualenv"] [[package]] name = "pytest-subtests" -version = "0.13.0" +version = "0.13.1" description = "unittest subTest() support and subtests fixture" optional = false python-versions = ">=3.7" files = [ - {file = "pytest_subtests-0.13.0-py3-none-any.whl", hash = "sha256:5a142064218df37a52299ddb393b1a6b2c1161ca19e71ca60c3a8040eb17e811"}, - {file = "pytest_subtests-0.13.0.tar.gz", hash = "sha256:9e02b9d243c0379b02abf3e0887da122bcb2714b021c3608a37f17ce210adce5"}, + {file = "pytest_subtests-0.13.1-py3-none-any.whl", hash = "sha256:ab616a22f64cd17c1aee65f18af94dbc30c444f8683de2b30895c3778265e3bd"}, + {file = "pytest_subtests-0.13.1.tar.gz", hash = "sha256:989e38f0f1c01bc7c6b2e04db7d9fd859db35d77c2c1a430c831a70cbf3fde2d"}, ] [package.dependencies] @@ -2405,4 +2405,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "3.12.*" -content-hash = "aa648c93f273c786c4b2d408ef423995298fc8383045b73c8555632fbcbf3b63" +content-hash = "6301b50e6ebb7d1d97d73884b29fe48df6d81ce2af6ffc89a224d26d92cf3d95" diff --git a/pyproject.toml b/pyproject.toml index 47f0b17c6..f8a15c926 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ license = "MIT" python = "3.12.*" # See https://bot-core.pythondiscord.com/ for docs. -pydis_core = { version = "11.2.0", extras = ["all"] } +pydis_core = { version = "11.3.0", extras = ["all"] } aiohttp = "3.9.5" arrow = "1.3.0" @@ -37,7 +37,7 @@ pre-commit = "3.7.1" pip-licenses = "4.5.1" pytest = "8.2.2" pytest-cov = "5.0.0" -pytest-subtests = "0.13.0" +pytest-subtests = "0.13.1" pytest-xdist = "3.6.1" ruff = "0.5.2" taskipy = "1.13.0" |