'Send message and shortly delete it

I'm trying to make a Discord bot delete its "system messages" after, say, 10 seconds, because I've been seeing a lot of "Invalid command" errors and "Done!" notifications, and I'd like to clear them out for actual messages. This is different from deleting messages where the user has a command; I already have that capability.



Solution 1:[1]

Current API differs from older versions. Proper way to pass timeout looks like this now.

Discord.js v13

message.reply('Invalid command!')
  .then(msg => {
    setTimeout(() => msg.delete(), 10000)
  })
  .catch(console.error);

Discord.js v12

message.reply('Invalid command!')
  .then(msg => {
    msg.delete({ timeout: 10000 })
  })
  .catch(console.error);

Solution 2:[2]

Every Discord.JS Version has a new way to delete with timeout.

Discord.JS V11:

message.channel.send('Test!').then(msg => msg.delete(10000));

Discord.JS V12:

message.channel.send('Test!').then(msg => msg.delete({timeout: 10000}));

Discord.JS V13:

message.channel.send('Test!').then(msg => setTimeout(() => msg.delete(), 10000));

Time is in milliseconds

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Paul Anderson