applied formatting and started refactoring

master
BlackDemonFire 2021-11-03 22:34:43 +01:00
parent c4a9495efd
commit f6099f0fe2
No known key found for this signature in database
GPG Key ID: 0BDEC32ADAA013EE
3 changed files with 106 additions and 103 deletions

View File

@ -1,53 +1,55 @@
import * as dotenv from 'dotenv';
import { codeBlock } from "@discordjs/builders";
import { Client, DMChannel, MessageEmbed } from "discord.js";
import * as dotenv from "dotenv";
import Enmap from "enmap";
import fs from "fs";
import WebUntis, { Lesson } from "webuntis";
dotenv.config();
import Discord from 'discord.js';
import Enmap from 'enmap';
import fs from 'fs';
import * as WebUntis from 'webuntis';
const untis = new WebUntis.default(
const untis = new WebUntis(
process.env.SCHOOLNAME,
process.env.USERNAME,
process.env.PASSWORD,
process.env.BASEURL
process.env.BASEURL,
);
console.log('Discord Bot: Logging in');
const client = new Discord.Client();
console.log("Discord Bot: Logging in");
const client = new Client({ intents: [] });
client.login(process.env.BOT_TOKEN);
client.on('ready', () => console.log(`Discord Bot: Logged in as ${client.user.tag}`));
client.on("ready", () => console.log(`Discord Bot: Logged in as ${client.user.tag}`));
const seenMessages = new Enmap({ name: "seenMessages" });
const knownLessons = new Enmap({ name: "knownLessons" });
const knownLessons: Enmap<string, Lesson> = new Enmap({ name: "knownLessons" });
const defaultEmbedColor = 0xFF9A00;
// Periodically attempt to connect to untis and fetch timetable updates
let fetchUpdates = () => {
console.log('Untis: Logging in');
async function fetchUpdates() {
console.log("Untis: Logging in");
try {
untis.login()
.then(() => {
console.log(`Untis: Logged in`)
await untis.login();
console.log("Untis: Logged in");
fs.readdirSync(`${__dirname}/modules`).filter(file => file.endsWith('.js')).forEach(file => {
require(`${__dirname}/modules/${file}`).run();
});
setTimeout(() => untis.logout().then(() => console.log('Untis: Logged out')), 10000);
fs.readdirSync(`${__dirname}/modules`).filter(file => file.endsWith(".js")).forEach(file => {
require(`${__dirname}/modules/${file}`).run();
});
} catch(e) {
setTimeout(async () => {
await untis.logout();
console.log("Untis: Logged out");
}, 10000);
} catch (e) {
console.error(e);
try {
let embed = new Discord.MessageEmbed()
.setTitle('An error has occurred.')
.setDescription(`\`\`\`js\n${e}\`\`\``)
const embed = new MessageEmbed()
.setTitle("An error has occurred.")
.setDescription(codeBlock("js", `${e}`))
.setColor(0xff0000);
sendEmbed(embed);
} catch(e) {
console.error(e);
} catch (err) {
console.error(err);
}
}
}
@ -56,21 +58,21 @@ fetchUpdates();
setInterval(fetchUpdates, 60000);
async function sendEmbed(embed: Discord.MessageEmbed) {
const channel = await client.channels.fetch(process.env.CHANNEL) as Discord.DMChannel;
if (!channel) throw `ERROR: Could not find channel`;
async function sendEmbed(embed: MessageEmbed) {
const channel = await client.channels.fetch(process.env.CHANNEL) as DMChannel;
if (!channel) throw "ERROR: Could not find channel";
if (!embed.timestamp) embed.setTimestamp();
channel.send(embed).catch(console.error);
channel.send({ embeds: [embed] }).catch(console.error);
}
export default {
untis: untis,
untis,
bot: client,
defaultEmbedColor: defaultEmbedColor,
sendEmbed: sendEmbed,
defaultEmbedColor,
sendEmbed,
db: {
seenMessages: seenMessages,
knownLessons: knownLessons
}
seenMessages,
knownLessons,
},
};

View File

@ -1,22 +1,21 @@
import * as WebUntis from 'webuntis';
import main from '../index';
import Discord from 'discord.js';
const { bot, defaultEmbedColor, untis, db, sendEmbed } = main;
import Discord from "discord.js";
import main from "../index";
const { defaultEmbedColor, untis, db, sendEmbed } = main;
export async function run() {
let news = await untis.getNewsWidget(new Date(), true);
export async function run() {
const news = await untis.getNewsWidget(new Date(), true);
if (!news) return;
news.messagesOfDay.forEach(message => {
if (db.seenMessages.get(`${message.id}`)) return;
console.log('New message found: ' + message.text);
let embed = new Discord.MessageEmbed()
.setAuthor('Notification')
console.log("New message found: " + message.text);
const embed = new Discord.MessageEmbed()
.setAuthor("Notification")
.setColor(defaultEmbedColor)
.setTitle(message.subject)
.setDescription(message.text.replace(/\<br\>/g, "\n"))
.setDescription(message.text.replace(/<br>/g, "\n"))
.setFooter(`Notification ID: ${message.id}`);
sendEmbed(embed);
db.seenMessages.set(`${message.id}`, true);
});

View File

@ -1,86 +1,88 @@
import * as WebUntis from 'webuntis';
import main from '../index';
import Discord from 'discord.js';
const { bot, defaultEmbedColor, untis, db, sendEmbed } = main;
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() {
let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
let sentClasses = {};
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 => {
let kLesson = db.knownLessons.get(`${lesson.id}`);
const kLesson = db.knownLessons.get(`${lesson.id}`);
if (kLesson && hasChanged(lesson, kLesson)) {
if (sentClasses['' + (lesson.sg || lesson.su?.[0]?.id) + ' -- ' + lesson.date]) return;
let dateInfo = {
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))
}
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`)
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 = ``;
.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']) {
let lessonStr = JSON.stringify(lesson[prop]);
let kLessonStr = JSON.stringify(kLesson[prop]);
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]);
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';
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`
desc += `**Type:** ${kLesson.activityType} \u200b → \u200b ${lesson.activityType} \n`;
}
if (lesson.substText)
if (lesson.substText) {
desc += `\n ${lesson.substText}`;
if (lesson.info)
}
if (lesson.info) {
desc += `\n ${lesson.info}`;
//if (lesson.code) embed.setColor(lesson.code == 'irregular' ? 'A781B4' : 'B1B3B4');
}
// 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');
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;
sentClasses[`${lesson.sg || lesson.su?.[ 0 ]?.id} -- ${lesson.date}`] = true;
sendEmbed(embed);
console.log(`Sent timetable update`);
db.knownLessons.set(`${lesson.id}`, lesson);
console.log("Sent timetable update");
}
else db.knownLessons.set(`${lesson.id}`, lesson);
db.knownLessons.set(`${lesson.id}`, lesson);
});
}
function hasChanged(lesson1: WebUntis.Lesson, lesson2: WebUntis.Lesson) {
function hasChanged(lesson1: Lesson, lesson2: Lesson) {
return (JSON.stringify(lesson1) != JSON.stringify(lesson2));
}