Example #1
0
def parse_command_config(parent, parent_name, config):
    mod_config = DataManager.get_manager("mod_config").get_data()
    # Remove commands that don't exist anymore
    to_remove = [command_name for command_name in mod_config[parent_name]["Commands"] if command_name not in config]
    for command_name in to_remove:
        mod_config[parent_name]["Commands"].pop(command_name)
    # Generate config if it doesn't exist
    for command_name in config:
        if command_name not in mod_config[parent_name]["Commands"]:
            mod_config[parent_name]["Commands"][command_name] = {
                "Disabled Channels": [],
                "Disabled Servers": [],
                "Minimum Permissions": ""
            }
    DataManager.get_manager("mod_config").write_data(mod_config)
    # Delete old mods and commands?
    # Spawn command objects based on config and return a dictionary with them
    return {command_name: (Command(parent, command_name, config[command_name]['Aliases'],
                                   config[command_name]["Enabled"],
                                   mod_config[parent_name]["Commands"][command_name]["Minimum Permissions"],
                                   config[command_name]['Help'],
                                   ''.join(use + "\n" for use in config[command_name]['Useage'])[0:-1],
                                   int(config[command_name]["Cool Down"]) if "Cool Down" in config[
                                       command_name] else 0,
                                   config[command_name]["Bypass Server Restrictions"] if
                                   "Bypass Server Restrictions" in config[command_name] else False,
                                   config[command_name]["Bypass Channel Restrictions"] if
                                   "Bypass Channel Restrictions" in config[command_name] else False,
                                   config[command_name]["DM Enabled"] if
                                   "DM Enabled" in config[command_name] else False))
            for command_name in config}
Example #2
0
def load_permissions():
    global default_perm
    global owner_perm
    print("[Loading permissions]", end='')
    permissions_config = DataManager.get_manager("bot_config").get_data(key="Permissions")
    # Build the permissions from the config
    for permission_name in permissions_config:
        # Grab info from each permission
        permission_config = permissions_config[permission_name]
        permission = Permission(permission_name, permission_config["Has Permissions"],
                                permission_config["Is Owner"], permission_config["Inherits"],
                                permission_config["Associated Roles"])
        permissions[permission_name] = permission
        # Check if it's the default permission, making sure there is a max of 1
        if permission_config["Is Default"]:
            assert default_perm is None, "Bot config contains more than one default role."
            default_perm = permission
        # Check if it's the owner permission, making sure there is a max of 1
        if permission_config["Is Owner"]:
            assert owner_perm is None, "Bot config contains more than one owner permission."
            owner_perm = permission
    # Check that there is a min of 1 owner and default permission
    assert owner_perm is not None, "There must be one owner permission"
    assert default_perm is not None, "There must be one default permission"
    print("[Done]")
Example #3
0
 async def load_mods(self):
     print("[Loading Mods]")
     mod_config_manager = DataManager.get_manager("mod_config")
     mod_configs = mod_config_manager.get_data()
     new_mod_config = {}
     # Cycle through all the mods in the mod directory
     # If the mod config doesn't contain the mod -> Generate config
     # If the mod config contains the mod        -> Keep Same config
     for mod_name in os.listdir("Mods/"):
         if mod_name not in mod_configs.keys():
             new_mod_config[mod_name] = {
                 "Commands": {},
                 "Enabled": True,
                 "Disabled Servers": [],
                 "Disabled Channels": []
             }
         else:
             # Append for config cleaning
             new_mod_config[mod_name] = mod_configs[mod_name]
         mod_config_manager.write_data(new_mod_config)
     # Cycle through all the files within the mod dir
     for mod_name in os.listdir("Mods/"):
         # Store the mod names to return them
         # Check if it's a newly installed mod or if the mod is enabled
         # Mod doesn't exist -> Newly installed -> Load it
         # Mod exists        -> Not disabled    -> Load it
         if mod_name not in mod_configs.keys(
         ) or mod_configs[mod_name]['Enabled'] is not False:
             # Make the python files importable and import them
             sys.path.insert(0, 'Mods/' + mod_name)
             print("[+][Loading: %s]" % mod_name)
             # Import and call mod init to get object
             mod = getattr(__import__(mod_name), mod_name)(mod_name,
                                                           self.embed_color)
             # Check for command conflicts and store commands
             for command_name in mod.commands:
                 command_alias = mod.commands[command_name]
                 for alias in command_alias:
                     assert alias != mod.name.lower(
                     ), "Mod name and command alias conflict - " + mod.name
                     # Check for conflicting commands
                     assert alias not in self.mod_command_aliases, "Duplicate mod commands - " + alias
                     # Add as known alias for further conflict checks
                     self.mod_command_aliases.append(alias)
                 # Register command
                 self.commands.append(command_alias)
             # Get mod's info
             # Store mod's info
             self.mods[mod.name] = mod
             print("[Done loading " + mod_name + "]")
         # Mod exists -> Disabled -> Don't load it, as per config
         else:
             print("[-][Not Loading: " + mod_name + "]")
     self.done_loading = True
     print("[Done loading Mods]")
Example #4
0
 async def command_called(self, message, command_alias):
     split_message = message.content.split(" ")
     server = message.guild
     channel = message.channel
     # Make sure everything initialized
     if self.done_loading:
         bot_config = DataManager.get_manager("bot_config")
         # Try to get the command by its alias
         command = self.get_command_by_alias(command_alias)
         # If it's a known command -> call it if it's enabled / allowed to be called
         if command is not None:
             if server is not None:
                 # Grab mod config to check for enabled servers / channels
                 mod_config = DataManager.get_manager(
                     "mod_config").get_data()[command.parent_mod.name]
                 # Check if the command bypasses server restrictions
                 if command.bypass_server_restrictions:
                     await command.call_command(message)
                 # Check if the bot is disabled in the current server
                 elif int(server.id) not in bot_config.get_data(
                         "Disabled Servers"):
                     # Check if command's parent mod is disabled in the current server
                     if int(server.id
                            ) not in mod_config["Disabled Servers"]:
                         # Check if the command bypasses channel restrictions
                         if command.bypass_channel_restrictions:
                             await command.call_command(message)
                         # Check if the bot is disabled in the current channel
                         elif int(channel.id) not in bot_config.get_data(
                                 "Disabled Channels"):
                             # Check if command's parent mod is disabled in the current channel
                             if int(
                                     channel.id
                             ) not in mod_config["Disabled Channels"]:
                                 await command.call_command(message)
                 else:
                     # Check if command's parent mod is disabled in the current server
                     if int(server.id) not in bot_config.get_data(
                             "Disabled Servers"):
                         # Check if command's parent mod is disabled in the current channel
                         if int(channel.id) not in bot_config.get_data(
                                 "Disabled Channels"):
                             if Permissions.has_permission(
                                     message.author,
                                     self.minimum_suggestion_permission):
                                 # No command called -> Not a known command
                                 most_similar_command = most_similar_string(
                                     command_alias,
                                     self.mod_command_aliases)
                                 # No similar commands -> Reply with an error
                                 if most_similar_command is None:
                                     # Reply that neither that mod nor command exists
                                     await Utils.simple_embed_reply(
                                         channel, "[Help]",
                                         "Unknown mod or command - %s" %
                                         split_message[1])
                                     # Similar-looking command exists -> Reply with it
                                 else:
                                     # Reply with a similar command
                                     await Utils.simple_embed_reply(
                                         channel, "[Unknown command]",
                                         "Did you mean `" +
                                         most_similar_command + "`?")
             # It's a PM / DM, so check if the command can be called in DMs
             elif command.dm_enabled:
                 await command.call_command(message)
     # Mods are still loading -> Let the author know
     else:
         await Utils.simple_embed_reply(
             channel, "[Error]", "The bot is still loading, please wait.")