discord.py rewrite:TypeError:cogs必须从Cog派生

问题描述 投票:0回答:2

随着我的机器人越来越大,我正在尝试实施齿轮,但是我遇到了一个问题。我已经准备好了我的整个代码,但由于一些奇怪的原因,我一直收到这个错误:

    Traceback (most recent call last):
  File "C:\Users\Lauras\Desktop\Akagi Bot\main.py", line 107, in <module>
    bot.add_cog("cogs.fun")
  File "C:\Users\Lauras\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 477, in add_cog
    raise TypeError('cogs must derive from Cog')
TypeError: cogs must derive from Cog

我在main.py上的代码如下所示:

   import discord
    import asyncio
    import typing
    import random
    import json
    import oauth
    from discord.ext import commands

bot = commands.Bot(command_prefix='~')

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Activity(name='with Kaga :3',type=0))
    print (discord.__version__)
    print(f"{bot.user.name} - {bot.user.id}")
    print ('Akagi is ready to serve the Commander :3 !')

    bot.add_cog("cogs.fun")
    bot.run(oauth.bot_token)

而“有趣”的齿轮如下:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='~')

class FunCog:
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def hug(self, ctx):
        await ctx.send('has been hugged by', file=discord.File('iloveyou.gif'))
        pass


def setup(bot: commands.Bot):
    bot.add_cog(FunCog(bot))

可能是什么问题呢?我也在使用discord.py重写。谢谢 !

python typeerror discord discord.py
2个回答
1
投票

我建议退房https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html这将有助于您更好地了解Cogs。

首先,您需要将bot.add_cog("cogs.fun")更改为bot.load_extension("cogs.fun")

这不是必需的,但您不需要再次定义bot。将def setup(bot: commands.Bot):改为def setup(bot):

您还需要将class FunCog:更改为class FunCog(commands.Cog):

我建议在重写版本出现新更新时及时了解更改。以下是working cog file.示例的快速浏览。希望这有帮助!最大。


0
投票

感谢@Ellisein帮助我使用class FunCog(commands.Cog):代码。帮助我修复代码的另一件事是用bot.add_cog("cogs.fun")替换main.py中的bot.load_extension("cogs.fun")

© www.soinside.com 2019 - 2024. All rights reserved.