问题
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