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

Py-cord/Discord.py无法删除机器人消息问题排查

解决Discord机器人删除消息时的AttributeError问题

问题原因

在py-cord 2.4.1中,ctx.respond()默认以异步方式处理交互(wait=False),返回的是Interaction对象而非发送出去的消息实例,而Interaction没有delete方法,因此触发AttributeError。

解决方案

调用ctx.respond()时添加wait=True参数,让方法等待消息发送完成后返回对应的Message对象,即可正常调用delete()方法。

修正后的代码

import discord
from tools.config import TOKEN

bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")

@bot.slash_command(name="chat", description="Тут можно добавить описание")
async def chat(ctx, msg):
        # 添加wait=True获取Message对象
        bot_msg = await ctx.respond(f'some message with tagging {ctx.author.mention}', wait=True)
        await bot_msg.delete(delay=5)   

bot.run(TOKEN)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 06:04:55