Skip to content

Commit ffbc580

Browse files
authored
Refactor/submitted checkin (#57)
* chore: file rename * feat: remove unused buttons after review a checkin * fix: remove unnecessary message
1 parent 199bc73 commit ffbc580

File tree

2 files changed

+29
-10
lines changed
  • src/bot
    • commands/checkin/handlers
    • events/interaction-create/checkin/validators

2 files changed

+29
-10
lines changed
File renamed without changes.

src/bot/events/interaction-create/checkin/validators/index.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import type { Attachment as AttachmentType } from '@type/attachment'
44
import type { CheckinAllowedEmojiType, CheckinColumn, CheckinStatusType, Checkin as CheckinType } from '@type/checkin'
55
import type { CheckinStreak } from '@type/checkin-streak'
66
import type { User } from '@type/user'
7-
import type { Attachment, Client, EmbedBuilder, Guild, GuildMember, Interaction, Message, TextChannel } from 'discord.js'
7+
import type { ActionRow, Attachment, ButtonComponent, Client, EmbedBuilder, Guild, GuildMember, Interaction, Message, TextChannel } from 'discord.js'
88
import crypto from 'node:crypto'
9-
import { CheckinError } from '@commands/checkin/handlers/checkin'
9+
import { CheckinError } from '@commands/checkin/handlers'
1010
import { AURA_FARMING_CHANNEL, CHECKIN_CHANNEL, GRINDER_ROLE } from '@config/discord'
1111
import { SubmittedCheckinError } from '@events/message-reaction-add/checkin/handlers/submitted'
1212
import { createEmbed, decodeSnowflakes, encodeSnowflake, getCustomId } from '@utils/component'
@@ -38,6 +38,12 @@ export class Checkin extends CheckinMessage {
3838
'🔥': 'APPROVED',
3939
}
4040

41+
static readonly CHECKIN_DELETED_BUTTONS = [
42+
CHECKIN_APPROVE_BUTTON_ID,
43+
CHECKIN_REJECT_BUTTON_ID,
44+
CHECKIN_CUSTOM_BUTTON_ID,
45+
]
46+
4147
static REVERSED_EMOJI_STATUS = Object.fromEntries(
4248
Object.entries(this.EMOJI_STATUS).map(([emoji, status]) => [status, emoji]),
4349
) as Record<CheckinStatusType, CheckinAllowedEmojiType>
@@ -169,13 +175,6 @@ export class Checkin extends CheckinMessage {
169175
allowedMentions: { users: [member.id], roles: [] },
170176
})
171177
}
172-
else {
173-
const checkinChannel = await getChannel(guild, CHECKIN_CHANNEL) as TextChannel
174-
await sendAsBot(null, checkinChannel, {
175-
content: `Hey, <@${member.id}>. You already have <@&${newRole.id}>`,
176-
allowedMentions: { users: [member.id], roles: [] },
177-
}, true)
178-
}
179178
}
180179

181180
static assertCheckinToday(user: User) {
@@ -439,7 +438,7 @@ export class Checkin extends CheckinMessage {
439438
const message = await checkinChannel.messages.fetch(messageId)
440439

441440
await this.validateCheckinHandleToUser(guild, flamewarden, updatedCheckin.user!.discord_id, updatedCheckin)
442-
await message.react(this.REVERSED_EMOJI_STATUS[checkinStatus])
441+
await this.editSubmittedCheckinMessage(message, checkinStatus)
443442

444443
return updatedCheckin
445444
}
@@ -457,6 +456,26 @@ export class Checkin extends CheckinMessage {
457456
await this.sendCheckinStatusToMember(flamewarden, member, updatedCheckin)
458457
}
459458

459+
static async editSubmittedCheckinMessage(message: Message, checkinStatus: CheckinStatusType) {
460+
await message.react(this.REVERSED_EMOJI_STATUS[checkinStatus])
461+
462+
const newRows = this.getNewButtons(message.components as ActionRow<ButtonComponent>[])
463+
await message.edit({ components: newRows })
464+
}
465+
466+
static getNewButtons(components: readonly ActionRow<ButtonComponent>[]): ActionRowBuilder<ButtonBuilder>[] {
467+
return components
468+
.map((row) => {
469+
const buttons = row.components
470+
.filter(btn => btn.customId && !this.CHECKIN_DELETED_BUTTONS.some(id => btn.customId!.startsWith(id)))
471+
.map(btn => ButtonBuilder.from(btn))
472+
const newRow = new ActionRowBuilder<ButtonBuilder>().addComponents(...buttons)
473+
474+
return buttons.length ? newRow : null
475+
})
476+
.filter((row): row is ActionRowBuilder<ButtonBuilder> => row !== null)
477+
}
478+
460479
static async updateCheckinMsgLink(interaction: Interaction, prisma: PrismaClient, checkin: CheckinType, msg: Message): Promise<CheckinType> {
461480
const msgLink = messageLink(interaction.channelId!, msg.id, interaction.guildId!)
462481

0 commit comments

Comments
 (0)