0

I'm new to discord bots. I have a script that gives members a role if the join a VC, that way they can see a Text channel. I want the channel to be cleared out.

This is what I have so far:

module.exports = (client) => {
    // Clears chat?
    client.on('messageCreate', async function (message) {
        if (message.channel.id === '965002780878176286') { //Bar-Chat
            var CheckMinutes = 1, CheckBAR = CheckMinutes * 60 * 1000;
            setInterval(function () {
                message.channel.messages.fetch({ limit: 100 }).then(function (deleteThese) { message.channel.bulkDelete(deleteThese); })
            }, CheckBAR);
        };
        if (message.channel.id === '965266326828490792') { //Just-Chat
            var CheckMinutes = 1, CheckJC = CheckMinutes * 60 * 1000;
            setInterval(function () {
                message.channel.messages.fetch({ limit: 100 }).then(function (deleteThese) { message.channel.bulkDelete(deleteThese); })
            }, CheckJC);
        };
        if (message.channel.id === '965266431589613618') { //Game-Chat #1
            var CheckMinutes = 1, CheckGC1 = CheckMinutes * 60 * 1000;
            setInterval(function () {
                message.channel.messages.fetch({ limit: 100 }).then(function (deleteThese) { message.channel.bulkDelete(deleteThese); })
            }, CheckGC1);
        };
        if (message.channel.id === '965266551857115156') { //Game-Chat #2
            var CheckMinutes = 1, CheckGC2 = CheckMinutes * 60 * 1000;
            setInterval(function () {
                message.channel.messages.fetch({ limit: 100 }).then(function (deleteThese) { message.channel.bulkDelete(deleteThese); })
            }, CheckGC2);
        };
        if (message.channel.id === '965266660003049472') { //Game-Chat #3
            var CheckMinutes = 1, CheckGC3 = CheckMinutes * 60 * 1000;
            setInterval(function () {
                message.channel.messages.fetch({ limit: 100 }).then(function (deleteThese) { message.channel.bulkDelete(deleteThese); })
            }, CheckGC3);
        };
    });
}

Is there a better way of doing this? Don't mind the short timer (still testing and don't want to wait like 15/30 mins).

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Sander
  • 11
  • 1
  • you don't have to fetch the messages in the channel befor bulk deleting them. You can easily just use .bulkDelete(); & it will delete the messages that arent older then 14 days. – Vxrious Apr 21 '22 at 17:50
  • @Sander are you wanting different timers for each channel or same for all? Are you trying to keep the channel clear or just wanting to delete messages once they get a certain age? Lastly, does this need to survive a bot reboot (database will be needed)? – Kaspr Apr 21 '22 at 23:54
  • If you want to clear all messages in a text channel, it may be easier to just clone the channel instead of repeatedly bulk deleting messages in the channel. You could check out the second snippet of code in [this answer](https://stackoverflow.com/a/71714984/6901876) for a loosely related example of how that could be done. By cloning the channel, the channel will keep all permissions and settings, but it will seem as though all messages have been cleared instantly. – Cannicide Apr 22 '22 at 07:00

0 Answers0