-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckin-status.ts
More file actions
154 lines (129 loc) · 6.19 KB
/
Copy pathcheckin-status.ts
File metadata and controls
154 lines (129 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type { PrismaClient } from '@generatedDB/client'
import type { CheckinStatusType, Checkin as CheckinType } from '@type/checkin'
import type { User } from '@type/user'
import type { EmbedBuilder, Guild, Interaction, ThreadAutoArchiveDuration } from 'discord.js'
import { CHECKIN_CHANNEL, FLAMEWARDEN_ROLE } from '@config/discord'
import { CHECKIN_STATUS_CLARIFICATION_BUTTON_ID } from '@events/interaction-create/checkin/handlers/status-clarification-button'
import { CHECKIN_STATUS_NOTE_BUTTON_ID } from '@events/interaction-create/checkin/handlers/status-note-button'
import { Checkin } from '@events/interaction-create/checkin/validators'
import { createEmbed, decodeSnowflakes, encodeSnowflake, getCustomId } from '@utils/component'
import { isDateYesterday } from '@utils/date'
import { DiscordAssert } from '@utils/discord'
import { DUMMY } from '@utils/placeholder'
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, messageLink, PermissionsBitField } from 'discord.js'
import { CheckinStatusError } from '../handlers/checkin-status'
import { CheckinStatusMessage } from '../messages/checkin-status'
export class CheckinStatus extends CheckinStatusMessage {
static override BASE_PERMS = [
...DiscordAssert.BASE_PERMS,
PermissionsBitField.Flags.UseApplicationCommands,
]
static CLARIFICATION_EMOJI = '❓'
static override THREAD_ARCHIVE_DURATION: ThreadAutoArchiveDuration = 1440
static getButtonId(interaction: Interaction, customId: string) {
const [prefix, guildId, checkinMessageId] = decodeSnowflakes(customId)
if (!guildId)
throw new CheckinStatusError(this.ERR.GuildMissing)
if (interaction.guildId !== guildId)
throw new CheckinStatusError(this.ERR.NotGuild)
if (!checkinMessageId)
throw new CheckinStatusError(this.ERR.CheckinIdMissing)
const checkinLink = messageLink(CHECKIN_CHANNEL, checkinMessageId, interaction.guildId)
return { prefix, guildId, checkinLink }
}
static async getEmbedStatusContent(guild: Guild, userDiscordId: string, checkin?: CheckinType) {
let content = ''
let embed: EmbedBuilder
const checkinStreak = checkin?.checkin_streak
const hasCheckedInToday = Checkin.hasCheckinToday(checkinStreak, checkin)
if (checkin && hasCheckedInToday) {
const flamewarden = await guild.members.fetch(checkin.reviewed_by!)
switch (checkin.status as CheckinStatusType) {
case 'WAITING': {
content = `<@&${FLAMEWARDEN_ROLE}>`
embed = createEmbed(
`🧭 Check-In #${checkin.public_id}`,
CheckinStatus.MSG.WaitingCheckin(userDiscordId, checkin),
DUMMY.COLOR,
{ text: DUMMY.FOOTER },
)
break
}
case 'APPROVED': {
embed = createEmbed(
`🔥 Check-In #${checkin.public_id}`,
CheckinStatus.MSG.ApprovedCheckin(userDiscordId, flamewarden, checkin),
DUMMY.COLOR,
{ text: DUMMY.FOOTER },
)
break
}
default: {
embed = createEmbed(
`❌ Check-In #${checkin.public_id}`,
CheckinStatus.MSG.RejectedCheckin(userDiscordId, flamewarden, checkin),
DUMMY.COLOR,
{ text: DUMMY.FOOTER },
)
break
}
}
return { content, embed }
}
const shouldShowNoCheckin = !checkin || (checkin.status === 'APPROVED' && isDateYesterday(checkin.created_at))
if (shouldShowNoCheckin) {
embed = createEmbed(
`🧐 Check-In`,
CheckinStatus.MSG.NoCheckin(userDiscordId, checkinStreak),
DUMMY.COLOR,
{ text: DUMMY.FOOTER },
)
return { content, embed }
}
const flamewarden = await guild.members.fetch(checkin.reviewed_by!)
const buttons = this.generateButtons(guild.id, checkin)
embed = createEmbed(
`🕯️ Check-In #${checkin.public_id}`,
CheckinStatus.MSG.LastCheckin(userDiscordId, checkin, flamewarden),
DUMMY.COLOR,
{ text: DUMMY.FOOTER },
)
return { content, embed, buttons }
}
static generateButtons(guildId: string, checkin: CheckinType): ActionRowBuilder<ButtonBuilder> | undefined {
if (checkin.status === 'WAITING') {
const { messageId } = this.getMessageFromLink(checkin.link!)
const noteButtonId = getCustomId([CHECKIN_STATUS_NOTE_BUTTON_ID, encodeSnowflake(guildId), encodeSnowflake(messageId)])
const noteButton = new ButtonBuilder()
.setCustomId(noteButtonId)
.setLabel('📜 Maklumat Klarifikasi')
.setStyle(ButtonStyle.Primary)
const clarificationButtonId = getCustomId([CHECKIN_STATUS_CLARIFICATION_BUTTON_ID, encodeSnowflake(guildId), encodeSnowflake(messageId)])
const clarificationButton = new ButtonBuilder()
.setCustomId(clarificationButtonId)
.setLabel('❓ Ajukan Klarifikasi')
.setStyle(ButtonStyle.Success)
return new ActionRowBuilder<ButtonBuilder>().addComponents(noteButton, clarificationButton)
}
}
static async getUser(prisma: PrismaClient, userDiscordId: string): Promise<User> {
const user = await prisma.user.findFirst({
where: {
discord_id: userDiscordId,
},
select: {
id: true,
discord_id: true,
created_at: true,
updated_at: true,
checkins: {
orderBy: { created_at: 'desc' },
take: 1,
include: { checkin_streak: true },
},
},
}) as User
await Checkin.setAttachments(prisma, user?.checkins?.[0])
return user
}
}