有没有办法在 discord.py-rewrite 中找到所有加载和卸载的齿轮

Is there a way to find all the loaded and unloaded cogs in discord.py-rewrite

我的机器人有很多齿轮,但我使用命令加载和卸载它们

我仍然感到困惑,load/unload 一次又一次的齿轮

我想知道有没有办法使用命令检查所有加载和卸载的齿轮。(除了默认的帮助命令)

查看 discord.py documentation,如果您尝试加载一个已经加载的齿轮, 它将引发 discord.ext.commands.ExtensionAlreadyLoaded 异常。使用这个错误,你可以这样做:

from discord import commands

bot = commands.Bot(command_prefix="!")

@bot.command()
async def check_cogs(ctx, cog_name):
    try:
        bot.load_extension(f"cogs.{cog_name}")
    except commands.ExtensionAlreadyLoaded:
        await ctx.send("Cog is loaded")
    except commands.ExtensionNotFound:
        await ctx.send("Cog not found")
    else:
        await ctx.send("Cog is unloaded")
        bot.unload_extension(f"cogs.{cog_name}")

PS:您的齿轮需要位于 cogs 文件夹中才能使此代码正常工作。