【发布时间】: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