How to prevent code from excecuting multiple times

63 views Asked by At

This code ends up spamming messages for one second every time it runs. Does anyone know to prevent that? the variable count is the amount of messages sent in the last hour, and my goal is if more then 15 were sent in the past hour then to send a message. Here is the code I tried:

var count = await keyv.get('messagecount')
const sendtochan = await rotate.guild.channels.cache.get('760626595332358205');
cron.schedule('0 0 * * * *', async () => {
    if (count > 14 && !rotate.author.bot) {
        keyv.set('messagecount', 0)
        const sendtochan = await rotate.guild.channels.cache.get('760626595332358205');
        sendtochan.send(`This channel's current topic is: **${await keyv.get('topic')}**`);
        return
    }
    if (count < 14 && !rotate.author.bot) {
        keyv.set('messagecount', 0)
        return
    }
});
0

There are 0 answers