From 4e4d000fc496656665e5438255ff622261724220 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Wed, 6 Oct 2021 16:52:56 +0200 Subject: [PATCH] novinky --- commands/ssps/novinky.js | 37 ++++++++++++++++++++++++++++++++++ index.js | 2 +- package.json | 3 ++- utils/api.js | 43 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 commands/ssps/novinky.js diff --git a/commands/ssps/novinky.js b/commands/ssps/novinky.js new file mode 100644 index 0000000..a647a6d --- /dev/null +++ b/commands/ssps/novinky.js @@ -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); + } +}; \ No newline at end of file diff --git a/index.js b/index.js index a4fa901..69cb5b1 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ client.registry ["ssps", "Příkazy pro SSPŠ"], ["nastaveni", "Nastavení bota"] ]) - .registerDefaults() + .registerDefaultTypes() .registerCommandsIn(path.join(__dirname, 'commands')); client.setProvider( diff --git a/package.json b/package.json index b66cdf4..214b1b1 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/utils/api.js b/utils/api.js index 2fc453d..db7229c 100644 --- a/utils/api.js +++ b/utils/api.js @@ -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");