简体   繁体   中英

Edit the last message that the bot sent discord.py

I've been looking around the web to see how I can make the bot edit its last message.

if message.content.lower() == 'edit':
  await message.channel.send('testing')
  channel = bot.get_channel(message.channel.id)
  message = await channel.fetch_message(message.id)
  await asyncio.sleep(3)
  await message.edit(content="the new content of the message")

But it just raises this

Ignoring exception in on_message

Traceback (most recent call last):
    File "/home/runner/FluidLuxuriousCertifications/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
    File "main.py", line 708, in on_message
message = await channel.fetch_message(message.id)
AttributeError: 'NoneType' object has no attribute 'fetch_message'
172.18.0.1 - - [19/Apr/2022 10:39:21] "HEAD / HTTP/1.1" 200 -

I've done this so I can implement a timer feature into my bot and tried this code to test it so it can edit the message as the timer goes down.

Thanks

I found out the answer finally, I used:

if message.content.startswith('edit'):
  test = await message.channel.send('not edited') # Defined message
  await asyncio.sleep(2) # Wait 2 seconds
  await test.edit(content='Edited') # Edit it

Thanks for your help though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM