You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pycord中‘Bot’对象不支持异步上下文管理器协议问题求助

解决Discord机器人async with client报错问题

问题原因

报错TypeError: 'Bot' object does not support the asynchronous context manager protocol的核心原因:

  • 使用的discord.py版本过低,v2.0之前的旧版本commands.Bot对象不支持异步上下文管理器协议。
  • client.run()本身是阻塞式调用,不能放在async with代码块中执行。

解决方案

方案1:升级discord.py并调整代码

  1. 执行命令升级依赖库:
pip install -U discord.py
  1. 修改main函数,用client.start(TOKEN)替代client.run(TOKEN)——v2.0+版本中async with需搭配start()使用:
async def main():
    async with client:
        await load()
        await client.start(TOKEN)  # 替换run()为start()

asyncio.run(main())

方案2:保留旧版本,移除async with

如果不想升级库,直接去掉async with块,先加载cogs再启动机器人:

async def main():
    await load()
    await client.run(TOKEN)

asyncio.run(main())

两种方案任选其一即可解决报错问题。

内容的提问来源于stack exchange,提问作者DrBasado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 02:17:11