类型错误:cron.CronJob不是构造函数



我正努力让机器人每天都在写消息。我安装了npm install discord.js和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)

终端显示

"let job = new cron.CronJob('30 2 * * *', () => {
^
TypeError: cron.CronJob is not a constructor"

正如@Zsolt Meszaros所提到的,您正在混合cronnode-cron。如果你想使用node-cron,你的日程安排应该是这样的:

cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});

应用到你的代码中,它看起来像这样:

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);

注意

除非在任务中提供scheduled: false参数,否则不需要使用task.start()

最新更新