I'm trying to make the bot writing messages every day. I installed npm install discord.js and npm install --save node-cron.
const { Client, Intents, GuildMember } = require('discord.js');
var cron = require('node-cron');
const client = new Client({ intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES] });
const TOKEN = '#';
client.once('ready', () => {
console.log("I am ready");
});
let job = new cron.CronJob('30 2 * * *', () => {
client.channels.cache.get('#').send("Hello!!")
})
job.start();
client.login(TOKEN)
And terminal says
"let job = new cron.CronJob('30 2 * * *', () => {
^
TypeError: cron.CronJob is not a constructor"
As @Zsolt Meszaros has mentioned, you're mixing cron and node-cron. If you want to use node-cron, your scheduling should look something like this:
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
Applied into your code it would look something like this:
const { Client, Intents, GuildMember } = require("discord.js");
const cron = require("node-cron");
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const TOKEN = "#";
client.once("ready", () => {
console.log("I am ready");
});
let job = cron.schedule("30 2 * * *", () => {
client.channels.cache.get("#").send("Hello!!");
});
client.login(TOKEN);
NOTE
You do not need to use task.start() unless you provide the scheduled: false parameter into the task.