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

Discord机器人无法编辑消息:通过ID获取消息报404错误求助

问题原因

你遇到的Unknown Message错误,核心原因是你发送的是临时消息(ephemeral=True)。这类消息是Discord专为命令发起者单独展示的私有消息,不会被存入频道的公共消息池,因此通过channel.fetch_message()根本无法检索到它。

解决方案

如果你必须坚持「先获取频道,再通过消息ID编辑」的需求,唯一可行的办法是放弃临时消息,发送普通可见消息:

修改后的代码如下:

import asyncio
import discord
from discord.ext import commands
from discord.commands import slash_command

class Test(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @slash_command(name='test', description='')
    async def test(self, ctx):
        embed = discord.Embed(
            description=f"The message I want to get by id"
        )
        # 发送普通消息,去掉ephemeral参数(默认False)
        msg = await ctx.respond(embed=embed)
        msg_id = msg.id

        await asyncio.sleep(2)

        channel = self.bot.get_channel(ctx.channel_id)
        message = await channel.fetch_message(msg_id)
        new_embed = discord.Embed(
            description=f"new message test"
        )
        await message.edit(embed=new_embed)

def setup(bot):
    bot.add_cog(Test(bot))
补充说明

如果你的场景必须使用临时消息,那无法通过「频道+ID」的方式编辑——因为临时消息不属于频道消息体系。此时只能直接使用ctx.respond()返回的msg对象进行编辑,也就是直接写await msg.edit(embed=new_embed),这是临时消息唯一的编辑途径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 17:55:19