I've been trying to create a Discord bot that posts reminders at 9:00 AM and 9:00 PM everyday from March 22, 2020 to April 27, 2020.
The scheduling process is where I initially got stuck. I took 2 approaches: 1. Doing it myself, using a 12hr long setInterval(), when I did that, i got an error stating that the progam failed to bind to a PORT within 60 seconds.
- Using the node.js cron library https://github.com/kelektiv/node-cron. But in this case the code itself didn't work the way I'd expect
const Discord = require('discord.js');
const cron = require('cron')
const client = new Discord.Client();
const dolChannelID = '<a working channel's id>'
let dolChannel = null
const msgs = {
'morning': 'Morning msg',
'evening': 'Evening msg',
'finale': 'Final msg before ending'
}
const startTime = new Date('March 22, 2020 9:38:00')
const lastDate = new Date('April 27, 2020 21:00:00')
// --------------------------------------------------
client.on('ready', () => {
console.log('we\'re up and running!')
client.channels.fetch(dolChannelID)
.then(channel => {
dolChannel = channel
dolChannel.send('Bot\'s working')
})
.then(() => {
let execJob = new cron.CronJob('50 9,21 * * *', function() {
let rn = new Date()
console.log('time', rn)
// IDEALLY HERE I'D EXPECT TO GET A CONSOLE LOG TELLING ME THE TIME twice a day,
// starting at 9:50 AM, but that didn't happen
// ALTHOUGH, when my cron time expression: '* * * * * *', then things worked somehow
//i.e. I got an console.log every sec.
}, null, true, 'Asia/Kolkata')
execJob.start()
})
.catch((err) => {
console.log('error \n \n', err)
})
})
client.login(<my secret>);
I would appreciate any help. I can't seem to figure out