forked from Lea/UntisBot
88 lines
3.4 KiB
TypeScript
88 lines
3.4 KiB
TypeScript
import Discord from "discord.js";
|
||
import { Lesson, ShortData } from "webuntis";
|
||
import main from "../index";
|
||
const { defaultEmbedColor, untis, db, sendEmbed } = main;
|
||
function addProp(prop: ShortData) {
|
||
let str = "";
|
||
if ((!prop.name && !prop.longname) || prop.name == "---") {
|
||
str += "None";
|
||
} else if (prop.longname) {
|
||
str += `${prop.longname} `;
|
||
if (prop.name && prop.name != prop.longname) str += `(${prop.name}) `;
|
||
} else {
|
||
str += prop.name;
|
||
}
|
||
return str;
|
||
}
|
||
|
||
|
||
export async function run() {
|
||
const timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
|
||
|
||
const sentClasses = {};
|
||
|
||
timetable.forEach(lesson => {
|
||
const kLesson = db.knownLessons.get(`${lesson.id}`);
|
||
if (kLesson && hasChanged(lesson, kLesson)) {
|
||
if (sentClasses["" + (lesson.sg || lesson.su?.[0]?.id) + " -- " + lesson.date]) return;
|
||
const dateInfo = {
|
||
year: Number(`${lesson.date}`.substr(0, 4)),
|
||
month: Number(`${lesson.date}`.substr(4, 2)),
|
||
day: Number(`${lesson.date}`.substr(6, 2)),
|
||
};
|
||
const date = new Date(dateInfo.year, dateInfo.month - 1, dateInfo.day);
|
||
|
||
const weekDay = ["Sunday", "Monday", "Thursday", "Wednesday", "Thursday", "Friday", "Saturday"][date.getDay()];
|
||
|
||
const embed = new Discord.MessageEmbed()
|
||
.setTitle("Lesson updated")
|
||
.setColor(defaultEmbedColor)
|
||
.setAuthor(`${weekDay}, ${date.getDate()}. ${date.getMonth() + 1}.: ${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"]) {
|
||
const lessonStr = JSON.stringify(lesson[prop]);
|
||
const kLessonStr = JSON.stringify(kLesson[prop]);
|
||
|
||
|
||
if (lessonStr != kLessonStr) {
|
||
desc += `**${propFullName[prop]}**: ${(addProp(kLesson[prop][0]) || "None")} → ${(addProp(lesson[prop][0] || "None"))}\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}`;
|
||
}
|
||
|
||
// if (lesson.code) embed.setColor(lesson.code == 'irregular' ? 'A781B4' : 'B1B3B4');
|
||
// Change the embed color when teacher ID is 0.
|
||
// Teacher ID 0 means that the class is canelled (at least on my school),
|
||
// although I don't know if this is always the case.
|
||
if (lesson.code == "irregular" || lesson.te[0].id == 0) embed.setColor(0xA781B4);
|
||
|
||
embed.setDescription(desc);
|
||
sentClasses[`${lesson.sg || lesson.su?.[ 0 ]?.id} -- ${lesson.date}`] = true;
|
||
sendEmbed(embed);
|
||
console.log("Sent timetable update");
|
||
}
|
||
db.knownLessons.set(`${lesson.id}`, lesson);
|
||
});
|
||
}
|
||
|
||
function hasChanged(lesson1: Lesson, lesson2: Lesson) {
|
||
return (JSON.stringify(lesson1) != JSON.stringify(lesson2));
|
||
} |