UntisBot/src/modules/scanForTimetableChanges.ts

63 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import * as WebUntis from 'webuntis';
import main from '../index';
import Discord from 'discord.js';
const { bot, defaultEmbedColor, untis, db, sendEmbed } = main;
export async function run() {
let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
timetable.forEach(lesson => {
let kLesson = db.knownLessons.get(`${lesson.id}`);
if (kLesson && hasChanged(lesson, kLesson)) {
let dateInfo = {
year: Number(`${lesson.date}`.substr(0, 4)),
month: Number(`${lesson.date}`.substr(4, 2)),
day: Number(`${lesson.date}`.substr(6, 2))
}
let date = new Date()
date.setFullYear(dateInfo.year);
date.setMonth(dateInfo.month - 1);
date.setDate(dateInfo.day);
let weekDay = ['Sunday', 'Monday', 'Thursday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
let embed = new Discord.MessageEmbed()
.setTitle(`Lesson updated`)
.setColor(defaultEmbedColor)
.setAuthor(`${weekDay}: ${lesson.sg || '(Unnamed lesson)'} - ${lesson.te[0].longname || 'No teacher'}`);
let desc = ``;
const propFullName = {
kl: 'Class',
te: 'Teacher',
su: 'Subject',
ro: 'Room'
}
for (const prop of ['kl', 'te', 'su', 'ro']) {
if (lesson[prop] != kLesson[prop]) {
desc += `**${propFullName[prop]}:** ${kLesson[prop]?.[0]?.longname} ${kLesson[prop]?.[0]?.longname != kLesson[prop]?.[0]?.name ? `(${(kLesson[prop]?.[0]?.name)})` : ''} \u200b **=>** \u200b ${lesson[prop]?.[0]?.longname} ${lesson[prop]?.[0]?.longname != lesson[prop]?.[0]?.name ? `(${lesson[prop]?.[0]?.name})` : ''} \n`;
}
}
if (lesson.activityType != kLesson.activityType) {
desc += `**Type:** ${kLesson.activityType} \u200b **=>** \u200b ${lesson.activityType} \n`
}
if (lesson.substText)
desc += `\n ${lesson.substText}`;
if (lesson.info)
desc += `\n ${lesson.info}`
embed.setDescription(desc);
console.log(`Sent timetable update`);
sendEmbed(embed);
db.knownLessons.set(`${lesson.id}`, lesson);
}
else db.knownLessons.set(`${lesson.id}`, lesson);
});
}
function hasChanged(lesson1: WebUntis.Lesson, lesson2: WebUntis.Lesson) {
return (JSON.stringify(lesson1) != JSON.stringify(lesson2));
}