Files
Breehavior-Monitor/cogs/sentiment/coherence.py
AJ Isaacs 660086a500 refactor: extract sentiment cog into package with shared _process_finding
Convert cogs/sentiment.py (1050 lines) into cogs/sentiment/ package:
- __init__.py (656 lines): core SentimentCog with new _process_finding()
  that deduplicates the per-user finding loop from _process_buffered and
  _run_mention_scan (~90 lines each → single shared method)
- actions.py: mute_user, warn_user
- topic_drift.py: handle_topic_drift
- channel_redirect.py: handle_channel_redirect, build_channel_context
- coherence.py: handle_coherence_alert
- log_utils.py: log_analysis, log_action, score_color
- state.py: save_user_state, flush_dirty_states

All extracted modules use plain async functions (not methods) receiving
bot/config as parameters. Named log_utils.py to avoid shadowing stdlib
logging. Also update CLAUDE.md with comprehensive project documentation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 17:06:27 -05:00

44 lines
1.6 KiB
Python

import asyncio
import logging
import discord
from cogs.sentiment.log_utils import log_action
from cogs.sentiment.state import save_user_state
logger = logging.getLogger("bcs.sentiment")
async def handle_coherence_alert(
bot, message: discord.Message, degradation: dict, coherence_config: dict,
db_message_id: int | None, dirty_users: set[int],
):
flag = degradation["flag"]
messages_map = coherence_config.get("messages", {})
alert_text = messages_map.get(flag, messages_map.get(
"default", "You okay there, {username}? That message was... something."
)).format(username=message.author.display_name)
await message.channel.send(alert_text)
await log_action(
message.guild,
f"**COHERENCE ALERT** | {message.author.mention} | "
f"Score: {degradation['current']:.2f} | Baseline: {degradation['baseline']:.2f} | "
f"Drop: {degradation['drop']:.2f} | Flag: {flag}",
)
logger.info(
"Coherence alert for %s: score=%.2f baseline=%.2f drop=%.2f flag=%s",
message.author, degradation["current"], degradation["baseline"],
degradation["drop"], flag,
)
asyncio.create_task(bot.db.save_action(
guild_id=message.guild.id,
user_id=message.author.id,
username=message.author.display_name,
action_type="coherence_alert",
message_id=db_message_id,
details=f"score={degradation['current']:.2f} baseline={degradation['baseline']:.2f} drop={degradation['drop']:.2f} flag={flag}",
))
save_user_state(bot, dirty_users, message.author.id)