Discord Bot开发遇RuntimeWarning:协程'Context.send'未被等待
问题:Discord Bot 运行时触发协程未等待警告
错误详情
RuntimeWarning: coroutine 'Context.send' was never awaited
ctx.send('Hello')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我的代码(已省略Bot令牌)
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents) @bot.command() async def test(ctx): await ctx.send("Hello!") bot.run("INSERT_BOT_TOKEN")
前置操作
之前遇到Python环境PATH配置问题,已卸载Python并清理环境变量,从官网重新下载安装时勾选了自动配置PATH选项,但错误仍未解决。
排查与解决方法
确认代码一致性
检查正在运行的脚本是否和你提供的代码完全一致——常见问题是修改代码后未保存,或运行了旧版本的脚本。确保test函数里的ctx.send确实带有await关键字,漏加该关键字是触发此警告的核心原因。验证版本兼容性
- 运行
python --version确认Python版本≥3.8(discord.py v2.x的最低要求)。 - 执行
pip install --upgrade discord.py,强制更新到最新版discord.py,避免版本不匹配导致的异常。
- 运行
检查环境配置
- Windows执行
where python,Linux/macOS执行which python,确认当前使用的Python解释器是你刚安装的版本,防止调用到残留的旧环境。 - 执行
pip list查看discord.py是否已正确安装在当前环境中。
- Windows执行
测试最小化验证代码
运行以下极简代码测试环境是否正常:import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents) @bot.event async def on_ready(): print(f'Logged in as {bot.user} (ID: {bot.user.id})') @bot.command() async def hello(ctx): await ctx.send('Hello World!') bot.run('你的Bot令牌')如果这段代码能正常响应
$hello命令,说明之前的问题出在代码保存或运行环境的调用错误上。
内容的提问来源于stack exchange,提问作者gzeesyd
相关产品推荐
相关产品推荐

