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

Discord.py如何编辑机器人发送的斜杠命令消息?

解决方案

你的问题核心是没掌握Discord斜杠命令与普通消息的交互逻辑差异,以下是两种可行的修正方案:

方案一:直接编辑原始交互响应

@bot.tree.command(name="image")
@app_commands.describe(arg = "The bot will respond with DALLE's response to this query")
async def image(interaction: discord.Interaction, arg: str):
    # 发送初始等待提示,若要让频道所有人可见,需添加ephemeral=False(默认仅发起者可见)
    await interaction.response.send_message("Waiting for DALL E's response...")
    # 调用DALL-E接口获取图片链接
    response = await getDALLEImage(arg)
    # 编辑原始响应消息
    await interaction.edit_original_response(content=response)

方案二:获取消息对象后编辑

@bot.tree.command(name="image")
@app_commands.describe(arg = "The bot will respond with DALLE's response to this query")
async def image(interaction: discord.Interaction, arg: str):
    await interaction.response.send_message("Waiting for DALL E's response...")
    # 获取已发送的初始消息对象
    original_msg = await interaction.original_response()
    response = await getDALLEImage(arg)
    # 通过消息对象的edit方法修改内容
    await original_msg.edit(content=response)

错误原因说明

  1. 你原来的代码中,interaction.response.send_message()不会返回Message对象,所以messageToEdit不是有效的消息实例,无法用channel.edit_message操作。
  2. 斜杠命令的响应属于Discord交互会话的一部分,必须通过interaction对象提供的专属方法(edit_original_response)或获取原始响应消息对象来编辑,不能用普通频道消息的编辑方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:22:15