mirror of
https://github.com/danbulant/ssps-bot
synced 2026-07-06 03:20:47 +00:00
novinky
This commit is contained in:
parent
f52ed8c787
commit
4e4d000fc4
4 changed files with 83 additions and 2 deletions
37
commands/ssps/novinky.js
Normal file
37
commands/ssps/novinky.js
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
2
index.js
2
index.js
|
|
@ -35,7 +35,7 @@ client.registry
|
||||||
["ssps", "Příkazy pro SSPŠ"],
|
["ssps", "Příkazy pro SSPŠ"],
|
||||||
["nastaveni", "Nastavení bota"]
|
["nastaveni", "Nastavení bota"]
|
||||||
])
|
])
|
||||||
.registerDefaults()
|
.registerDefaultTypes()
|
||||||
.registerCommandsIn(path.join(__dirname, 'commands'));
|
.registerCommandsIn(path.join(__dirname, 'commands'));
|
||||||
|
|
||||||
client.setProvider(
|
client.setProvider(
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
"node-fetch": "2",
|
"node-fetch": "2",
|
||||||
"sqlite": "^4.0.23",
|
"sqlite": "^4.0.23",
|
||||||
"sqlite3": "^5.0.2"
|
"sqlite3": "^5.0.2",
|
||||||
|
"turndown": "^7.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
43
utils/api.js
43
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 {
|
class API {
|
||||||
request = request;
|
request = request;
|
||||||
|
|
||||||
|
|
@ -110,6 +146,13 @@ class API {
|
||||||
return new Schedule(res);
|
return new Schedule(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getNews() {
|
||||||
|
/** @type {WPPost[]} */
|
||||||
|
const res = await request(`wp-json/wp/v2/posts`);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
formatRoom(room) {
|
formatRoom(room) {
|
||||||
if(!room) return "";
|
if(!room) return "";
|
||||||
return room.toString().padStart(3, "0");
|
return room.toString().padStart(3, "0");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue