How to Make a Bot Delete Messages After a Certain Time Period

How to make a bot delete messages after a certain time period

Might want to check out this question here.

As answered in that post the best way to do that is by deleting the message after x amount of seconds.

    message.reply('Hit or miss.')
.then(msg => {
.catch(); /*Used for error handling*/

Proper credit should go to user LW001 who answered the question on the post I referred to.

Making a bot delete its own message after a timeout

You need to get the message that you sent to delete it.`Deleted ${num} messages!`).then(sentMessage => {

This is gonna send the message and after the message was sent it will return it so you can manage it (delete/edit).

Discord PYthon bot deleting own message after some time

await ctx.send("...", delete_after=10.0)

The parameter delete_after will delete the message after 10s.

Alternatively, you can also assign the message to a variable, wait, and then delete.

msg = await ctx.send("...")
await asyncio.sleep(10.0)
await msg.delete()

How to make bot delete its own message after some time?

There is a better way than what Dean Ambros and Dom suggested, you can simply add the kwarg delete_after in .send

await ctx.send('whatever', delete_after=60.0)


Send message and shortly delete it

I recommend you send the message, wait for the response and delete the returned message after that time. Here's how it'd work now:

message.reply('Invalid command')
.then(msg => {
setTimeout(() => msg.delete(), 10000)
.catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

See the Discord.JS Docs for more information about the Message.delete() function and the Node Docs for information about setTimeout().

Old ways to do this were:

Discord.JS v12:

message.reply('Invalid command')
.then(msg => {
msg.delete({ timeout: 10000 })
.catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

Discord.JS v11:

message.reply('Invalid command')
.then(msg => {
.catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

How do I delete discord bot message after time?

If you want that second piece of code to work in the same way that your first, working example works, this is how you would need to change the setInterval and its contents:

setInterval(async () => {

user = client.users.cache.get(config.user);
updated = client.users.cache.get(config.user).presence.status;

if(updated != status) {
status = updated;

if(updated == 'online') {
var message = await channel.send(embed);
message.delete({timeout: 5000});

} else {
}, 1250);

The two major errors you made here are:

  1. You did not define the message variable. You simply did
    channel.send(embed) without putting its result in that variable,
    like so: var message = await channel.send(embed);
  2. Sending a message in a channel is asynchronous. In other words,
    because sending a message requires your code to make a request to
    the Discord API and for the Discord API to then interpret and
    respond to your request, it takes some time for the message to
    actually be sent. As such, your message.delete() line is called
    before the Discord API even gets a chance to send the message in the
    first place, so the code thinks it is going to be deleting a
    non-existent message. This is why you need to use async/await
    here (which you did in your first piece of code but not your
    second); await will wait until the request is completed (aka until
    the Promise is resolved) before continuing to the rest of your code.
    Therefore, by using await, the message deletion timer will only
    begin after the message has successfully been sent, creating the
    functionality you want.

You can alternatively use .then(), but async/await is designed to be a neater and easier way of handling asynchronous Promises like these. Note that in order for await to work, you must set the function it is running in as async. In this scenario, you need to add async before the method running in the setInterval, as shown in my above solution.

discord bot that deletes messages until a specific time on real clock when a command is said

Your minute variable is never changed in the "deleting loop" of your code, so once the program enters the loop, it will never exit.

Try updating the variable while you're inside your while loop like so:

while minute > 0 and minute <= 38:
# do stuff
minute = int(time.strftime("%M"))

Related Topics

Leave a reply