formátování času

This commit is contained in:
Daniel Bulant 2021-10-06 17:17:43 +02:00
parent 4e4d000fc4
commit dde3e1335e
5 changed files with 26 additions and 19 deletions

View file

@ -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<infer U> ? U : T} Depromise
* @template T
*/
/** @type {Record<string, Depromise<ReturnType<api["getSchedule"]>>>} */
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);

View file

@ -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;

View file

@ -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) => {

View file

@ -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",

View file

@ -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);
}