DIScord 机器人无法编辑消息

问题描述 投票:0回答:1

我在通过 ID 获取消息时遇到问题。有点奇怪,我无法编辑刚刚发送的消息。

我收到错误:

discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

复制错误的代码:

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"
        )
        msg = await ctx.respond(embed=embed, ephemeral=True)
        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 获取消息。我看起来不合逻辑,但我需要它。

python discord discord.py pycord disnake
1个回答
0
投票

您传入的 ID

channel.fetch_message
不正确。

错误位于代码中的以下行:

msg_id = msg.id

这不是获取消息 ID,而是获取 交互 ID。您存储

msg
返回值的
ctx.respond
变量实际上是一个 交互对象,而不是可消息对象。相反,获取
msg.message.id
,即:

msg_id = msg.message.id
© www.soinside.com 2019 - 2024. All rights reserved.