discord.py重写:TypeError:齿轮必须源自齿轮

时间:2019-03-19 00:13:03

标签: python typeerror discord discord.py

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

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

2 个答案:

答案 0 :(得分:1)

我建议签出https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html 这将帮助您更好地了解齿轮。

首先,您需要将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.的示例的快速浏览。 希望这对您有所帮助!最多

答案 1 :(得分:0)

感谢@Ellisein帮助我解决describe('Section', () => { it('should submit form on blur', () => { const wrapper = shallow(<Section content={{ body: [] }} />); const spy = jest.spyOn(wrapper.instance(), 'submitForm'); wrapper.find('section').simulate('blur'); expect(wrapper.state().formSubmitted).toEqual(true); expect(spy).toHaveBeenCalled(); // Success! }) }) 字符串代码。帮我修复代码的另一件事是用class FunCog(commands.Cog):替换了main.py中的bot.add_cog("cogs.fun")