python-3.x 命令“reload”引发异常:TypeError:BotBase.reload_extension()缺少1个必需的位置参数:'姓名'

ztmd8pv5  于 2023-06-07  发布在  Python
关注(0)|答案(2)|浏览(148)

我和我的女朋友有一个discord.py 2.0机器人,它可以做“x”的事情,我添加了一个命令来重新加载所有的扩展。但是当执行相同的命令时,它返回以下错误:
Hybrid command raised an error: Command 'reload' raised an exception: TypeError: BotBase.reload_extension() missing 1 required positional argument: 'self'
下面是代码:
reload.py:

import discord
import settings

from discord.ext import commands
from discord.interactions import Interaction

@commands.hybrid_command(name="reload", description="Reloads all extensions")
async def reload(ctx):
     for cmd_file in settings.CMDS_DIR.glob("*.py"):
            if cmd_file.name != "__init__.py":
                extensions = f"extension.{cmd_file.name[:-3]}"
                await commands.Bot.reload_extension(name=extensions)
                print(f"Reloaded extension: {extensions}")

async def setup(bot):
    bot.add_command(reload)

在main.py文件中,它使用以下代码加载扩展:

@bot.event
async def on_ready():
        print(f'Logged in as {bot.user.name}')
        for guild in bot.guilds:
            print(f"Bot is in {guild.name} with {guild.member_count} members")
        bot.remove_command('help')
        for cmd_file in settings.CMDS_DIR.glob("*.py"):
            if cmd_file.name != "__init__.py":
                extensions = f"extension.{cmd_file.name[:-3]}"
                await bot.load_extension(extensions)
                print(f"Loaded extension: {extensions}")
        try:
            synced = await bot.tree.sync()
            print(f"Synced {len(synced)} slash commands\n")
        except Exception as e:
            print(f"Failed to sync slash commands: {e}")

预期的功能:重新加载所有命令/扩展。
我收到的回复是:Hybrid command raised an error: Command 'reload' raised an exception: TypeError: BotBase.reload_extension() missing 1 required positional argument: 'name'

rks48beu

rks48beu1#

commands.Bot是一个类,你需要使用示例(你在主文件中定义的)。
由于您无法直接访问bot示例,因此可以使用ctx.bot来访问它:

import discord
import settings

from discord.ext import commands
from discord.interactions import Interaction

@commands.hybrid_command(name="reload", description="Reloads all extensions")
async def reload(ctx):
    for cmd_file in settings.CMDS_DIR.glob("*.py"):
        if cmd_file.name != "__init__.py":
            extensions = f"extension.{cmd_file.name[:-3]}"
            await ctx.bot.reload_extension(name=extensions)
            print(f"Reloaded extension: {extensions}")

async def setup(bot):
    bot.add_command(reload)
uqcuzwp8

uqcuzwp82#

commands.Bot是类。您需要使用该类的对象,这就是全局bot。那么

await bot.reload_extension(name=extensions)

相关问题