Enhance mention handling in message processing

- Updated `config.ini.example` to introduce the `respond_to_mentions` setting, allowing configuration of how the bot responds to mentions in channel messages.
- Refactored `MessageHandler` to implement logic for handling mentions based on the new configuration, including stripping mentions when appropriate.
- Added `cleanup_message_for_matching` method in `BaseCommand` to streamline message processing and mention validation.
- Enhanced various command classes to utilize the new cleanup method for consistent mention handling.
- Introduced tests to validate the behavior of the new mention handling logic across different configurations.
This commit is contained in:
agessaman committed 2026-04-11 18:45:34 -07:00
1 parent 4ee20794df
commit 8bea10cdc3
21 files changed
+415 -113

No files matched your search

+15 -1
View File
@@ -2849,7 +2849,21 @@ class MessageHandler:
if not self.should_process_message(message):
return
self.logger.info(f"Processing message: {message.content}")
# Handle respond_to_mentions for channel messages
if not message.is_dm:
_mention_mode = self.bot.config.get('Bot', 'respond_to_mentions', fallback='also').strip().lower()
if _mention_mode in ('also', 'only'):
import re
_bot_name = self.bot.config.get('Bot', 'bot_name', fallback='Bot')
_mention = f'@[{_bot_name}]'
_has_mention = _mention.lower() in message.content.lower()
if _mention_mode == 'only' and not _has_mention:
self.logger.debug(f"Ignoring channel message (respond_to_mentions=only, no mention of {_mention})")
return
if _has_mention:
message.content = re.sub(re.escape(_mention), '', message.content, flags=re.IGNORECASE).strip()
self.logger.info(f"Processing message: '{message.content}' from {message.sender_id} in {'DM' if message.is_dm else message.channel}")
# Check for advert command (DM only)
if message.is_dm and message.content.strip().lower() == "advert":