How to make a bot edit its own message in discord.py

Question:

Is there a way to have a bot edit its own message? I’ve tried to look for an answer but couldn’t find one.

Asked By: user13449177

||

Answers:

This will be done through code. You need to just somehow execute it in your bot program. For example, make a command for it that executes it and later you can delete that.

  1. Get the message object. This can be done by first getting the channel object and then getting the message from it. Basically:
channel = bot.get_channel(id_of_the_channel)
message = await channel.fetch_message(id_of_the_message)

# make sure that you change "id_of_the_channel" for the id of the channel (as an integer)
# and make sure to change "id_of_the_message" for the id of the message (as an integer)
# you can get those by enabling Developer Mode in the Appearance settings in discord
# and right-clicking on the channel to get its id, and right-clicking on the message to get
# its id as well.
  1. After you’ve done that, you call the edit method of that message object to edit it. Also, because it’s a coroutine, you’ll need to await it. Then you need to pass the new text you want the message you’re editing to have, to the content kwarg. For example, if the new text you want to put is “the new content of the message”, you’ll be left with this:
await message.edit(content="the new content of the message")

And that’s basically it. Execute those three lines of code through your bot, and it will edit the message.

Answered By: KickBull

I tried it with another method and it works.
I am using nextcord.py library.

You can do something like this:

@bot.event:
async def on_message(message):
    if message.content == "Test!":
        ctx = await message.reply("Processing!")
        await ctx.edit("Test Successful!")
Answered By: NOOBPOOK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.