refactor split board update and notifications to their modules
This commit is contained in:
parent
20160611fd
commit
ba1479acbe
5 changed files with 141 additions and 123 deletions
27
src/lib/board.ts
Normal file
27
src/lib/board.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { Client, EmbedBuilder } from "npm:discord.js";
|
||||
|
||||
import { fetch_feed_channel, format_devoir_title } from "./lib.ts";
|
||||
import { Storage } from "./storage.ts";
|
||||
import { Channel, collect, log_from } from "./utils.ts";
|
||||
|
||||
const log = log_from(import.meta);
|
||||
|
||||
export async function update_loop(bot: Client, storage: Storage, update_display: Channel) {
|
||||
log("Waiting for updates.");
|
||||
while (true) {
|
||||
const _trigger = await update_display.receive();
|
||||
log("Updating board.");
|
||||
for await (const [_, feed] of storage.feeds.list()) {
|
||||
const feed_channel = await fetch_feed_channel(bot, feed);
|
||||
const embed = new EmbedBuilder().setTitle("`📚` Devoirs")
|
||||
.setFooter({ text: "Mise à jour " + Date.now() });
|
||||
const sorted_devoirs = (await (collect(storage.devoirs.list())))
|
||||
.map(([_, d]) => d).toSorted((a, b) => a.date > b.date ? 1 : -1);
|
||||
for (const devoir of sorted_devoirs) {
|
||||
embed.addFields({ name: format_devoir_title(devoir), value: devoir.description });
|
||||
}
|
||||
const board_message = await feed_channel.messages.fetch(feed.board_message_id);
|
||||
board_message.edit({ embeds: [embed], content: "" });
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue