From 4119bdcc251011e8e881ff3df5a6ae2a861b3579 Mon Sep 17 00:00:00 2001 From: Saalim Aqueel Date: Sun, 15 Oct 2023 06:29:26 +0530 Subject: [PATCH] implemented Reminders o notify users o complete Task --- package-lock.json | 43 +++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/index.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/package-lock.json b/package-lock.json index 43548e9..521618e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "dotenv": "^16.3.1", "fs": "^0.0.1-security", "mongoose": "^7.6.2", + "node-schedule": "^2.1.1", "pdfkit": "^0.13.0", "xlsx": "^0.18.5" }, @@ -579,6 +580,17 @@ "node": ">=0.8" } }, + "node_modules/cron-parser": { + "version": "4.9.0", + "resolved": "https://registry.npmjs.org/cron-parser/-/cron-parser-4.9.0.tgz", + "integrity": "sha512-p0SaNjrHOnQeR8/VnfGbmg9te2kfyYSQ7Sc/j/6DtPL3JQvKxmjO9TSjNFpujqV3vEYYBvNNvXSxzyksBWAx1Q==", + "dependencies": { + "luxon": "^3.2.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -1603,6 +1615,19 @@ "resolved": "https://registry.npmjs.org/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz", "integrity": "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==" }, + "node_modules/long-timeout": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/long-timeout/-/long-timeout-0.1.1.tgz", + "integrity": "sha512-BFRuQUqc7x2NWxfJBCyUrN8iYUYznzL9JROmRz1gZ6KlOIgmoD+njPVbb+VNn2nGMKggMsK79iUNErillsrx7w==" + }, + "node_modules/luxon": { + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.3.tgz", + "integrity": "sha512-tFWBiv3h7z+T/tDaoxA8rqTxy1CHV6gHS//QdaH4pulbq/JuBSGgQspQQqcgnwdAx6pNI7cmvz5Sv/addzHmUg==", + "engines": { + "node": ">=12" + } + }, "node_modules/magic-bytes.js": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.5.0.tgz", @@ -1731,6 +1756,19 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/node-schedule": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/node-schedule/-/node-schedule-2.1.1.tgz", + "integrity": "sha512-OXdegQq03OmXEjt2hZP33W2YPs/E5BcFQks46+G2gAxs4gHOIVD1u7EqlYLYSKsaIpyKCK9Gbk0ta1/gjRSMRQ==", + "dependencies": { + "cron-parser": "^4.2.0", + "long-timeout": "0.1.1", + "sorted-array-functions": "^1.3.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", @@ -2084,6 +2122,11 @@ "npm": ">= 3.0.0" } }, + "node_modules/sorted-array-functions": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/sorted-array-functions/-/sorted-array-functions-1.3.0.tgz", + "integrity": "sha512-2sqgzeFlid6N4Z2fUQ1cvFmTOLRi/sEDzSQ0OKYchqgoPmQBVyM3959qYx3fpS6Esef80KjmpgPeEr028dP3OA==" + }, "node_modules/sparse-bitfield": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", diff --git a/package.json b/package.json index ceeac92..eb3957e 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dotenv": "^16.3.1", "fs": "^0.0.1-security", "mongoose": "^7.6.2", + "node-schedule": "^2.1.1", "pdfkit": "^0.13.0", "xlsx": "^0.18.5" }, diff --git a/src/index.js b/src/index.js index 4fce41f..f03c677 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ require("dotenv").config(); const { Client, GatewayIntentBits, MessageEmbed } = require("discord.js"); const mongoose = require("mongoose"); const XLSX = require("xlsx"); +const schedule = require("node-schedule"); const PDFDocument = require("pdfkit"); const fs = require("fs"); @@ -234,7 +235,46 @@ db.once("open", () => { message.reply("Your resubmission has been recorded!"); } } + //-------------------------------------- SEND_REMINDERS ----------------------------------// + async function checkDailyProgress() { + const today = new Date(); + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + + // Query the database to find users who haven't submitted for today + try { + const users = await User.find({ + lastSubmissionDate: { $lt: today, $gte: yesterday }, + }); + // Iterate through the users and send reminders + for (const user of users) { + // Send a reminder message to the user + const userId = user.userId; // User's Discord ID + const reminderMessage = "Don't forget to submit your daily progress!"; + + // Replace the following line with your desired method to send the reminder to the user + const discordUser = await client.users.fetch(userId); + discordUser.send(reminderMessage); + } + } catch (err) { + console.error("Error querying the database:", err); + } + } + + // Schedule the reminder function to run at 6:00 PM (18:00) in GMT+5:30 every day + schedule.scheduleJob( + "0 18 * * *", + { tz: "Asia/Kolkata" }, + checkDailyProgress + ); + //============================Manually trigger reminder [Testing]======================== + if ( + message.content === "!testreminder" + ) { + checkDailyProgress(); // Manually trigger the reminder check + message.channel.send("Testing reminders..."); // Send a confirmation message + } //---------------------------------- EXPORT_CHALLENGE_RECORD_SHEET ------------------------// if (message.content === "!export") {