Connor Hancock
Connor Hancock

Reputation: 37

Get bot to edit old message by id

How do i get my discord.py bot to edit an old message by id? i have this:

@client.command()
@commands.has_any_role("role1", "role2")
async def rulesedit(message: messageid):

    embed = discord.Embed(colour=discord.Colour.from_rgb(162, 28, 29))

    embed.add_field("yes")

    await message.message.edit(embed=embed) 

but i get this error despite it being a message the bot has previously already sent:

Command raised an exception: Forbidden: 403 Forbidden (error code: 50005): Cannot edit a message authored by another user

Upvotes: 0

Views: 146

Answers (1)

Dominik
Dominik

Reputation: 3592

You have a few logic errors in your code, so it can't work that way at all.

Point 1: A field in an embed always needs a value and a name, otherwise you get an error.

Point 2: You have to query the message via fetch, but beware: This is an API query and should not be used too often.

Here is a working code:

@client.command()
@commands.has_any_role("role1", "role2")
async def rulesedit(ctx, messageid):
    try:
        msg = await ctx.channel.fetch_message(messageid) # Fetch the ID, needs to be executed in the wanted channel
        embed = discord.Embed(color=discord.Color.green()) # Changed the Color

        embed.add_field(name="TEST", value="TEST") # Added a correct field

        await msg.edit(content = None, embed=embed) # Removed the old content/message and replaced it with the embed
    except:
        return

Of course I changed and omitted some things, you would have to add/change them again if needed.

Upvotes: 1

Related Questions