From 2f4c6c2778095578f67d9952addf5833d6642188 Mon Sep 17 00:00:00 2001 From: Roeniss Moon Date: Mon, 2 Sep 2024 00:31:48 +0900 Subject: [PATCH 1/2] feat: Add guild_id to user schema --- src/commands/register.ts | 8 +++++++- src/model/mongodb_user_schema.ts | 4 +++- src/util/mongoUtil.ts | 10 +++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/commands/register.ts b/src/commands/register.ts index b4719e3..3c9159e 100644 --- a/src/commands/register.ts +++ b/src/commands/register.ts @@ -35,8 +35,14 @@ export default { return; } + const guildId = interaction.guildId + if(!guildId){ + await interaction.reply("서버 정보를 가져올 수 없습니다. 명령을 취소합니다.") + return; + } + await interaction.reply({embeds: [getUserInfo(realUser)]}) - const response = await MongoUtil.addUser(interaction.user.id, boj_id!); + const response = await MongoUtil.addUser(interaction.user.id, boj_id!, guildId); if (response) { await interaction.followUp("정상적으로 등록되었습니다.") logger.info(`${interaction.user.id} / ${boj_id} 가입 완료`) diff --git a/src/model/mongodb_user_schema.ts b/src/model/mongodb_user_schema.ts index e50334c..ef35a22 100644 --- a/src/model/mongodb_user_schema.ts +++ b/src/model/mongodb_user_schema.ts @@ -6,12 +6,14 @@ interface IUser extends mongoose.Document { discord_id: string; boj_id: string; daily_time: string; + guild_id: string; } const UserSchema = new mongoose.Schema({ discord_id: {type: String, required: true}, boj_id: {type: String, required: true}, - daily_time: {type: String, required: false} + daily_time: {type: String, required: false}, + guild_id: {type: String, required: false} // not required for backward compatibility }); export const Mongodb_user_schema = mongoose.model('Mongodb_use_schema', UserSchema, 'boj_user'); diff --git a/src/util/mongoUtil.ts b/src/util/mongoUtil.ts index 1e343fd..31dfbed 100644 --- a/src/util/mongoUtil.ts +++ b/src/util/mongoUtil.ts @@ -1,21 +1,21 @@ -import * as mongoose from 'mongoose'; -import { Mongodb_user_schema } from '../model/mongodb_user_schema.js'; +import {Mongodb_user_schema} from '../model/mongodb_user_schema.js'; import {logger} from "../logger.js"; import {Problem} from "../model/problem_schema.js"; import {BojProblem} from "../model/problem_class.js"; -import {ClientSession} from "typeorm"; import {Report} from "../model/report_schema.js"; export class MongoUtil{ //map discordId and bojId, and save it to mongoDB - static async addUser(userDiscordId: string, userBojId: string): Promise{ + static async addUser(userDiscordId: string, userBojId: string, guildId: string): Promise{ try{ const user = new Mongodb_user_schema({ discord_id: userDiscordId, boj_id: userBojId, + daily_time: null, + guild_id: guildId, }); await user.save(); - logger.info(`Adding user ${userDiscordId} with boj id ${userBojId}`); + logger.info(`Adding user ${userDiscordId} with boj id ${userBojId} in guild ${guildId}`); return true; }catch (error: any){ logger.error(error.message); From 24da3ece42bb81ef3d524b97b4bf2090113623eb Mon Sep 17 00:00:00 2001 From: Roeniss Moon Date: Mon, 2 Sep 2024 00:14:41 +0900 Subject: [PATCH 2/2] feat: /list shows all registered user in the server --- src/commands/list.ts | 42 ++++++++++++++++++++++++++++++++ src/embedMessage/guideMessage.ts | 1 + src/util/mongoUtil.ts | 14 +++++++++++ src/util/timeUtil.ts | 12 +++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/commands/list.ts create mode 100644 src/util/timeUtil.ts diff --git a/src/commands/list.ts b/src/commands/list.ts new file mode 100644 index 0000000..b8430c5 --- /dev/null +++ b/src/commands/list.ts @@ -0,0 +1,42 @@ +import {logger} from "../logger.js"; +import {MongoUtil} from "../util/mongoUtil.js"; +import {ChatInputCommandInteraction, SlashCommandBuilder} from "discord.js"; +import {TimeUtil} from "../util/timeUtil.js"; + +function formatDailyTime(dailyTime: string) { + if (dailyTime) { + return TimeUtil.formatDailyTime(dailyTime) + } + return "미등록" +} + +export default { + data: new SlashCommandBuilder() + .setName('list') + .setDescription('등록된 모든 백준 아이디 목록을 보여드립니다.'), + async execute(interaction: ChatInputCommandInteraction) { + try { + const guildId = interaction.guildId + if (!guildId) { + await interaction.reply("서버 정보를 가져올 수 없습니다. 명령을 취소합니다.") + return; + } + + const users = await MongoUtil.findAllUserInGuild(guildId); + if (!users || users.length === 0) { + await interaction.reply(`등록된 유저가 없습니다. \n/quit을 통해 아이디를 등록해주세요.`) + return; + } + + const userInfo = users.map(user => { + return `백준 아이디: ${user.boj_id} | 매일 알림 시간: ${formatDailyTime(user.daily_time)}`; + }).join('\n') + + await interaction.reply(userInfo) + return; + } catch (error: any) { + await interaction.reply("알 수 없는 오류가 발생했습니다.") + logger.error(error.message) + } + } +} diff --git a/src/embedMessage/guideMessage.ts b/src/embedMessage/guideMessage.ts index a47e89c..cb40e24 100644 --- a/src/embedMessage/guideMessage.ts +++ b/src/embedMessage/guideMessage.ts @@ -17,6 +17,7 @@ export const embedWelcome = new EmbedBuilder() { name: '/categorylist', value: '/category 사용 전, 알고리즘 분류 목록표를 보여드립니다.', inline: false }, { name: '/quit', value: '백준 ID를 제거합니다. 봇을 제거할 시 정보 또한 동일하게 제거됩니다.', inline: false }, { name: '/deactivate', value: '일일 문제 알림 수신을 비활성화 합니다.', inline: false }, + { name: '/list', value: '등록된 모든 백준 아이디 목록을 보여드립니다.', inline: false }, { name: '\u200B', value: '\u200B' }, { name: '업데이트 로그는 다음 링크에서 확인해주세요.', value: 'https://github.com/boaz-baekjoon/baekjoon-discord-bot/releases', inline: false }, ) diff --git a/src/util/mongoUtil.ts b/src/util/mongoUtil.ts index 31dfbed..e0f2faf 100644 --- a/src/util/mongoUtil.ts +++ b/src/util/mongoUtil.ts @@ -127,6 +127,20 @@ export class MongoUtil{ } } + static async findAllUserInGuild(guildId: string): Promise{ + try{ + const users = await Mongodb_user_schema.find({guild_id: guildId}); + logger.info(`Finding all users in guild ${guildId}`); + if (users){ + return users; + }else { + return null; + } + }catch (error: any){ + logger.error(error.message); + } + } + //find problem with problemId static async findProblemWithProblemId(problemId: number): Promise{ try { diff --git a/src/util/timeUtil.ts b/src/util/timeUtil.ts new file mode 100644 index 0000000..9f01970 --- /dev/null +++ b/src/util/timeUtil.ts @@ -0,0 +1,12 @@ +export class TimeUtil { + static formatDailyTime(dailyTime: string): string { + let [hh, mm] = dailyTime.split(' ') + if (hh.length === 1) { + hh = `0${hh}` + } + if (mm.length === 1) { + mm = `0${mm}` + } + return `${hh}:${mm}` + } +} \ No newline at end of file