From 14ae923f3eaf0be999a01f78dc9419bbee75dc36 Mon Sep 17 00:00:00 2001 From: as6325400 Date: Mon, 5 Feb 2024 03:47:34 +0800 Subject: [PATCH] add discord bot --- file/User.json | 2 +- src/index.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/file/User.json b/file/User.json index 32b5040..fd3a630 100644 --- a/file/User.json +++ b/file/User.json @@ -1,3 +1,3 @@ { - "USERID_SET": ["223796"] + "USERID_SET": ["223796", "223798", "151204"] } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index ff10a61..980cf15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Client } from "discord.js"; +import { Client, TextChannel } from "discord.js"; import { IntentOptions } from "./config/IntentOptions"; import * as dotenv from "dotenv"; import { Query } from "./query"; @@ -13,9 +13,13 @@ dotenv.config(); (async () => { const BOT = new Client({intents: IntentOptions}); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + let channel : TextChannel | undefined; + await BOT.login(process.env.BOT_TOKEN); BOT.on("ready", () => { console.log("Bot is ready!"); + channel = BOT.channels.cache.get(process.env.BOT_CHANNEL_ID!) as TextChannel; }); const query : Query = await Query.init(); @@ -26,16 +30,19 @@ dotenv.config(); for (const user of userQueue) { const newProblemSets = await getAcceptSet(user.Id); if (!user.equalSets(newProblemSets)) { - console.log("User " + user.Name + " has new accepted problems!"); + channel?.send(`User ${user.Name} has new accepted problems!`); const diff = user.diffSets(newProblemSets); for (const problem of diff) { - console.log("New problem: " + query.getProblem(problem)!.title); + const newProblem = query.getProblem(problem); + channel?.send( + `New problem: ${newProblem!.title}\n` + + `Tags: ${newProblem!.tags}\n` + + `URL: ${process.env.CSES_URL}${newProblem!.url}` + ); } user.updateAccept(diff); } } await new Promise(resolve => setTimeout(resolve, 1000 * 5)); } - - })(); \ No newline at end of file