|
| 1 | +import BaseCommand from '#src/core/BaseCommand.js'; |
| 2 | +import Context from '#src/core/CommandContext/Context.js'; |
| 3 | +import { RegisterInteractionHandler } from '#src/decorators/RegisterInteractionHandler.js'; |
| 4 | +import { Pagination } from '#src/modules/Pagination.js'; |
| 5 | +import UserDbService from '#src/services/UserDbService.js'; |
| 6 | +import { CustomID } from '#src/utils/CustomID.js'; |
| 7 | +import db from '#src/utils/Db.js'; |
| 8 | +import { InfoEmbed } from '#src/utils/EmbedUtils.js'; |
| 9 | +import { getEmoji } from '#src/utils/EmojiUtils.js'; |
| 10 | +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, RepliableInteraction } from 'discord.js'; |
| 11 | + |
| 12 | +export default class InboxCommand extends BaseCommand { |
| 13 | + private readonly userDbService = new UserDbService(); |
| 14 | + |
| 15 | + constructor() { |
| 16 | + super({ |
| 17 | + name: 'inbox', |
| 18 | + description: 'Check your inbox for latest important updates & announcements', |
| 19 | + types: { slash: true, prefix: true }, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + async execute(ctx: Context) { |
| 24 | + await showInbox(ctx, this.userDbService); |
| 25 | + } |
| 26 | + |
| 27 | + @RegisterInteractionHandler('inbox', 'viewOlder') |
| 28 | + async handleViewOlder(interaction: RepliableInteraction) { |
| 29 | + await showInbox(interaction, this.userDbService, { showOlder: true, ephemeral: true }); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export async function showInbox( |
| 34 | + interaction: Context | RepliableInteraction, |
| 35 | + userDbService: UserDbService, |
| 36 | + opts?: { showOlder?: boolean; ephemeral?: boolean }, |
| 37 | +) { |
| 38 | + const userData = await userDbService.getUser(interaction.user.id); |
| 39 | + const inboxLastRead = userData?.inboxLastReadDate || new Date(); |
| 40 | + |
| 41 | + const announcements = !opts?.showOlder |
| 42 | + ? await db.announcement.findMany({ |
| 43 | + where: { createdAt: { gt: inboxLastRead } }, |
| 44 | + take: 10, |
| 45 | + orderBy: { createdAt: 'desc' }, |
| 46 | + }) |
| 47 | + : await db.announcement.findMany({ |
| 48 | + where: { createdAt: { lt: inboxLastRead } }, |
| 49 | + take: 50, // limit to 50 older announcementsorderBy: { createdAt: 'desc' }, |
| 50 | + }); |
| 51 | + |
| 52 | + const components = !opts?.showOlder |
| 53 | + ? [ |
| 54 | + new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 55 | + new ButtonBuilder() |
| 56 | + .setCustomId(new CustomID().setIdentifier('inbox', 'viewOlder').toString()) |
| 57 | + .setLabel('View Older') |
| 58 | + .setStyle(ButtonStyle.Secondary), |
| 59 | + ), |
| 60 | + ] |
| 61 | + : []; |
| 62 | + |
| 63 | + if (announcements.length === 0) { |
| 64 | + const embed = new InfoEmbed() |
| 65 | + .setTitle(':tada: All caught up!') |
| 66 | + .setDescription( |
| 67 | + `I'll let you know when there's more. But for now, there's only Chipi here: ${getEmoji('chipi_smile', interaction.client)}`, |
| 68 | + ); |
| 69 | + await interaction.reply({ embeds: [embed], components }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + new Pagination(interaction.client, { hiddenButtons: ['search', 'select'] }) |
| 74 | + .addPages( |
| 75 | + announcements.map((announcement) => ({ |
| 76 | + components, |
| 77 | + embeds: [ |
| 78 | + new InfoEmbed() |
| 79 | + .setTitle(announcement.title) |
| 80 | + .setDescription(announcement.content) |
| 81 | + .setThumbnail(announcement.thumbnailUrl) |
| 82 | + .setImage(announcement.imageUrl) |
| 83 | + .setTimestamp(announcement.createdAt), |
| 84 | + ], |
| 85 | + })), |
| 86 | + ) |
| 87 | + .run(interaction, { ephemeral: opts?.ephemeral }); |
| 88 | + |
| 89 | + await userDbService.updateUser(interaction.user.id, { inboxLastReadDate: new Date() }); |
| 90 | +} |
0 commit comments