From c5ff1b74da585defb32b3149f3fb3dfbc48615a1 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Sat, 10 Oct 2020 11:14:30 +0200 Subject: [PATCH] :sparkles: Add marks notifications --- index.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/index.js b/index.js index da56359..6e171d6 100644 --- a/index.js +++ b/index.js @@ -64,11 +64,45 @@ const pronoteSynchronization = async () => { homeworks }); + const marks = await session.marks("trimester"); + const subjectsNewMarks = marks.subjects.filter((subj) => cache.marks.subjects && cache.marks.subjects.find((s) => s.name === subj.name) && cache.marks.subjects.find((s) => s.name === subj.name).averages.student !== subj.averages.student); + if (subjectsNewMarks.length > 0 && subjectsNewMarks.length <= 3) { + subjectsNewMarks.forEach((subj) => { + const marks = subj.marks.filter((mark) => !(cache.marks.subjects.find((s) => s.name === subj.name).marks.some((cacheMark) => cacheMark.id === mark.id))); + marks.forEach((mark) => sendDiscordNotificationMark(subj, mark)); + }); + } + // Mise à jour du cache pour les notes + writeCache({ + ...cache, + marks + }); + // Déconnexion de Pronote session.logout(); }; +/** + * Envoi un notification de note sur Discord + * @param {string} subject La matière de la note + * @param {pronote.Mark} mark La note à envoyer + */ +const sendDiscordNotificationMark = (subject, mark) => { + const infos = `Moyenne de la classe : ${mark.average}/${mark.scale}`; + const description = mark.title ? `${mark.title}\n\n${infos}` : infos; + const embed = new Discord.MessageEmbed() + .setTitle(subject.name.toUpperCase()) + .setDescription(description) + .setFooter(`Date de l'évaluation : ${moment(mark.date).format("dddd Do MMMM")}`) + .setURL(process.env.PRONOTE_URL) + .setColor("#70C7A4"); + + client.channels.cache.get(process.env.MARKS_CHANNEL_ID).send(embed).then((e) => { + e.react("✅"); + }); +}; + /** * Envoi une notification de devoir sur Discord * @param {pronote.Homework} homework Le devoir à envoyer