Tri*_*ath 9 python heroku discord discord.py
我一年前制作并部署到 Heroku 的不和谐机器人一直工作到现在。然而,在更改了一些 cogs 并将 python 更新到版本 3.9.10 后,我在 Heroku 日志中收到以下警告:
app[worker.1]: /app/m_bot.py:120: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
app[worker.1]: client.load_extension(f"cogs.{filename[:-3]}")
app[worker.1]: RuntimeWarning: Enable tracemalloc to get the object allocation traceback
app[worker.1]: Bot is ready.
app[api]: Build succeeded>
Run Code Online (Sandbox Code Playgroud)
120行块是:
app[worker.1]: /app/m_bot.py:120: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
app[worker.1]: client.load_extension(f"cogs.{filename[:-3]}")
app[worker.1]: RuntimeWarning: Enable tracemalloc to get the object allocation traceback
app[worker.1]: Bot is ready.
app[api]: Build succeeded>
Run Code Online (Sandbox Code Playgroud)
机器人上线但不响应任何命令。除了上面列出的内容之外,我没有进行任何其他更改。
当我在我的电脑上运行我的机器人时它可以工作,所以我怀疑这可能是版本问题。
我该如何解决这个问题?
The*_*gUs 18
从discord.py 2.0版本开始,Bot.load_extension现在是一个协程,需要等待。这是为了允许Cog子类使用协程进行覆盖cog_unload。
await必须用在 前面client.load_extension,如图:
await client.load_extension("your_extension")
Run Code Online (Sandbox Code Playgroud)
在你的每个齿轮中:
将标准setup函数替换为异步函数:
async def setup(bot):
await bot.add_cog(YourCog(bot))
Run Code Online (Sandbox Code Playgroud)
如果您想使用常规约定来添加扩展,则需要使用以下代码:
在您客户的文件中:
async def load_extensions():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
# cut off the .py from the file name
await client.load_extension(f"cogs.{filename[:-3]}")
Run Code Online (Sandbox Code Playgroud)
您还应该将登录信息包装在异步“主”函数中,您可以在其中调用此函数。请注意,下面的代码没有设置日志记录,您需要自己设置:
async def main():
async with client:
await load_extensions()
await client.start('your_token')
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)
这两个函数取代了旧的方式:
client.run("your_token")
Run Code Online (Sandbox Code Playgroud)
以及您在问题中发布的代码。
Discord.py 2.0 异步更改(感谢 ChrisDewa 在评论中提到这一点)
| 归档时间: |
|
| 查看次数: |
18420 次 |
| 最近记录: |