diff --git a/cogs/chat.py b/cogs/chat.py index 4ecde05..a8fbb2e 100644 --- a/cogs/chat.py +++ b/cogs/chat.py @@ -1,3 +1,4 @@ +import asyncio import logging from collections import deque from pathlib import Path @@ -125,6 +126,18 @@ class ChatCog(commands.Cog): {"role": "assistant", "content": response} ) + # Wait for any pending sentiment analysis to finish first so + # warnings/mutes appear before the chat reply + sentiment_cog = self.bot.get_cog("SentimentCog") + if sentiment_cog: + key = (message.channel.id, message.author.id) + task = sentiment_cog._debounce_tasks.get(key) + if task and not task.done(): + try: + await asyncio.wait_for(asyncio.shield(task), timeout=15) + except (asyncio.TimeoutError, asyncio.CancelledError): + pass + await message.reply(response, mention_author=False) logger.info( "Chat reply in #%s to %s: %s", diff --git a/cogs/sentiment.py b/cogs/sentiment.py index a6721d1..20f45a6 100644 --- a/cogs/sentiment.py +++ b/cogs/sentiment.py @@ -96,8 +96,12 @@ class SentimentCog(commands.Cog): if existing_task and not existing_task.done(): existing_task.cancel() - # Start new debounce timer - batch_window = config.get("sentiment", {}).get("batch_window_seconds", 3) + # Skip debounce when bot is @mentioned so warnings fire before chat replies + if self.bot.user in message.mentions: + batch_window = 0 + else: + batch_window = config.get("sentiment", {}).get("batch_window_seconds", 3) + self._debounce_tasks[key] = asyncio.create_task( self._debounce_then_process(key, batch_window) ) diff --git a/config.yaml b/config.yaml index a7ca6f7..db4d693 100644 --- a/config.yaml +++ b/config.yaml @@ -11,9 +11,9 @@ monitoring: sentiment: warning_threshold: 0.6 - mute_threshold: 0.75 + mute_threshold: 0.65 spike_warning_threshold: 0.5 # Single message score that triggers instant warning - spike_mute_threshold: 0.8 # Single message score that triggers instant mute + spike_mute_threshold: 0.7 # Single message score that triggers instant mute context_messages: 8 # Number of previous messages to include as context rolling_window_size: 10 # Number of messages to track per user rolling_window_minutes: 15 # Time window for tracking