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

discord.py rewrite: TypeError: cogs must derive from Cog(discord.py 重写:TypeError:cogs 必须从 Cog 派生)
本文介绍了discord.py 重写:TypeError:cogs 必须从 Cog 派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

As my bot is getting bigger, I'm trying to implement cogs, however I have ran across a problem. I have my whole code set up and ready, but for some weird reason I keep getting this error:

    Traceback (most recent call last):
  File "C:UsersLaurasDesktopAkagi Botmain.py", line 107, in <module>
    bot.add_cog("cogs.fun")
  File "C:UsersLaurasAppDataLocalProgramsPythonPython36libsite-packagesdiscordextcommandsot.py", line 477, in add_cog
    raise TypeError('cogs must derive from Cog')
TypeError: cogs must derive from Cog

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

My code on main.py looks like this:

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

What could be the problem? I'm also using discord.py rewrite. Thanks !

推荐答案

我建议查看 https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html这将帮助您更好地了解 Cogs.

I recommend checking out https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html This will help you get a better understanding of Cogs.

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

Firstly, you need to change bot.add_cog("cogs.fun") to bot.load_extension("cogs.fun")

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

This isn't necessary but you don't need to define bot again. Change def setup(bot: commands.Bot): to def setup(bot):

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

You will also need to change class FunCog: to class FunCog(commands.Cog):

我建议在重写版本的新更新发布时及时了解更改.以下是工作 cog 文件的示例..希望这有帮助!最大.

I recommend staying up to date with changes when new updates come out for the rewrite version. Here is a quick look into an example of a working cog file.. Hope this helped! Max.

这篇关于discord.py 重写:TypeError:cogs 必须从 Cog 派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)