This commit is contained in:
Daniel Bulant 2021-10-06 16:52:56 +02:00
parent f52ed8c787
commit 4e4d000fc4
4 changed files with 83 additions and 2 deletions

37
commands/ssps/novinky.js Normal file
View file

@ -0,0 +1,37 @@
const commando = require("@iceprod/discord.js-commando");
const { MessageEmbed } = require("discord.js");
const TurndownService = require('turndown');
const api = require("../../utils/api");
const turndownService = new TurndownService({
bulletListMarker: "-",
codeBlockStyle: "fenced"
});
module.exports = class novinky extends commando.Command {
constructor(client) {
super(client, {
name: "novinky",
memberName: "novinky",
group: "ssps",
description: "Zobrazí novinky ze školy",
args: []
});
}
async run(msg) {
const news = await api.getNews();
const embed = new MessageEmbed();
embed.setTitle("Novinky ze školy");
for(const post of news.slice(0, 25)) {
embed.addField(
post.title.rendered.trim(),
turndownService.turndown(post.excerpt.rendered)
);
}
return msg.reply(embed);
}
};

View file

@ -35,7 +35,7 @@ client.registry
["ssps", "Příkazy pro SSPŠ"],
["nastaveni", "Nastavení bota"]
])
.registerDefaults()
.registerDefaultTypes()
.registerCommandsIn(path.join(__dirname, 'commands'));
client.setProvider(

View file

@ -6,6 +6,7 @@
"js-yaml": "^4.1.0",
"node-fetch": "2",
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2"
"sqlite3": "^5.0.2",
"turndown": "^7.1.1"
}
}

View file

@ -95,6 +95,42 @@ class Schedule {
}
}
/**
* @typedef WPPost
* @property {number} id
* @property {string} date ISO date
* @property {string} date_gmt
* @property {{ rendered: string }} guild
* @property {string} modified ISO date
* @property {string} modified_gmt
* @property {string} slug Used in pretty URLs
* @property {string} status Always publish in public API
* @property {"post"} type
* @property {string} link
* @property {{ rendered: string }} title
* @property {{ rendered: string, protected: boolean }} content
* @property {{ rendered: string, protected: boolean }} excerpt
* @property {number} author
* @property {number} featured_media
* @property {"closed" | "open"} comment_status Seems to be always closed
* @property {"closed" | "open"} ping_status Seems to be always on
* @property {boolean} sticky
* @property {string} template
* @property {string} format commonly 'standard'
* @property {[]} meta ?
* @property {number[]} categories
* @property {WPPostLinks} _links
*/
/**
* @typedef WPPostLinks
* @property {{ href: string }[]} self
* @property {{ href: string }[]} collection
* @property {{ href: string }[]} about
* @property {{ href: string, embeddable: boolean }[]} author
* @property {{ href: string, embeddable: boolean }[]} replies
* @property {{ href: string, count: number }[]} version-history
*/
class API {
request = request;
@ -110,6 +146,13 @@ class API {
return new Schedule(res);
}
async getNews() {
/** @type {WPPost[]} */
const res = await request(`wp-json/wp/v2/posts`);
return res;
}
formatRoom(room) {
if(!room) return "";
return room.toString().padStart(3, "0");