diff --git a/README.md b/README.md new file mode 100644 index 0000000..08caddf --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Ruche manager + +![ruche-manager profile pic](./data/profile.png) + +Mange la ruche. + +--- + +## Description + +Bot gestionnaire de devoirs, spécialisé pour la majeure SSIE. + +## Usage + +### Dépendance + +- deno + installer via votre package manager : + - windows : `winget install deno` + - archlinux : `sudo pacman -S deno` + - debian : `sudo apt install deno` + - autre : téléchargeable ici : https://deno.land/ + +### Exécution + +1. Token + - Récupérer un token pour Bot discord, d'une application ayant les permissions d'exécuter des interractions et écrire des messages. + - Écrire le token dans un fichier `./token` + +2. Lancer + - `./src/bot.ts ./token` diff --git a/src/bot.ts b/src/bot.ts index a617bc4..fddc05d 100755 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,5 +1,8 @@ #!/bin/env -S deno run -A --unstable-kv +import { assertExists } from "https://deno.land/std@0.223.0/assert/assert_exists.ts"; +import { assert } from "https://deno.land/std@0.223.0/assert/assert.ts"; + import { AutocompleteInteraction, ChatInputCommandInteraction, @@ -25,14 +28,14 @@ const subjects = new Set([ "Sûreté", ]); +const feed_channel_id = "871779571064778784"; + async function main() { - const [token] = Deno.args; + const [token_file] = Deno.args; + const token = (await Deno.readTextFile(token_file)).trim(); const storage = await Storage.open("./local/db"); const bot = new Client({ intents: ["GuildMessages", "Guilds"] }); - const feed_channel = await bot.channels.fetch("871779571064778784"); - assertExists(feed_channel); - assert(feed_channel instanceof TextChannel); const update_display = channel(); bot.on("interactionCreate", async (interaction) => { @@ -43,6 +46,11 @@ async function main() { log("Logging in ..."); await bot.login(token); log("Logged in as", bot.user?.username); + + const feed_channel = await bot.channels.fetch(feed_channel_id); + assertExists(feed_channel); + assert(feed_channel instanceof TextChannel); + const commands = build_api_commands(subjects); await bot.application?.commands.set([]); await bot.application?.commands.set(commands, "871777993922588712"); @@ -197,9 +205,6 @@ async function handle_autocomplete(interaction: AutocompleteInteraction, storage log("Unknown command", interaction.commandName); } -import { assertExists } from "https://deno.land/std@0.223.0/assert/assert_exists.ts"; -import { assert } from "https://deno.land/std@0.223.0/assert/assert.ts"; - async function update_loop(bot: Client, storage: Storage, update_display: Channel, feed_channel: TextChannel) { log("Waiting for updates."); while (true) { @@ -220,20 +225,18 @@ async function update_loop(bot: Client, storage: Storage, update_display: Channe const _1min = 60 * 1000; const _24h = 24 * 60 * _1min; +const _3d = 3 * _24h; async function notification_loop(bot: Client, storage: Storage, feed_channel: TextChannel) { while (true) { const devoirs_to_notify = new Set(); - const notification_threshold = _24h; + const notification_threshold = _3d; for await (const [devoir_id, devoir] of storage.devoirs.list()) { if (Date.now() - devoir.date < notification_threshold) devoirs_to_notify.add(devoir_id); } for await (const [notification_id, notification] of storage.notifications.list()) { if (!devoirs_to_notify.has(notification.devoir_id)) { - const channel = await bot.channels.fetch("e"); - assert(channel !== null); - assert(channel.isTextBased()); try { const message = await feed_channel.messages.fetch(notification.message_id); await message.delete();