Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit a message in discord.py

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.

like image 749
nijwons Avatar asked Apr 16 '19 15:04

nijwons


People also ask

Can you edit a discord message?

Click on one of the servers on the left side of your screen and open a text channel. Hover on a message and click the Edit icon from the options that appear on your screen. Alternatively, you can click the ellipsis and select Edit Message from the menu. Type the new message on the field.

How do you quickly edit messages on discord?

Pressing the arrow key up allows you to edit the last message you've written. But it often happens that there is another message after it, but you want to correct the one above it. In my opinion you should be able to edit the message above by pressing the arrow key up once again.


2 Answers

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")
like image 114
Patrick Haugh Avatar answered Oct 12 '22 07:10

Patrick Haugh


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")
like image 26
KaptainRPG Avatar answered Oct 12 '22 05:10

KaptainRPG