I'm new to discord js and trying to make my bot send a message at a specific time (set via timestamp).
client.on('ready', function(e)
{
console.log(`Logged in as ${client.user.tag}!`)
let scheduledMessage = new cron.CronJob('0 10 15 * * *', () => {
const guild = client.guilds.cache.get('ID');
const channel = client.channels.cache.get('ID');
const exampleEmbed = new MessageEmbed()
.setColor('#ebdc05')
.setTitle('Another ten minutes')
channel.send({ embeds: [exampleEmbed] });
});
scheduledMessage.start()
});
I found this method that sends a message at 15:10:00. My problem is that I would need the bot to send the message every 31 hours.
I also found the method to write '0 0 */31 * * *' instead of '0 10 15 * * *'. And that works, every 31 hours a message would be sent, but I'm afraid my bot sometimes could send the message a bit after the 31-hour mark and just add up seconds and minutes after time, that at some point it sends the message at like 15:12:00 or sth.
So I came up with the idea to just make a timestamp, and add the time to always have the 31 hours, then let the bot send the message at the exact timestamp. Sadly, I found nothing to that problem.
Is that even possible?
Edit: The timestamp variant would be useful because I can subtract and add time to that very easily and also show it in an embed.
The point of the cron library you're using is that you can explicitly tell it the minutes and seconds—in your case, 0.
It won't accumulate errors. Unless it's very broken, the cron library will do the "figure out the next timestamp" stuff for you.
The cron string 0 0 */31 * * * means "Every 31 hours, when the time looks like XX:00:00". So it will never run at 15:12:00—or 15:10:00 for that matter. You've said "run at this exact minute and second", so it will.
Disclaimer: I tried to read the code of https://github.com/kelektiv/node-cron to confirm this myself, but it's pretty convoluted. https://github.com/node-cron/node-cron/tree/master/src was easier to read and it definitely is checking the timestamp every second.