本文介绍了有没有办法在discord.py-rewrite中找到所有已加载和已卸载的齿轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

查看 discord.py文档,如果您尝试加载已加载的齿轮,它将引发 discord.ext.commands.ExtensionAlreadyLoaded 异常.使用此错误,您可以执行以下操作:

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:您的齿轮需要放置在文件夹中,此代码才能正常工作.

PS: your cogs will need to be in a cogs folder for this code to work.

这篇关于有没有办法在discord.py-rewrite中找到所有已加载和已卸载的齿轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:16