Modified commands to work off of reverse dictionary search, allowing for multiple commands to trigger one method.

This commit is contained in:
Tanner Van Teeffelen 2025-06-16 15:23:42 +00:00
parent c04f5dab6a
commit a06f0ad7f2
2 changed files with 21 additions and 13 deletions

15
bot.py
View file

@ -33,6 +33,7 @@ customcommands_dict = methods.loadcommands("customcommands.json")
#Debug message on startup. Not visible to server. #Debug message on startup. Not visible to server.
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f"Current working directory: {os.getcwd()}")
print(f'Logged in as {bot.user}!') print(f'Logged in as {bot.user}!')
#Look for matching commands in JSON file for simple commands. #Look for matching commands in JSON file for simple commands.
@ -40,6 +41,7 @@ async def on_ready():
async def on_message(message): async def on_message(message):
commands_dict = methods.loadcommands("commands.json") commands_dict = methods.loadcommands("commands.json")
print("Loaded commands_dict:", commands_dict)
customcommands_dict = methods.loadcommands("customcommands.json") customcommands_dict = methods.loadcommands("customcommands.json")
if message.author.bot: if message.author.bot:
@ -48,12 +50,19 @@ async def on_message(message):
content = message.content.strip() #Remove the prefix. content = message.content.strip() #Remove the prefix.
if content.startswith(PREFIX): if content.startswith(PREFIX):
parts = content[1:].split() parts = content[1:].split()
cmd = parts[0] cmd = parts[0]
args = parts[1:] args = parts[1:]
if cmd in commands_dict: #Reverse dictionary lookup.
method_name = commands_dict[cmd] 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) method = getattr(methods, method_name, None)
if callable(method): 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.") await message.channel.send("Invalid number of arguments, or there was an error in the method.")
return return
if cmd in customcommands_dict: elif cmd in customcommands_dict:
response = customcommands_dict[cmd] response = customcommands_dict[cmd]
response = response.replace("{mention}", message.author.mention) response = response.replace("{mention}", message.author.mention)

View file

@ -1,11 +1,10 @@
{ {
"hello": "cmd_hello", "cmd_hello": ["hello", "hi"],
"goodbye": "cmd_goodbye", "cmd_goodbye": ["goodbye", "bye"],
"whatareyou": "cmd_whatareyou", "cmd_whatareyou": ["whatareyou"],
"addpoints": "cmd_addpoints", "cmd_addpoints": ["addpoints"],
"checkpoints": "cmd_checkpoints", "cmd_checkpoints": ["checkpoints", "check"],
"addcommand": "cmd_addcommand", "cmd_addcommand": ["addcommand"],
"listcommands": "cmd_listcommands", "cmd_listcommands": ["listcommands", "list"],
"roll": "cmd_roll", "cmd_roll": ["roll", "r"]
"r": "cmd_roll" }
}