From 62721d027ec094e6abbb6ad10bf903562a8e11ef Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:25:29 +0530 Subject: [PATCH] Add `framework.isobot.commands` module to allow bot cogs to interface with the commands registry --- framework/isobot/commands.py | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 framework/isobot/commands.py diff --git a/framework/isobot/commands.py b/framework/isobot/commands.py new file mode 100644 index 0000000..9014e99 --- /dev/null +++ b/framework/isobot/commands.py @@ -0,0 +1,65 @@ +# Imports +import json +from framework.isobot.colors import Colors as colors + +# Classes +class Commands(): + """The library used to fetch information about isobot commands, and manage them.""" + def __init__(self): + print(f"[Framework/Loader] {colors.green}CommandsDB initialized.{colors.end}") + + def load(self) -> dict: + """Loads the latest content from the commands database onto memory.""" + with open("config/commands.json", 'r', encoding="utf-8") as f: data = json.load(f) + return data + + def save(self, data: dict) -> int: + """Saves the cached database to local machine storage.""" + with open("config/commands.json", 'w+', encoding="utf-8") as f: json.dump(data, f) + return 0 + + def fetch_raw(self) -> dict: + """Returns the commands database in a raw `json` format.""" + return self.load() + + def list_commands(self) -> list: + """Returns all the added commands as a `list`.""" + cmds = self.load() + return cmds.keys() + + def command_disabled_flag(self, command: str, status: bool): + """Enables or disables the `disabled` flag for a command.""" + cmds = self.load() + cmds[command]["disabled"] = bool + self.save(cmds) + return 0 + + def command_bugged_flag(self, command: str, status: bool): + """Enables or disables the `bugged` flag for a command.""" + cmds = self.load() + cmds[command]["bugged"] = bool + self.save(cmds) + return 0 + + def add_command(self, command_name: str, stylized_name: str, description: str, command_type: str, usable_by: str, *, cooldown: int = None): + """Adds a new command entry into the command registry.""" + cmds = self.load() + cmds[command_name] = { + "name": stylized_name, + "description": description, + "type": command_type, + "cooldown": cooldown, + "args": None, + "usable_by": usable_by, + "disabled": False, + "bugged": False + } + self.save(cmds) + return 0 + + def remove_command(self, command): + """Removes a command permanently from the command registry.""" + cmds = self.load() + del cmds[command] + self.save(cmds) + return 0