不和谐\cog.py:672: 运行时警告: 从未等待过协作例程'setup' 安装程序(自身)



我试图使用齿轮来分割我的命令,并保持我的代码有组织,但是我正在努力弄清楚为什么这段代码不适合我,我个人找不到等待函数适合在哪里?

Main.py代码

async def on_ready():
print(f"{bot.user.name} is Online!")
change_status.start()
bot.load_extension("cogs.help")

Cogs Code:

class Test(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.command()
async def hello(self, ctx, *, member: discord.Member):
await ctx.send(f"Hello {member.name}")

async def setup(bot):
bot.add_cog(Test(bot))

我想你可能会对这个链接感兴趣。它解释了如何从不和谐的V1迁移到V2。实际上,由于V2,add_cog函数是异步的,所以您也必须异步地定义setup函数。load_extension函数也一样,它变成了异步的。

async def on_ready():
print(f"{bot.user.name} is Online!")
change_status.start()
await bot.load_extension("cogs.help")

Cogs Code:

class Test(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.command()
async def hello(self, ctx, *, member: discord.Member):
await ctx.send(f"Hello {member.name}")

async def setup(bot):
await bot.add_cog(Test(bot))

您必须等待add_cog使其正常工作。

你必须在setup中编辑add_cog,你可以这样做:-

await bot.add_cog(Test(bot))

设置botbot: commands.Bot,def __init__(self, bot):async def setup(bot):

commands.Bot表示不和谐机器人,bot是需要设置的参数。

您还必须等待bot.add_cog(),正如其他人所提到的。

完整代码:

from discord.ext import commands
class Test(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot

@commands.command()
async def hello(self, ctx, *, member: discord.Member):
await ctx.send(f"Hello {member.name}")

async def setup(bot: commands.Bot):
await bot.add_cog(Test(bot))