ruche-manager/src/bot.ts
2024-05-04 09:56:37 +02:00

52 lines
1.7 KiB
TypeScript
Executable file

#!/bin/env -S deno run -A --unstable-kv
import * as fs from "node:fs";
import { Client } from "npm:discord.js";
import { Storage } from "./lib/storage.ts";
import { channel, log_from } from "./lib/utils.ts";
import { update_loop } from "./lib/board.ts";
import { notification_loop } from "./lib/notification.ts";
import { declare_commands, handle_autocomplete, handle_command } from "./lib/commands/commands.ts";
const log = log_from(import.meta);
const subjects = new Set(fs.readFileSync('/modules.conf', 'utf-8').split('\n'));
async function main() {
const debug_server = Deno.env.get("DEBUG");
if (debug_server !== undefined) log("Starting in debug mode on server", debug_server);
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 update_display = channel();
bot.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand()) return await handle_command(interaction, storage, update_display);
if (interaction.isAutocomplete()) return await handle_autocomplete(interaction, storage);
});
log("Logging in ...");
await bot.login(token);
log("Logged in as", bot.user?.username);
const commands = declare_commands(subjects);
if (debug_server !== undefined) {
for (const command of commands) command.setName("dbg-" + command.name);
await bot.application?.commands.set(commands, debug_server);
} else {
await bot.application?.commands.set(commands);
}
log("Registered", commands.length, "commands.");
update_loop(bot, storage, update_display);
update_display.send();
notification_loop(bot, storage);
log("Oki.");
}
if (import.meta.main) await main();