使用discord.py编辑消息报错:'coroutine' object has no attribute 'edit'
问题解决:discord.py编辑消息报错AttributeError
错误原因
- 未正确等待异步方法:
client.fetch_message()是异步函数,直接赋值给msg会得到一个协程对象,而非实际的Message实例,自然没有edit方法。 - 参数传递错误:
msg.edit()中,content参数用于传递文本内容,文件需要通过file参数传入,不能把discord.File传给content。
修正后的代码
chat = client.get_channel(1007301750895677491) # 必须用await获取Message对象 msg = await client.fetch_message(1007556250134917170) # 用file参数传递文件,可同时设置content文本(可选) await msg.edit(content="本周结果:", file=discord.File('week_result.png'))
额外说明
- 如果不需要文本内容,仅替换为图片,可省略
content参数:await msg.edit(file=discord.File('week_result.png')) - 确保你的bot拥有编辑消息的权限,并且目标消息确实存在于指定频道中。
内容的提问来源于stack exchange,提问作者Wolky
相关产品推荐
相关产品推荐

