Magic code that makes it work better

This commit is contained in:
Jan 2020-12-14 17:46:48 +01:00
parent 61da37f19f
commit 4b31c83320
2 changed files with 33 additions and 10 deletions

View file

@ -6,9 +6,12 @@ const { bot, defaultEmbedColor, untis, db, sendEmbed } = main;
export async function run() { export async function run() {
let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true); let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
let sentClasses = {};
timetable.forEach(lesson => { timetable.forEach(lesson => {
let kLesson = db.knownLessons.get(`${lesson.id}`); let kLesson = db.knownLessons.get(`${lesson.id}`);
if (kLesson && hasChanged(lesson, kLesson)) { if (kLesson && hasChanged(lesson, kLesson)) {
if (sentClasses['' + (lesson.sg || lesson.su?.[0]?.id) + ' -- ' + lesson.date]) return;
let dateInfo = { let dateInfo = {
year: Number(`${lesson.date}`.substr(0, 4)), year: Number(`${lesson.date}`.substr(0, 4)),
month: Number(`${lesson.date}`.substr(4, 2)), month: Number(`${lesson.date}`.substr(4, 2)),
@ -24,7 +27,7 @@ export async function run() {
let embed = new Discord.MessageEmbed() let embed = new Discord.MessageEmbed()
.setTitle(`Lesson updated`) .setTitle(`Lesson updated`)
.setColor(defaultEmbedColor) .setColor(defaultEmbedColor)
.setAuthor(`${weekDay}: ${lesson.sg || '(Unnamed lesson)'} - ${lesson.te[0].longname || 'No teacher'}`); .setAuthor(`${weekDay}, ${date.getDate()}. ${date.getMonth() + 1}.: ${lesson.sg || '(Unnamed lesson)'} - ${lesson.te[0].longname || 'No teacher'}`);
let desc = ``; let desc = ``;
@ -35,23 +38,43 @@ export async function run() {
ro: 'Room' ro: 'Room'
} }
for (const prop of ['kl', 'te', 'su', 'ro']) { for (const prop of ['kl', 'te', 'su', 'ro']) {
if (lesson[prop] != kLesson[prop]) { let lessonStr = JSON.stringify(lesson[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`; let kLessonStr = JSON.stringify(kLesson[prop]);
let addProp = (prop) => {
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;
}
if (lessonStr != kLessonStr) {
desc += '**' + propFullName[prop] + '**: ' + (addProp(kLesson[prop][0]) || 'None') + ' → ' + (addProp(lesson[prop][0] || 'None')) + '\n';
} }
} }
if (lesson.activityType != kLesson.activityType) { if (lesson.activityType != kLesson.activityType) {
desc += `**Type:** ${kLesson.activityType} \u200b **=>** \u200b ${lesson.activityType} \n` desc += `**Type:** ${kLesson.activityType} \u200b \u200b ${lesson.activityType} \n`
} }
if (lesson.substText) if (lesson.substText)
desc += `\n ${lesson.substText}`; desc += `\n ${lesson.substText}`;
if (lesson.info) if (lesson.info)
desc += `\n ${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('A781B4');
embed.setDescription(desc); embed.setDescription(desc);
console.log(`Sent timetable update`); sentClasses['' + (lesson.sg || lesson.su?.[0]?.id) + ' -- ' + lesson.date] = true;
sendEmbed(embed); sendEmbed(embed);
console.log(`Sent timetable update`);
db.knownLessons.set(`${lesson.id}`, lesson); db.knownLessons.set(`${lesson.id}`, lesson);
} }
else db.knownLessons.set(`${lesson.id}`, lesson); else db.knownLessons.set(`${lesson.id}`, lesson);

View file

@ -10,11 +10,11 @@
// "allowJs": true, /* Allow javascript files to be compiled. */ // "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */ // "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */ "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */ "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */ // "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */ "outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */ // "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */