2
votes

I have a problem I almost resolved but i'm now stuck.

I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this: At the top of my code I have var currentdate = new Date();

And then, later in my source code:

if(currentdate.getMinutes() == currentdate.getHours())
{
    bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}

It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.

And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.

Thank you for the help and sorry if my english is bad !

2
You can take a look at the node-cron package to set a task at specific times. Else you could probably do it aswell with a setIntervalT. Dirks
Could you provide more code? Where are you running this snippet? If you are running it inside a on('message') listener, that may answer why your code is not called if a message is not sent.user9016207
@WillHoskings Yeah it's actually inside the on('message') but when I put it outside of it, the bot just do nothing at all.Misubata

2 Answers

3
votes

You need to be checking at some interval whether or not to send a message.

Something like setInterval would work.

setInterval(function(){
    if(currentdate.getMinutes() == currentdate.getHours())
    {
        bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
    }
}, MIN_INTERVAL)


You want MIN_INTERVAL to be the minimum amount of time in milliseconds to check for sending messages.

If you want to check every minute

const MIN_INTERVAL = 1000 * 60

0
votes

You have to use if statement and you didn't specified hour or minutes, so the bot can't send message.


 async function verifyTime() {
    var d = new Date();

    if (d.getHours() == d.getMinutes()) {

           //your code goes here
          
        } catch (error) {

            console.log(error);

        }
        setTimeout(() => {
            verifyTime();
        }, 61 * 1000);
    } else {
        setTimeout(() => {
            verifyTime();
        }, 1000);
    }
 }


client.login(token);

//⬇ remember to place this under your client.login

setTimeout(() => {
 verifyTime();
 }, 5000);