最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Make my discord bot send message at specific hours - Stack Overflow

matteradmin7PV0评论

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 !

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 !

Share Improve this question edited Feb 17, 2019 at 18:05 Misubata asked Feb 17, 2019 at 17:39 MisubataMisubata 412 silver badges6 bronze badges 3
  • 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 setInterval – T. Dirks Commented Feb 17, 2019 at 18:09
  • 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 Commented Feb 17, 2019 at 18:09
  • @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 Commented Feb 17, 2019 at 18:15
Add a ment  | 

2 Answers 2

Reset to default 3

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

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);
Post a comment

comment list (0)

  1. No comments so far