From a527346ae66a0052796a37827089f60b38d3c9f5 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 6 Jun 2025 21:19:54 +0000 Subject: [PATCH] Limit rule update to small channels --- cogs/aimod_cog.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cogs/aimod_cog.py b/cogs/aimod_cog.py index c28ee2c..75f881b 100644 --- a/cogs/aimod_cog.py +++ b/cogs/aimod_cog.py @@ -49,6 +49,10 @@ from .aimod_config import ( ) +# Avoid loading an excessive number of messages when updating rules +MAX_RULE_MESSAGES = 25 + + class AIModerationCog(commands.Cog): """ A Discord Cog that uses Google Vertex AI to moderate messages based on server rules. @@ -753,9 +757,18 @@ class AIModerationCog(commands.Cog): ) -> None: """Pull the server rules from a channel and update the global config.""" messages = [] - async for msg in channel.history(limit=None, oldest_first=True): + async for msg in channel.history( + limit=MAX_RULE_MESSAGES + 1, oldest_first=True + ): if msg.content: messages.append(msg.content) + if len(messages) > MAX_RULE_MESSAGES: + await interaction.response.send_message( + f"Channel has more than {MAX_RULE_MESSAGES} messages." + " Please consolidate your rules into fewer messages.", + ephemeral=True, + ) + return if not messages: await interaction.response.send_message(