Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a bot's message in discord.py

I have already made all of the proper imports and I have tried looking for answers from other posts but it seems to not quite fit my issue. I am trying to randomly send a message, which I can do. However I can not seem to delete the messages after a certain cool down time. The cool down time is not the issue however. It is deleting the bots message. I know how to delete a user's message but I have very little idea on how I would delete the bots message. Any help would be nice. Here is my code with the exception of my token ID and imports.

async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
    channel = client.get_channel('397920718031159318')
    messages = ["A random cat has appeared", "oh look its a cate"]
    await client.send_message(channel, random.choice(messages))
    time.sleep(3) #I am using this as the cool down time to delete the 
                  #message
    await client.delete_message(messages)
    await asyncio.sleep(4)
like image 991
Cody Vollrath Avatar asked Mar 07 '18 19:03

Cody Vollrath


People also ask

How do I delete Discord messages fast bot?

Open discord and double-click on the saved script to load it. Open the chat that you want to delete and press “T” on your keyboard. This will automatically start deleting the messages fast.


2 Answers

Now (discord.py v1.3.0), just:

import discord

from discord.ext.commands import Bot


bot = Bot()

@bot.command()
async def ping(ctx):
    await ctx.send('pong!', delete_after=5)

bot.run('YOUR_DISCORD_TOKEN')
like image 143
Mikael Flora Avatar answered Oct 27 '22 16:10

Mikael Flora


while not client.is_closed:
    channel = client.get_channel('397920718031159318')
    messages = ["A random cat has appeared", "oh look its a cate"]
    message = await client.send_message(channel, random.choice(messages))
    await asyncio.sleep(3) 
    await client.delete_message(message)
    await asyncio.sleep(4)

You have to capture the message object that send_message produces, and then send that object to delete_message

like image 23
Patrick Haugh Avatar answered Oct 27 '22 15:10

Patrick Haugh