AttributeError: 'CogLoader' object has no attribute '_BotBase__extensions'

AttributeError: 'CogLoader' object has no attribute '_BotBase__extensions'

作为学习 OOP 的一部分,我正在尝试创建一个 class 来处理加载和卸载齿轮。但是我收到以下错误

Traceback (most recent call last):
  File "c:\Users\mirza\Desktop\Work\Working Directory\Project\BotsB\bot.py", line 50, in <module>
    main()
  File "c:\Users\mirza\Desktop\Work\Working Directory\Project\BotsB\bot.py", line 43, in main
    bot = CustomBotClient()
  File "c:\Users\mirza\Desktop\Work\Working Directory\Project\BotsB\bot.py", line 21, in __init__
    raise e
  File "c:\Users\mirza\Desktop\Work\Working Directory\Project\BotsB\bot.py", line 18, in __init__
    self.load_extension(f"cogs.{filename[:-3]}")
  File "C:\Users\mirza\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 671, in load_extension
    if name in self.__extensions:
AttributeError: 'CustomBotClient' object has no attribute '_BotBase__extensions'

这是 bot.py 中给我错误的代码

import discord
from discord.ext import commands

import os
from dotenv import load_dotenv


class CogLoader(commands.Bot):

    def __init__(self):
        for filename in os.listdir('./cogs'):
            if filename.endswith(".py"):
                try:
                    self.load_extension(f"cogs.{filename[:-3]}")
                except Exception as e:
                    print(f"cogs.{filename[:-3]} cannot be loaded")
                    raise e

    @commands.command()
    async def load(self, ctx, extension):
        self.load_extension(f"cogs.{extension}")

    @commands.command()
    async def unload(self, ctx, extension):
        self.unload_extension(f"cogs.{extension}")

    @commands.command()
    async def reload(self, ctx, extension):
        self.unload_extension(f"cogs.{extension}")
        self.load_extension(f"cogs.{extension}")


class CustomBotClient(CogLoader):
    async def on_ready(self):
        print(f"Bot {self.user} is connected to Discord and ready to roll!")


def main():
    bot = CustomBotClient()

    load_dotenv()
    bot.run(os.getenv("TOKEN"))


if __name__ == "__main__":
    main()

这是我要加载的齿轮

from discord.ext import commands


class Greetings(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="GreetME")
    async def greet_me(self, ctx):
        await ctx.send("Hello! {author.user}")


def setup(bot):
    bot.add_cog(Greetings(bot))

我正在学习 youtube 教程,但他没有为此使用 class。所以,我在这里是一个损失。任何帮助将不胜感激。

您需要在 CogLoader.__init__

中初始化 commands.Bot

这可以通过以下代码完成:

class CogLoader(commands.Bot):

    def __init__(self, command_prefix, **options):
        super().__init__(command_prefix, **options)
        for filename in os.listdir('./cogs'):
            if filename.endswith(".py"):
                try:
                    self.load_extension(f"cogs.{filename[:-3]}")
                except Exception as e:
                    print(f"cogs.{filename[:-3]} cannot be loaded")
                    raise e

def main():
    bot = CustomBotClient(command_prefix="!")

这样,commands.Bot 和 BotBase,它们的属性和方法都被加载了。