diff --git a/commands/ssps/rozvrh.js b/commands/ssps/rozvrh.js index aeb29a0..02f6ec5 100644 --- a/commands/ssps/rozvrh.js +++ b/commands/ssps/rozvrh.js @@ -1,14 +1,8 @@ const commando = require("@iceprod/discord.js-commando"); const { MessageEmbed } = require("discord.js"); +const { DateTime } = require("luxon"); const api = require("../../utils/api"); -/** - * @typedef {T extends PromiseLike ? U : T} Depromise - * @template T - */ -/** @type {Record>>} */ -var cache = {}; - module.exports = class rozvrh extends commando.Command { constructor(client) { super(client, { @@ -31,17 +25,16 @@ module.exports = class rozvrh extends commando.Command { className = className.replace(".", "").toUpperCase(); className = api.map[className]; if(!className) return msg.reply("Třída není podporovaná."); - if(!cache[className]) { - cache[className] = await api.getSchedule(className); - } + const sc = await api.getSchedule(className); - const date = new Date; - const dayOfWeek = (date.getDay() > 1 && date.getDay() < 6 ? date.getDay() : 1) - 1; - const schedule = cache[className].schedule[dayOfWeek]; + const date = DateTime.now(); + if(date.hour > 16) date.plus({ days: 1 }); // show tomorrow supplementations after 4PM + const dayOfWeek = (date.weekday > 1 && date.weekday < 6 ? date.weekday : 1) - 1; + const schedule = sc.schedule[dayOfWeek]; const embed = new MessageEmbed(); embed.setTitle("Rozvrh"); - embed.setDescription("Rozvrh pro třídu " + api.demap[className]); + embed.setDescription(`Rozvrh pro třídu ${api.demap[className]} pro ${date.toFormat("cccc")}`); for(let cellI in schedule) { cellI = parseInt(cellI); diff --git a/commands/ssps/suplovani.js b/commands/ssps/suplovani.js index a4f9dca..c5034d4 100644 --- a/commands/ssps/suplovani.js +++ b/commands/ssps/suplovani.js @@ -1,7 +1,9 @@ const commando = require("@iceprod/discord.js-commando"); const { MessageEmbed } = require("discord.js"); +const { DateTime } = require("luxon"); const api = require("../../utils/api"); + module.exports = class suplovani extends commando.Command { constructor(client) { super(client, { @@ -14,11 +16,12 @@ module.exports = class suplovani extends commando.Command { } async run(msg) { - const date = new Date; + const date = DateTime.now(); + if(date.hour > 16) date.plus({ days: 1 }); // show tomorrow supplementations after 4PM const supplementations = await api.getSupplementations(date); const embed = new MessageEmbed(); - embed.setTitle(`Suplování pro den ${date.getDate()}. ${date.getMonth() + 1}.`); + embed.setTitle(`Suplování pro den ${date.day}. ${date.month}.`); for(const change of supplementations.data.ChangesForClasses) { const changes = new Map; diff --git a/index.js b/index.js index 69cb5b1..d86fd63 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,21 @@ const sqlite3 = require("sqlite3"); const yaml = require("js-yaml"); const fs = require("fs-extra"); const path = require("path"); +const luxon = require("luxon"); + +luxon.Settings.defaultLocale = "cs"; const config = yaml.load(fs.readFileSync("./config.yml", { encoding: "utf-8" })); const client = new Commando.Client({ owner: '820696421912412191', - commandPrefix: "ssps!" + commandPrefix: "ssps!", + presence: { + activity: { + type: "WATCHING", + name: "Prestiž" + } + } }); client.on("commandError", (c, e) => { diff --git a/package.json b/package.json index 214b1b1..db20530 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "discord.js": "12", "fs-extra": "^10.0.0", "js-yaml": "^4.1.0", + "luxon": "^2.0.2", "node-fetch": "2", "sqlite": "^4.0.23", "sqlite3": "^5.0.2", diff --git a/utils/api.js b/utils/api.js index db7229c..953c2fe 100644 --- a/utils/api.js +++ b/utils/api.js @@ -1,3 +1,4 @@ +const { DateTime } = require("luxon"); const fetch = require("node-fetch"); async function request(endpoint, body) { @@ -134,8 +135,8 @@ class Schedule { class API { request = request; - async getSupplementations(date = new Date) { - const res = await request(`wp-content/themes/ssps-wordpress-theme/supplementation.php/?date=${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, "0")}${date.getDate().toString().padStart(2, "0")}`); + async getSupplementations(date = new DateTime) { + const res = await request(`wp-content/themes/ssps-wordpress-theme/supplementation.php/?date=${date.toFormat("yyyyMMdd")}`); return new Supplementations(res); }