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

Author by
nijwons
Updated on August 20, 2021Comments
-
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 over 3 yearsI 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 over 3 yearsAlthough I have admin permissions on the bot.
-
Patrick Haugh over 3 years@nijwons Actually, the docs for
manage_messages
lead me to believe that you cannot edit another users message. -
nijwons over 3 yearsOh, is that so? Is it the same for nicknames as i get the same error with that.
-
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 10 monthsShould we store the
message
object which is created fromawait self.channel.send(msg)
?