Search code examples
pythondiscorddiscord.pypycorddisnake

DIscord bot can't edit message


I am having issues with fetching a message by id. It's kinda weird that I can't edit the message I just sent.

I get the error:

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

Code to replicate the error:

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))

I specifically need to get channel and then fetch message by its id. I seems unlogical, but I need it.


Solution

  • The ID you're passing into channel.fetch_message is incorrect.

    The error is on the following line in your code:

    msg_id = msg.id
    

    Instead of getting the message ID, this is getting the interaction ID. The msg variable that you store ctx.respond's return value is actually an interaction object, and not a messageable. Instead, get the msg.message.id which would be:

    msg_id = msg.message.id