Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

editing messages with discord.py

I'm trying to edit my bots sent messages, but I'm getting an error

@client.command()
async def edit(ctx):
    message = await ctx.send('testing')
    time.sleep(0.3)
    message.edit(content='v2')

Error:

 RuntimeWarning: coroutine 'Message.edit' was never awaited
  message.edit(content='v2')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

And by the way, is there any way of editing a message by simply having the message ID?

Upvotes: 3

Views: 12107

Answers (1)

Diggy.
Diggy.

Reputation: 6944

time.sleep() is a blocking call, meaning that it pretty much screws up your script. What you'll instead want to use is await asyncio.sleep().

Also, edit() is a coroutine, so it needs to be awaited. Here is what your command should look like:

import asyncio # if you haven't already

@client.command()
async def edit(ctx):
    message = await ctx.send('testing')
    await asyncio.sleep(0.3)
    await message.edit(content='v2')

To edit a message via ID, you'll need the channel that it came from:

@client.command()
async def edit(ctx, msg_id: int = None, channel: discord.TextChannel = None):
    if not msg_id:
        channel = client.get_channel(112233445566778899) # the message's channel
        msg_id = 998877665544332211 # the message's id
    elif not channel:
        channel = ctx.channel
    msg = await channel.fetch_message(msg_id)
    await msg.edit(content="Some content!")

The usage for this command would be !edit 112233445566778899 #message-channel-origin assuming that the prefix is !, and don't bother using the channel argument if the message is in the channel you're executing the command in.


References:

Upvotes: 3

Related Questions