From a06f0ad7f2d8ad2b9129ab935f6059a92d18d661 Mon Sep 17 00:00:00 2001 From: TVanteeffelen Date: Mon, 16 Jun 2025 15:23:42 +0000 Subject: [PATCH] Modified commands to work off of reverse dictionary search, allowing for multiple commands to trigger one method. --- bot.py | 15 ++++++++++++--- commands.json | 19 +++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bot.py b/bot.py index c6b9aa5..ff13d74 100644 --- a/bot.py +++ b/bot.py @@ -33,6 +33,7 @@ customcommands_dict = methods.loadcommands("customcommands.json") #Debug message on startup. Not visible to server. @bot.event async def on_ready(): + print(f"Current working directory: {os.getcwd()}") print(f'Logged in as {bot.user}!') #Look for matching commands in JSON file for simple commands. @@ -40,6 +41,7 @@ async def on_ready(): async def on_message(message): commands_dict = methods.loadcommands("commands.json") + print("Loaded commands_dict:", commands_dict) customcommands_dict = methods.loadcommands("customcommands.json") if message.author.bot: @@ -48,12 +50,19 @@ async def on_message(message): content = message.content.strip() #Remove the prefix. if content.startswith(PREFIX): + parts = content[1:].split() cmd = parts[0] args = parts[1:] - if cmd in commands_dict: - method_name = commands_dict[cmd] + #Reverse dictionary lookup. + command_to_method = {} + for method_name, aliases in commands_dict.items(): + for alias in aliases: + command_to_method[alias] = method_name + + if cmd in command_to_method: + method_name = command_to_method[cmd] method = getattr(methods, method_name, None) if callable(method): @@ -67,7 +76,7 @@ async def on_message(message): await message.channel.send("Invalid number of arguments, or there was an error in the method.") return - if cmd in customcommands_dict: + elif cmd in customcommands_dict: response = customcommands_dict[cmd] response = response.replace("{mention}", message.author.mention) diff --git a/commands.json b/commands.json index c3fef78..530848d 100644 --- a/commands.json +++ b/commands.json @@ -1,11 +1,10 @@ { - "hello": "cmd_hello", - "goodbye": "cmd_goodbye", - "whatareyou": "cmd_whatareyou", - "addpoints": "cmd_addpoints", - "checkpoints": "cmd_checkpoints", - "addcommand": "cmd_addcommand", - "listcommands": "cmd_listcommands", - "roll": "cmd_roll", - "r": "cmd_roll" -} \ No newline at end of file + "cmd_hello": ["hello", "hi"], + "cmd_goodbye": ["goodbye", "bye"], + "cmd_whatareyou": ["whatareyou"], + "cmd_addpoints": ["addpoints"], + "cmd_checkpoints": ["checkpoints", "check"], + "cmd_addcommand": ["addcommand"], + "cmd_listcommands": ["listcommands", "list"], + "cmd_roll": ["roll", "r"] +}