Ad

How To Make Discord Bot Delete Messages Send By The Bot

- 1 answer

I have made a discord bot that adds people to queue by a command and then shows the queue. I dont want the channel to fill up with unfinished queues. So I want the bot to delete its own messages.

Example:

user says: !dips

Bot says: 1. User #this is message 1.

Another user says: !dips

Bot says: 1. User, 2. Another user. #this is message 2.

Bot deletes now message 1. which is not needed anymore since the same information is in message 2.

Here is the python code I am working with.

@client.event 
async def on_message(message):

if message.content.lower() in list_of_commands:
    await message.channel.send(queue(arg1,arg2,arg3))
    #After sending new message here, it should delete the old message.

Ad

Answer

You could declare the message as a variable and then delete it later.

@client.event 
async def on_message(message):

    if message.content.lower() in list_of_commands:
        msg = await message.channel.send(queue(arg1,arg2,arg3))
        # do your stuff
        await msg.delete()
Ad
source: stackoverflow.com
Ad