diff --git a/package-lock.json b/package-lock.json index 951087b..2695f37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "googlethis": "^1.7.1", "insta-fetcher": "^1.3.25", "node-cache": "^5.1.2", + "node-cron": "^3.0.3", "node-telegram-bot-api": "^0.61.0", "openai": "^3.3.0", "pg": "^8.7.3", @@ -37,6 +38,7 @@ "@types/adm-zip": "^0.5.0", "@types/express": "^4.17.17", "@types/fluent-ffmpeg": "^2.1.21", + "@types/node-cron": "^3.0.11", "@types/node-telegram-bot-api": "^0.61.6", "@types/pg": "^8.10.1", "@types/string-similarity": "^4.0.0", @@ -632,6 +634,12 @@ "undici-types": "~5.26.4" } }, + "node_modules/@types/node-cron": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz", + "integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==", + "dev": true + }, "node_modules/@types/node-telegram-bot-api": { "version": "0.61.11", "resolved": "https://registry.npmjs.org/@types/node-telegram-bot-api/-/node-telegram-bot-api-0.61.11.tgz", @@ -4392,6 +4400,25 @@ "node": ">= 8.0.0" } }, + "node_modules/node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/node-cron/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/node-getopt": { "version": "0.3.3-forked-by-takamin", "resolved": "git+ssh://git@github.com/takamin/node-getopt.git#a94ecd2791f28b89108a001d5e5f51d632dbe671", @@ -7338,6 +7365,12 @@ "undici-types": "~5.26.4" } }, + "@types/node-cron": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz", + "integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==", + "dev": true + }, "@types/node-telegram-bot-api": { "version": "0.61.11", "resolved": "https://registry.npmjs.org/@types/node-telegram-bot-api/-/node-telegram-bot-api-0.61.11.tgz", @@ -10132,6 +10165,21 @@ "clone": "2.x" } }, + "node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "requires": { + "uuid": "8.3.2" + }, + "dependencies": { + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + } + } + }, "node-getopt": { "version": "git+ssh://git@github.com/takamin/node-getopt.git#a94ecd2791f28b89108a001d5e5f51d632dbe671", "from": "node-getopt@github:takamin/node-getopt" diff --git a/package.json b/package.json index ae0c32e..f1c41ca 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "googlethis": "^1.7.1", "insta-fetcher": "^1.3.25", "node-cache": "^5.1.2", + "node-cron": "^3.0.3", "node-telegram-bot-api": "^0.61.0", "openai": "^3.3.0", "pg": "^8.7.3", @@ -52,6 +53,7 @@ "@types/adm-zip": "^0.5.0", "@types/express": "^4.17.17", "@types/fluent-ffmpeg": "^2.1.21", + "@types/node-cron": "^3.0.11", "@types/node-telegram-bot-api": "^0.61.6", "@types/pg": "^8.10.1", "@types/string-similarity": "^4.0.0", diff --git a/src/bot.ts b/src/bot.ts index ef9b659..25cd477 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -8,6 +8,7 @@ import makeWASocket, { } from "@whiskeysockets/baileys"; import pino from "pino"; +import cron from "node-cron"; /* ----------------------------- add local files ---------------------------- */ import { setAuth, getAuth } from "./db/authDB"; @@ -34,7 +35,7 @@ import { pvxFunctionsEnabled } from "./utils/config"; stats.started = getIndianDateTime().toDateString(); -let dateCheckerInterval: NodeJS.Timeout; +let cronJob: cron.ScheduledTask | undefined; let milestonesDefault: MilestonesDefault = {}; @@ -79,7 +80,7 @@ const startBot = async () => { commandsOwners, allCommandsName, } = await addCommands(); - clearInterval(dateCheckerInterval); + cronJob?.stop(); const { version, isLatest } = await fetchLatestBaileysVersion(); console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`); @@ -106,7 +107,7 @@ const startBot = async () => { store?.bind(bot.ev); if (pvxFunctionsEnabled === "true") { - dateCheckerInterval = await pvxFunctions(bot); + cronJob = await pvxFunctions(bot); } let botNumberJid = bot.user ? bot.user.id : ""; // '1506xxxxx54:3@s.whatsapp.net' diff --git a/src/functions/pvxFunctions.ts b/src/functions/pvxFunctions.ts index 26868d0..660e06b 100644 --- a/src/functions/pvxFunctions.ts +++ b/src/functions/pvxFunctions.ts @@ -1,3 +1,4 @@ +import cron from "node-cron"; import { ownerNumberWithJid } from "../utils/config"; import { pvxgroups } from "../utils/constants"; import { deleteOldNews } from "../db/newsDB"; @@ -7,11 +8,11 @@ import { getIndianDateTime, getOldIndianDateTime } from "./getIndianDateTime"; import postStudyInfo from "./postStudyInfo"; import { postTechNewsHeadline, postTechNewsList } from "./postTechNews"; -const pvxFunctions = async (bot: Bot) => { +const pvxFunctions = async (bot: Bot): Promise => { let usedDate = getIndianDateTime().toDateString(); - return setInterval(async () => { - console.log("SET INTERVAL."); + return cron.schedule("0 */20 * ? * *", async () => { + console.log("Cron 20 min !"); const date = getIndianDateTime(); const todayDate = date.toDateString(); @@ -53,7 +54,7 @@ const pvxFunctions = async (bot: Bot) => { }); } } - }, 1000 * 60 * 30); // 30 min + }); }; export default pvxFunctions;