隨著我的機器人變得越來越大,我正在嘗試實現(xiàn)齒輪,但是我遇到了一個問題。我已經(jīng)設置并準備好我的整個代碼,但由于某種奇怪的原因,我不斷收到此錯誤: 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 commandsbot = commands.Bot(command_prefix='~')@bot.eventasync 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 discordfrom discord.ext import commandsbot = 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')) passdef setup(bot: commands.Bot): bot.add_cog(FunCog(bot))可能是什么問題呢?我也在使用 discord.py 重寫。謝謝 !
2 回答

一只斗牛犬
TA貢獻1784條經(jīng)驗 獲得超2個贊
我建議查看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):
我建議在重寫版本的新更新出現(xiàn)時及時了解更改。下面是一個工作 cog 文件示例的快速瀏覽。. 希望這有幫助!最大限度。

至尊寶的傳說
TA貢獻1789條經(jīng)驗 獲得超10個贊
感謝@Ellisein 幫助我處理class FunCog(commands.Cog):
代碼字符串。幫助我修復代碼的另一件事是bot.add_cog("cogs.fun")
將 main.py 中的bot.load_extension("cogs.fun")
!
添加回答
舉報
0/150
提交
取消