From 94c2484f8773589c11489cbe56106fd2f3e35943 Mon Sep 17 00:00:00 2001 From: Julian Fietkau <git@fietkau.software> Date: Fri, 1 Nov 2024 14:15:43 +0100 Subject: [PATCH] Adjust parser so commands are case insensitive --- parser.py | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/parser.py b/parser.py index e0f9fe9..a4c6052 100644 --- a/parser.py +++ b/parser.py @@ -182,14 +182,14 @@ class Parser: if message.find(' ') != -1: raw_params = message[message.index(' ')+1:] - self.plugin_manager.command_priority_read(user, command, raw_params) + self.plugin_manager.command_priority_read(user, [command[0].lower()] + command[1:], raw_params) if user.lower() in self.storage['ignore_list']: return - if command[0] in self.plugin_manager.get_provided_commands() and command[0] not in reserved_commands: - self.plugin_manager.command_read(user, command, raw_params) + if command[0].lower() in self.plugin_manager.get_provided_commands() and command[0].lower() not in reserved_commands: + self.plugin_manager.command_read(user, [command[0].lower()] + command[1:], raw_params) return - if command[0] == 'about': + if command[0].lower() == 'about': if self.bot_name == 'TimberBot': about = 'I am TimberBot, a chat bot written in Python by Julian Fietkau.' else: @@ -199,7 +199,7 @@ class Parser: core_admin_commands = ['add', 'remove', 'enable', 'disable', 'reload', 'flush_names', 'mute', 'unmute', 'ignore', 'conf', 'plugins'] - if command[0] == 'help': + if command[0].lower() == 'help': if len(command) == 1: command_list = ['help'] command_list += list(self.plugin_manager.get_help_text().keys()) @@ -207,7 +207,7 @@ class Parser: command_list_text = ' · '.join([self.command_prefixes[0] + cmd for cmd in command_list]) self.reply(user, 'Main commands are: ' + command_list_text + '. Type "' + self.command_prefixes[0] + 'help commandname" for details (e.g. "' + self.command_prefixes[0] + 'help ' + command_list[0] + '"). Type "' + self.command_prefixes[0] + 'help commands" for a full command list.') if len(command) >= 2: - if command[1] == 'commands': + if command[1].lower() == 'commands': command_list = ['about', 'help'] command_list = command_list + self.plugin_manager.get_provided_commands() command_list.sort() @@ -218,13 +218,13 @@ class Parser: command_list.sort() command_list_text = 'Admin commands: ' + ' · '.join([self.command_prefixes[0] + cmd for cmd in command_list]) self.reply(user, command_list_text) - elif command[1] == 'help': + elif command[1].lower() == 'help': self.reply(user, 'The ' + self.command_prefixes[0] + 'help command provides usage directions for any active command provided by a plugin. Type "' + self.command_prefixes[0] + 'help commandname" for details, or just "' + self.command_prefixes[0] + 'help" to get a list of commands that have help text available.') - elif command[1] in self.plugin_manager.get_help_text(): + elif command[1].lower() in self.plugin_manager.get_help_text(): self.reply(user, self.plugin_manager.get_help_text()[command[1]]) return - if command[0] == 'admin': + if command[0].lower() == 'admin': if self.is_admin(user, params): if len(command) > 1: command = command[1:] @@ -239,13 +239,13 @@ class Parser: # From this point onward, it is guaranteed that the user is a bot admin (else the method has already returned). - if command[0] in self.plugin_manager.get_provided_admin_commands(): + if command[0].lower() in self.plugin_manager.get_provided_admin_commands(): if self.is_admin(user, params): - self.plugin_manager.admin_command_read(user, command, raw_params) + self.plugin_manager.admin_command_read(user, [command[0].lower()] + command[1:], raw_params) - if command[0] in core_admin_commands: + if command[0].lower() in core_admin_commands: - if command[0] == 'add' and len(command) >= 2: + if command[0].lower() == 'add' and len(command) >= 2: admins = self.get_conf('main', 'admins') successes = [] for user_name in command[1:]: @@ -260,7 +260,7 @@ class Parser: if len(successes) > 1: self.reply(user, 'Added ' + ', '.join(successes) + ' as administrators.') - if command[0] == 'remove' and len(command) >= 2: + if command[0].lower() == 'remove' and len(command) >= 2: admins = self.get_conf('main', 'admins') successes = [] for user_name in command[1:]: @@ -275,8 +275,9 @@ class Parser: if len(successes) >= 1: self.reply(user, 'Removed ' + ', '.join(successes) + ' from administrator list.') - if command[0] == 'enable' and len(command) >= 2: + if command[0].lower() == 'enable' and len(command) >= 2: for plugin_name in command[1:]: + plugin_name = plugin_name.lower() if plugin_name not in self.plugin_manager.active_plugins: self.plugin_manager.scan_plugins() if plugin_name in self.plugin_manager.available_plugins: @@ -291,8 +292,9 @@ class Parser: else: self.reply(user, 'Plugin ' + plugin_name + ' is already enabled.') - if command[0] == 'disable' and len(command) >= 2: + if command[0].lower() == 'disable' and len(command) >= 2: for plugin_name in command[1:]: + plugin_name = plugin_name.lower() if plugin_name in self.plugin_manager.active_plugins: try: status = self.plugin_manager.disable_plugins(plugin_name) @@ -303,21 +305,22 @@ class Parser: else: self.reply(user, 'Plugin ' + plugin_name + ' is not enabled.') - if command[0] == 'reload' and len(command) >= 2: + if command[0].lower() == 'reload' and len(command) >= 2: for plugin_name in command[1:]: + plugin_name = plugin_name.lower() if plugin_name in self.plugin_manager.active_plugins: self.plugin_manager.reload_plugins(plugin_name) self.reply(user, 'Reloaded ' + plugin_name + ' plugin.') else: self.reply(user, 'Plugin ' + plugin_name + ' is not enabled.') - if command[0] == 'plugins': + if command[0].lower() == 'plugins': print_lists = ['active', 'additional'] - if len(command) >= 2 and command[1] == 'active': + if len(command) >= 2 and command[1].lower() == 'active': print_lists = ['active'] - if len(command) >= 2 and command[1] == 'additional': + if len(command) >= 2 and command[1].lower() == 'additional': print_lists = ['additional'] - if len(command) >= 2 and command[1] == 'all': + if len(command) >= 2 and command[1].lower() == 'all': print_lists = ['all'] if 'active' in print_lists: self.reply(user, 'Active plugins: ' + ', '.join(sorted(self.plugin_manager.active_plugins.keys()))) @@ -327,14 +330,14 @@ class Parser: if 'all' in print_lists: self.reply(user, 'All available plugins: ' + ', '.join(sorted(self.plugin_manager.available_plugins))) - if command[0] == 'mute': + if command[0].lower() == 'mute': self.reply(user, 'The bot is now muted and will send no more messages until an admin uses the "unmute" command.') self.set_conf('main', 'mute', True) - if command[0] == 'unmute': + if command[0].lower() == 'unmute': self.set_conf('main', 'mute', False) self.reply(user, 'The bot is now unmuted and will send messages as normal.') - if command[0] == 'ignore': + if command[0].lower() == 'ignore': if len(command) > 1: additions = [] removals = [] @@ -355,13 +358,13 @@ class Parser: self.reply(user, reply) self.set_conf('main', 'ignore_list', self.storage['ignore_list'].join(' ')) - if command[0] == 'flush_names': + if command[0].lower() == 'flush_names': for name in self.storage['names']: if self.storage['names'][name]['status'] in ['online', 'prelim-online'] and name.lower() != self.bot_name: self.storage['names'][name]['status'] = 'offline' self.reply(user, 'Chatter list flushed.') - if command[0] == 'conf': + if command[0].lower() == 'conf': if len(command) < 3: return if len(command) == 3: -- 2.43.0