猿问

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

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


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


缥缈止盈
浏览 307回答 2
2回答

一只斗牛犬

我建议查看https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html&nbsp;这将帮助您更好地了解 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):我建议在重写版本的新更新出现时及时了解更改。下面是一个工作 cog 文件示例的快速浏览。.&nbsp;希望这有帮助!最大限度。

至尊宝的传说

感谢@Ellisein 帮助我处理class FunCog(commands.Cog):代码字符串。帮助我修复代码的另一件事是bot.add_cog("cogs.fun")将 main.py 中的bot.load_extension("cogs.fun")!
随时随地看视频慕课网APP

相关分类

Python
我要回答