【问题标题】:discord.py rewrite: TypeError: cogs must derive from Cogdiscord.py 重写:TypeError:cogs 必须从 Cog 派生
【发布时间】:2019-08-09 10:36:21
【问题描述】:

随着我的机器人越来越大,我正在尝试实现 cogs,但是我遇到了一个问题。我已经设置并准备好了整个代码,但由于某些奇怪的原因,我不断收到此错误:

    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 重写。谢谢!

【问题讨论】:

  • 您的 cog 没有从 Cog 继承...错误很明显。
  • 我不太明白,我错过了什么?
  • class Funcog: 替换为class FunCog(commands.Cog):
  • 我做到了,但我仍然得到同样的错误:/我认为 bot.add_cog("cogs.fun") 这条线在某种程度上导致了问题,不是吗?
  • 最近 cogs 的工作方式发生了变化,您可以在此处找到这些变化的文档:discordpy.readthedocs.io/en/rewrite/ext/commands/cogs.html

标签: python typeerror discord discord.py


【解决方案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. 示例的快速浏览。 希望这有帮助!最大。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2021-03-11
      相关资源
      最近更新 更多