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.
@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)

View file

@ -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"
"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"]
}