Untitled

mail@pastecode.io avatar
unknown
python
18 days ago
2.9 kB
3
Indexable
Never
async def load(bot: Brew, *, reload: bool = False) -> tuple[int, int]:
    cwd = os.getcwd()

    search_path = os.path.join(cwd, "src/cogs/**/*.py")
    module_names = glob.glob(search_path, recursive=True)
    success, failures = 0, 0

    for module_name in module_names:
        try:
            relative_path = os.path.relpath(module_name, cwd)
            import_path = (
                relative_path.replace("src" + os.path.sep, "")
                .replace(os.path.sep, ".")
                .replace(".py", "")
            )
            if reload:
                await bot.reload_extension(import_path)
            else:
                await bot.load_extension(import_path)
            success += 1
        except Exception as e:
            _log.error(f"Failed to load module {module_name}: {e}")
            failures += 1
    try:
        await bot.load_extension("jishaku")
    except:
        pass

    style = Fore.RED if failures > 0 else Fore.GREEN

    pfx = "re" if reload else ""
    ssfx = "s" if success != 1 else ""
    fsfx = "s" if failures != 1 else ""

    _log.info(
        f"{style}Successfully {pfx}loaded %d{style} cog{ssfx} with %d{style} failure{fsfx}.{Fore.RESET}",
        success,
        failures,
    )

    if dv.get_prod():
        cmds = {}
        for command in bot.walk_commands():
            params = []
            params_help = []
            for key, value in command.params.items():
                params.append(key.replace("_", " "))
                params_help.append(f"({key.replace('_', ' ')})")
            desc = (
                CommandDescription.load(command.extras)
                if command.extras
                else CommandDescription()
            )

            if desc.dev_command:
                continue

            example = desc.example
            if isinstance(example, list):
                example = example.pop(0)

            syntax_text = desc.manual_syntax if desc.manual_syntax else " ".join(params_help)
            if not desc.example:
                syntax = f"Syntax: {command.qualified_name}"
            else:
                syntax = f"Syntax: {command.qualified_name} {syntax_text}\nExample: {command.qualified_name} {example}".strip()

            cmd = {
                "name": command.qualified_name,
                "description": command.help or "No description provided.",
                "aliases": command.aliases,
                "params": params,
                "info": desc.info,
                "syntax": syntax,
            }

            category = command.module.split(".")[1:0:-1][0]
            if category not in cmds:
                cmds[category] = []

            cmds[category].append(cmd)

        bot.redis.delete("commands")
        for category, commands in cmds.items():
            bot.redis.hset("commands", category, json.dumps(commands))

    return success, failures
Leave a Comment