|
| 1 | +import { EntityRepository } from '@mikro-orm/core'; |
| 2 | +import dayjs from 'dayjs'; |
| 3 | +import { CacheType, ChatInputCommandInteraction } from 'discord.js'; |
| 4 | + |
| 5 | +import { ErrorReply } from '@susc/common/reply'; |
| 6 | +import { BaseDiscordCommandHandler } from '@susc/discord/base-discord-command-handler'; |
| 7 | +import { GetRepository } from '@susc/discord/get-repository.decorator'; |
| 8 | +import { ConfirmHistoryEntity } from '@susc/entities/confirm-history.entity'; |
| 9 | +import { UserEntity } from '@susc/entities/user.entity'; |
| 10 | + |
| 11 | +export class MyHistoryCommandHandler extends BaseDiscordCommandHandler { |
| 12 | + @GetRepository(UserEntity) |
| 13 | + private readonly userRepository!: EntityRepository<UserEntity>; |
| 14 | + |
| 15 | + @GetRepository(ConfirmHistoryEntity) |
| 16 | + private readonly confirmHistoryRepository!: EntityRepository<ConfirmHistoryEntity>; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + super('인증내역'); |
| 20 | + } |
| 21 | + |
| 22 | + async handle( |
| 23 | + interaction: ChatInputCommandInteraction<CacheType>, |
| 24 | + ): Promise<void> { |
| 25 | + const user = await this.userRepository.findOne({ |
| 26 | + discordId: interaction.user.id, |
| 27 | + }); |
| 28 | + |
| 29 | + if (!user) { |
| 30 | + await interaction.reply( |
| 31 | + ErrorReply( |
| 32 | + '아직 깃허브 계정을 등록하지 않았어요. **/등록** 명령어를 사용해서 깃허브 계정을 등록해주세요!', |
| 33 | + ), |
| 34 | + ); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const [confirmHistories, count] = |
| 39 | + await this.confirmHistoryRepository.findAndCount( |
| 40 | + { userId: user.id }, |
| 41 | + { limit: 10, orderBy: { createdAt: 'desc' } }, |
| 42 | + ); |
| 43 | + |
| 44 | + if (confirmHistories.length === 0) { |
| 45 | + await interaction.reply( |
| 46 | + ErrorReply( |
| 47 | + '아직 인증한 내역이 없어요.', |
| 48 | + '📜 아직 인증한 내역이 없어요!', |
| 49 | + ), |
| 50 | + ); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const confirmHistoryList = confirmHistories |
| 55 | + .map((confirmHistory) => { |
| 56 | + const date = dayjs(confirmHistory.createdAt) |
| 57 | + .tz('Asia/Seoul') |
| 58 | + .format('YYYY년 MM월 DD일 HH시 mm분'); |
| 59 | + return `- ${date}`; |
| 60 | + }) |
| 61 | + .join('\n'); |
| 62 | + |
| 63 | + const description = [ |
| 64 | + `📌 총 인증 횟수: **${count}회**`, |
| 65 | + confirmHistoryList, |
| 66 | + ].join('\n\n'); |
| 67 | + |
| 68 | + await interaction.reply({ |
| 69 | + embeds: [{ color: 0x57ad68, title: '📜 내 인증 내역', description }], |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
0 commit comments