Skip to content

Commit

Permalink
✨ Send notifications when lessons is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Feb 15, 2021
1 parent 034d0d6 commit a18caa5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ TOKEN=""

HOMEWORKS_CHANNEL_ID="763786100598505512"
MARKS_CHANNEL_ID="764405715707756566"
AWAY_CHANNEL_ID="795225387822874674"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 ! 💫

Expand Down
51 changes: 47 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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éé
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a18caa5

Please sign in to comment.