How to Add a Delay to Message.Delete()

How to add a delay to message.delete()?

TextChannel.send(...) returns a Message too, so if you want to store the message that your bot just sent, you can do:

botSentMessage = await channel.send("some message")

Then finally, to add delay, you can use await asyncio.sleep(delayInSeconds).

Your end result should be this:

if message.content.startswith('!commands'):
channel = client.get_channel(525412770062139402)
if message.channel is not channel:
await message.delete()
channel = message.channel

# Gets the message the bots just sent.
botsMessage = await channel.send("{0.mention} Please use bot commands in the #help channel.".format(message.author))
# Waits for 3 seconds before continuing to the next line of code
await asyncio.sleep(3)
await botsMessage.delete()

Discord.js - Attempting to add a delay to Deleting message command

setTimeout's first argument is the callback which will be invoked after the duration (the second argument)

For example,

await message.channel.messages.fetch({limit: 2 + args[0]}).then(messages =>{
message.channel.send('Deleting '+args[0]+' messages in 5 seconds...')
setTimeout(() => {
message.channel.bulkDelete(messages);
}, 5000)
...

As a general note, mixing await and classic Promise chaining (.then) is interesting. Something like

// helper so you can await a setTimeout
function sleep(seconds) {
return new Promise(r => setTimeout(r, seconds * 1000))
}


const messages = await message.channel.messages.fetch({limit: 2 + args[0]})
message.channel.send('Deleting '+args[0]+' messages in 5 seconds...')
await sleep(5) // see above
message.channel.bulkDelete(messages);

keeps your code flat and, imo more readable.

How can I set a delay before deleting an embed with discord.js?

you really messed up the .then

.then is another way of handling promises and callbacks.. but in your code, there is no callback.

what you should do is put the code that deletes the message outside and above the .then

afterwards, make sure that .then actually has a callback. it should look like this:

command(client, 'title' , (message) => {

if(message.content.startsWith('!' + 'title')) {
message.delete()
}

const embed = new Discord.MessageEmbed()
.setTitle('Title Here')

message.channel.send(embed).then(msg => {

setTimeout(() => {
msg.delete()
}, 5000)
})

what this does is that, when you run the command, it deletes the invocation. it then sends the embed and waits until the discord api responds saying “the message was sent successfully!”, to which it runs the timeout for 5 seconds and deletes the embed.

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 can I make my bot delete its own message?

You can store sent messages in a variable and use asyncio's sleep function for the delay, or delete()'s kwarg, delay:

@bot.command()
async def disappear(ctx):
msg = await ctx.send("Hey!")
await asyncio.sleep(1)
await msg.delete()

And the kwarg:

@bot.command()
async def disappear(ctx):
msg = await ctx.send("Hey!")
await msg.delete(delay=1)

References:

  • asyncio.sleep
  • Message.delete()


Related Topics



Leave a reply



Submit