nijwons
nijwons

Reputation: 305

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.

Upvotes: 21

Views: 77713

Answers (8)

Rea
Rea

Reputation: 1

@bot.event
async def on_message(message):
  if message.content == 'test':
    messages=await message.channel.send("CONTENT")
    await asyncio.sleep(INT)
    await messages.edit(content="NEW CONTENT")

Upvotes: 0

clxrity
clxrity

Reputation: 336

Assign the original message to a variable. Reference the variable with .edit(content='content').

(You need "content=" in there).

@bot.command()
async def test(ctx):
    msg = await ctx.send('test')
    await msg.edit(content='this message has been edited')

Upvotes: 0

Hayley Davies
Hayley Davies

Reputation: 1

If you are wanting to update responses in discord.py you have to use:

@tree.command(name = 'foobar', description = 'Send the word foo and update it to say bar')
async def self(interaction: discord.Interaction):
    await interaction.response.send_message(f'foo', ephemeral = True)
    time.sleep(1)
    await interaction.edit_original_response(content=f'bar')

Upvotes: 0

Xenon
Xenon

Reputation: 1

This is what I did:

@bot.event
async def on_message(message):
  if message.content == 'test':
    await message.channel.send('Hello World!')
    await message.edit(content='testtest')

I don't know if this will work for you, but try and see.

Upvotes: -1

FLAK-ZOSO
FLAK-ZOSO

Reputation: 4098

Did you do this:

from discord import edit

or this:

from discord import *

before using message.edit function?

If you did it, maybe the problem is with your discord.py version. Try this:

print(discord.__version__)

Upvotes: 0

lunarcySays
lunarcySays

Reputation: 1

Please try to add def to your code like this:

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

Upvotes: -1

KaptainRPG
KaptainRPG

Reputation: 101

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

Upvotes: 10

Patrick Haugh
Patrick Haugh

Reputation: 60954

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

Upvotes: 26

Related Questions