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

ⅰ亾dé卋堺 提交于 2021-01-29 06:58:28

问题


I have a lot of cogs for my bot, but i load and unload them using commands

Still I get confused and load/unload a cog again-and-again

I wonder is there a way to check all the loaded and unloaded cogs using commands.(Other than the default help command)


回答1:


Looking at discord.py documentation, if you try to load a cog that is already loaded, it will raise a discord.ext.commands.ExtensionAlreadyLoaded exception. Using this error, you could do this:

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: your cogs will need to be in a cogs folder for this code to work.



来源:https://stackoverflow.com/questions/63036583/is-there-a-way-to-find-all-the-loaded-and-unloaded-cogs-in-discord-py-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!