From abe8e57ae337916feadfb6c505ff8ce504dbaf14 Mon Sep 17 00:00:00 2001 From: agessaman Date: Sun, 18 Jan 2026 20:04:50 -0800 Subject: [PATCH] feat: Update banned user handling to support prefix matching - Modified configuration examples to clarify that banned users are now identified by sender names using prefix matching. - Implemented a new method in CommandManager to check if a user is banned based on prefix matching, enhancing the bot's ability to ignore messages from banned senders. - Updated message handling logic to utilize the new prefix matching functionality for improved user management. --- config.ini.example | 4 ++-- config.ini.minimal-example | 4 ++-- modules/command_manager.py | 9 +++++++++ modules/core.py | 4 ++-- modules/message_handler.py | 4 ++-- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/config.ini.example b/config.ini.example index f13e7a8..7c3716c 100644 --- a/config.ini.example +++ b/config.ini.example @@ -124,8 +124,8 @@ monitor_channels = general,test,emergency respond_to_dms = true [Banned_Users] -# List of banned user IDs (comma-separated) -# Bot will ignore messages from these users +# List of banned sender names (comma-separated). Matching is prefix (starts-with): +# "Awful Username" also matches "Awful Username 🍆". No bot responses in channels or DMs. banned_users = [Localization] diff --git a/config.ini.minimal-example b/config.ini.minimal-example index 92775c9..94e599c 100644 --- a/config.ini.minimal-example +++ b/config.ini.minimal-example @@ -199,8 +199,8 @@ monitor_channels = general,test,emergency respond_to_dms = true [Banned_Users] -# List of banned user IDs (comma-separated) -# Bot will ignore messages from these users +# List of banned sender names (comma-separated). Matching is prefix (starts-with): +# "Awful Username" also matches "Awful Username 🍆". No bot responses in channels or DMs. banned_users = [Logging] diff --git a/modules/command_manager.py b/modules/command_manager.py index 07fc188..0e4e17f 100644 --- a/modules/command_manager.py +++ b/modules/command_manager.py @@ -402,6 +402,15 @@ class CommandManager: banned = self.bot.config.get('Banned_Users', 'banned_users', fallback='') return [user.strip() for user in banned.split(',') if user.strip()] + def is_user_banned(self, sender_id: Optional[str]) -> bool: + """Check if sender is banned using prefix (starts-with) matching. + + A banned entry "Awful Username" matches "Awful Username" and "Awful Username 🍆". + """ + if not sender_id: + return False + return any(sender_id.startswith(entry) for entry in self.banned_users) + def load_monitor_channels(self) -> List[str]: """Load monitored channels from config""" channels = self.bot.config.get('Channels', 'monitor_channels', fallback='') diff --git a/modules/core.py b/modules/core.py index f4a66bb..43e942f 100644 --- a/modules/core.py +++ b/modules/core.py @@ -524,8 +524,8 @@ monitor_channels = general,test,emergency respond_to_dms = true [Banned_Users] -# List of banned user IDs (comma-separated) -# Bot will ignore messages from these users +# List of banned sender names (comma-separated). Matching is prefix (starts-with): +# "Awful Username" also matches "Awful Username 🍆". No bot responses in channels or DMs. banned_users = [Scheduled_Messages] diff --git a/modules/message_handler.py b/modules/message_handler.py index 66e1e2d..13e56e4 100644 --- a/modules/message_handler.py +++ b/modules/message_handler.py @@ -1966,8 +1966,8 @@ class MessageHandler: if not self.bot.config.getboolean('Bot', 'enabled'): return False - # Check if sender is banned - if message.sender_id and message.sender_id in self.bot.command_manager.banned_users: + # Check if sender is banned (starts-with matching) + if self.bot.command_manager.is_user_banned(message.sender_id): self.logger.debug(f"Ignoring message from banned user: {message.sender_id}") return False