How to edit a message in discord.py

43,590

Solution 1

You can use the Message.edit coroutine. The arguments must be passed as keyword arguments content, embed, or delete_after. You may only edit messages that you have sent.

await message.edit(content="newcontent")

Solution 2

Here's a solution that worked for me.

@client.command()
async def test(ctx):
  message = await ctx.send("hello")
  await asyncio.sleep(1)
  await message.edit(content="newcontent")
Share:
43,590
nijwons
Author by

nijwons

Updated on August 20, 2021

Comments

  • nijwons
    nijwons over 1 year

    I would like to have my bot edit a message if it detects a keyword, i'm not sure how to edit the message though.

    I've looked through the documentation but can't seem to figure it out. I'm using discord.py with python 3.6.

    This is the code:

    @bot.event
    async def on_message(message):
        if 'test' in message.content:
            await edit(message, "testtest")
    

    This is the error:

      File "testthing.py", line 67, in on_message
        await edit(message, "test")
     NameError: name 'edit' is not defined
    

    I would like the bot to edit a message to "testtest" if the message contains the word test, but i just get an error.

  • nijwons
    nijwons over 3 years
    I tried it, but i just get this error: raise Forbidden(r, data) discord.errors.Forbidden: FORBIDDEN (status code: 403): Cannot edit a message authored by another user
  • nijwons
    nijwons over 3 years
    Although I have admin permissions on the bot.
  • Patrick Haugh
    Patrick Haugh over 3 years
    @nijwons Actually, the docs for manage_messages lead me to believe that you cannot edit another users message.
  • nijwons
    nijwons over 3 years
    Oh, is that so? Is it the same for nicknames as i get the same error with that.
  • Patrick Haugh
    Patrick Haugh over 3 years
    @nijwons There is a permission for that: manage_nicknames. Make sure your bot has that, and check if it is a role hierarchy issue. Your bot may not be able to edit users whose highest role is higher than the highest role of the bot.
  • alper
    alper 10 months
    Should we store the message object which is created from await self.channel.send(msg) ?