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 => {
msg.delete(10000)
})
.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.
message.channel.send(`Deleted ${num} messages!`).then(sentMessage => {
sentMessage.delete(5000);
});
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 discord.py 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)
reference
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 => {
msg.delete(10000)
})
.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 {
return
}
}, 1250);
The two major errors you made here are:
- You did not define the
message
variable. You simply didchannel.send(embed)
without putting its result in that variable,
like so:var message = await channel.send(embed)
; - 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, yourmessage.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 useasync
/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 usingawait
, 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
Input Date Format With Dd/Mm/Yyyy in the Input Field
Onclick Button Get from Database and Display on a Input Field Live
What Does This Format Means T00:00:00.000Z
Jquery: Simplest Way to Check Url for a Parameter and Pass It to Anchor Href If It Exists
Require(Img Path) Not Working/ Cannot Find Module "." Reactjs
How to Read a Properties File in JavaScript from Project Directory
Convert Milliseconds to Hours and Minutes Using Momentjs
How to Sort a Map by Value in JavaScript
How to Reload a Page With React-Router
How to Include External JavaScript Library in Reactjs
Clear an Input Field With Reactjs
How to Check If a Textbox Is Empty Using JavaScript
Image Taken from Camera Not Saved to Gallery
How to Get Date Value from One Date Input Field and Put It to Other Date Field Using JavaScript