From a18caa59395eb9b97af7cd0c7ee8d91b860c6e75 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Mon, 15 Feb 2021 22:15:25 +0100 Subject: [PATCH] :sparkles: Send notifications when lessons is cancelled --- .env.example | 1 + README.md | 2 +- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- package.json | 2 +- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index a7dfa89..0180ecb 100644 --- a/.env.example +++ b/.env.example @@ -7,3 +7,4 @@ TOKEN="" HOMEWORKS_CHANNEL_ID="763786100598505512" MARKS_CHANNEL_ID="764405715707756566" +AWAY_CHANNEL_ID="795225387822874674" diff --git a/README.md b/README.md index 1a992cc..57dcd20 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Pronote Discord Bot -Un bot Discord très simple qui envoie des notifications dans un salon sur Discord lorsqu'un devoir ou une note est ajouté sur Pronote ! 📚 +Un bot Discord très simple qui envoie des notifications dans un salon sur Discord lorsqu'un devoir ou une note est ajouté sur Pronote, ou lorsqu'un enseignant est absent ! 📚 Si vous êtes plus à l'aise avec Python vous pouvez également utiliser le bot de **[busybox11](https://github.com/busybox11/probote)**, qui sera surement compatible avec la dernière version de Pronote bientôt ! 💫 diff --git a/index.js b/index.js index 337532a..992a884 100644 --- a/index.js +++ b/index.js @@ -26,9 +26,10 @@ const writeCache = (newCache) => { */ const resetCache = () => writeCache({ homeworks: [], - marks: [{ + marks: { subjects: [] - }] + }, + lessonsAway: [] }); // Si le fichier cache n'existe pas, on le créé @@ -38,8 +39,7 @@ if (!fs.existsSync("cache.json")) { // S'il existe, on essaie de le parser et si ça échoue on le reset pour éviter les erreurs try { cache = JSON.parse(fs.readFileSync("cache.json", "utf-8")); - } catch (e) { - console.error(e); + } catch { resetCache(); } } @@ -87,6 +87,32 @@ const pronoteSynchronization = async () => { marks }); + const nextWeekDay = new Date(); + nextWeekDay.setDate(nextWeekDay.getDate() + 30); + const timetable = await session.timetable(new Date(), nextWeekDay); + const awayNotifications = []; + timetable.filter((lesson) => lesson.isAway).forEach((lesson) => { + if (!cache.lessonsAway.some((lessonID) => lessonID === lesson.id)){ + awayNotifications.push({ + teacher: lesson.teacher, + from: lesson.from, + subject: lesson.subject, + id: lesson.id + }); + } + }); + if (awayNotifications.length) { + awayNotifications.forEach((awayNotif) => sendDiscordNotificationAway(awayNotif)); + } + + writeCache({ + ...cache, + lessonsAway: [ + ...cache.lessonsAway, + ...awayNotifications.map((n) => n.id) + ] + }); + // Déconnexion de Pronote session.logout(); @@ -135,6 +161,23 @@ const sendDiscordNotificationHomework = (homework) => { }); }; +/** + * Envoi une notification de cours annulé sur Discord + * @param {any} awayNotif Les informations sur le cours annulé + */ +const sendDiscordNotificationAway = (awayNotif) => { + const embed = new Discord.MessageEmbed() + .setTitle(awayNotif.subject.toUpperCase()) + .setDescription(`${awayNotif.teacher} sera absent le ${moment(awayNotif.from).format("dddd Do MMMM")}`) + .setFooter(`Cours annulé de ${awayNotif.subject}`) + .setURL(process.env.PRONOTE_URL) + .setColor("#70C7A4"); + + client.channels.cache.get(process.env.AWAY_CHANNEL_ID).send(embed).then((e) => { + e.react("✅"); + }); +}; + client.on("ready", () => { console.log(`Ready. Logged as ${client.user.tag}!`); client.user.setActivity("Pronote", { diff --git a/package.json b/package.json index 33e2058..0f23bbd 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "discord.js": "^12.0.2", "dotenv": "^8.2.0", "moment": "^2.29.1", - "pronote-api": "Litarvan/pronote-api" + "pronote-api": "^2.3.2" }, "devDependencies": { "eslint": "^7.10.0"