【问题标题】:Is there a way to find all the loaded and unloaded cogs in discord.py-rewrite有没有办法在 discord.py-rewrite 中找到所有加载和卸载的 cog
【发布时间】:2020-11-12 03:16:21
【问题描述】:

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

我仍然感到困惑并一次又一次地加载/卸载齿轮

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

【问题讨论】:

    标签: python-3.x discord.py-rewrite


    【解决方案1】:

    查看discord.py documentation,如果您尝试加载已加载的 cog, 它将引发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 需要位于 cogs 文件夹中,此代码才能正常工作。

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2010-11-28
      • 2019-08-19
      • 2010-10-11
      相关资源
      最近更新 更多