0
votes

I am setting up a Discord bot that will delete all messages in a specific text channel in an Interval of 15 minutes but the messages won't delete.

Client.on("message", async function(message) {
    if(message.channel.id != "506258018032418817") return;
    setInterval (function () {
        var Trash = message.channel.fetchMessages({ limit: 100 }).then(
        message.channel.bulkDelete(Trash));
      }, 900000); 
});
1
do you have a link to the Discord API by chance? also is there a reason you need to make the function async if you are not using await? You also are missing a . before the then() so I'm not sure how this is working. Any console errors? - aug
No Error Showing - Vali
@aug If you click on the [discord] tag and then click "Learn more..." you'll find a link to the Discord API. This works for most tags. - Conspicuous Compiler
@aug Oh, but that's the wrong link in this case. You'll want the Discord.JS documentation, not the Discord API - Conspicuous Compiler

1 Answers

0
votes

You're attempting to access the Trash variable, but it doesn't contain what you think it does. Trash is being assigned the Promise that represents the end result of the function chain.

You should take the result being passed into then() and use that as a parameter to your bulkDelete() call.

Client.on("message", async function(message) {
    if(message.channel.id != "506258018032418817") return;
    setInterval (function () {
        message.channel.fetchMessages({ limit: 100 }).then(
           function(deleteThese) { message.channel.bulkDelete(deleteThese); })
      }, 900000); 
});

Keep in mind, the logic for this function is not sound. The pseudo-code goes like this:

- whenever a message is sent to this specific channel
  - queue up a timer to happen in 15 minutes
- after 15 minutes, delete 100 messages from the channel

Consider what happens if this happens

1:00 Paul talks in channel
--- (timer 1 is started)
1:01 Elizabeth responds
--- (timer 2 is started)
... < time passes > ...
1:14 Charles says something in the channel
... < timer 1 triggers; room is wiped > ...
1:15 Diane says something in response to Charles' deleted message
... < timer 2 triggers; room is wiped > ...
1:16 Charles asks Diane to repeat what she just said

You might want to change your approach to pass a time in your fetchMessages() call so it only deletes messages that are 15 minutes old or older.