-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinteractionCreate.ts
More file actions
248 lines (229 loc) · 7.81 KB
/
interactionCreate.ts
File metadata and controls
248 lines (229 loc) · 7.81 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {
ActionRowBuilder,
type AutocompleteInteraction,
ButtonBuilder,
type ButtonInteraction,
ButtonStyle,
type ChatInputCommandInteraction,
Events,
type Interaction,
type InteractionEditReplyOptions,
type InteractionReplyOptions,
italic,
type MessageContextMenuCommandInteraction,
MessageFlags,
ModalBuilder,
type ModalSubmitInteraction,
type RepliableInteraction,
TextInputBuilder,
TextInputStyle,
} from "discord.js";
import type { DatadropClient } from "../datadrop.js";
import type { Event } from "../models/index.js";
import { CommandHandler } from "../services/CommandHandler.js";
export default {
name: Events.InteractionCreate,
execute: interactionCreate,
} as Event;
async function interactionCreate(
client: DatadropClient,
interaction: Interaction,
) {
if (
interaction.isChatInputCommand() ||
interaction.isMessageContextMenuCommand() ||
interaction.isAutocomplete()
) {
await handleCommandInteraction(client, interaction);
} else if (isVerificationButton(interaction)) {
await handleVerificationButton(client, interaction);
} else if (isVerificationModalSubmission(interaction)) {
await handleVerificationModalSubmission(client, interaction);
} else if (
isUnhandledInteraction(interaction) &&
interaction.isRepliable()
) {
await interaction.reply({
content: "Ce message ne t'était assurément pas destiné!",
flags: MessageFlags.Ephemeral,
});
}
}
async function handleCommandInteraction(
client: DatadropClient,
interaction:
| ChatInputCommandInteraction
| MessageContextMenuCommandInteraction
| AutocompleteInteraction,
) {
const commandHandler = new CommandHandler(client);
if (commandHandler.shouldExecute(interaction)) {
await commandHandler.execute(interaction);
}
}
async function handleVerificationButton(
client: DatadropClient,
interaction: ButtonInteraction,
) {
if (await isAlreadyVerified(client, interaction)) return;
await showVerificationModal(interaction);
}
async function handleVerificationModalSubmission(
client: DatadropClient,
interaction: ModalSubmitInteraction,
) {
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
if (await isAlreadyVerified(client, interaction)) return;
let content: string;
switch (interaction.customId) {
case `lacm${interaction.user.id}`: {
if (emailIsValid(interaction)) {
content = await getEmailAndSendCode(interaction, client);
await showVerificationButton(
interaction,
`${content}\n${italic("D'ailleurs, l'email peut potentiellement se retrouver dans tes spams!")}`,
client,
);
} else {
await interaction.editReply({
content:
"L'adresse email fournie n'est pas valide!\nSi tu es un.e étudiant.e, elle doit correspondre à etuXXXXX@henallux.be.\nSi tu es un.e professeur.e, elle doit correspondre à mdpXXXXX@henallux.be.",
});
}
break;
}
case `lav${interaction.user.id}`: {
content = await verifyCode(interaction, client);
await interaction.editReply({ content });
break;
}
}
}
async function isAlreadyVerified(
client: DatadropClient,
interaction: Interaction,
) {
const userFromDatabase = await client.database.read(interaction.user.id);
if (userFromDatabase?.activatedCode) {
if (interaction.isRepliable()) {
const replyOptions: InteractionReplyOptions = {
content:
"Tu as déjà lié ton compte Hénallux avec ton compte Discord!",
flags: MessageFlags.Ephemeral,
};
if (interaction.deferred) {
await interaction.editReply(
replyOptions as InteractionEditReplyOptions,
);
} else if (interaction.replied) {
await interaction.followUp(replyOptions);
} else {
await interaction.reply(replyOptions);
}
}
return true;
}
return false;
}
function isVerificationButton(
interaction: Interaction,
): interaction is ButtonInteraction {
return (
interaction.isButton() &&
interaction.customId.startsWith("la") &&
interaction.customId.includes(interaction.user.id)
);
}
async function showVerificationModal(interaction: ButtonInteraction) {
const modal = new ModalBuilder().setTitle("Lier son compte");
const input = new TextInputBuilder();
switch (interaction.customId) {
case `lae${interaction.user.id}`:
modal.setCustomId(`lacm${interaction.user.id}`);
input
.setLabel("Email Hénallux")
.setPlaceholder("********@henallux.be")
.setRequired(true)
.setMinLength(20)
.setStyle(TextInputStyle.Short)
.setCustomId("email");
break;
case `lacb${interaction.user.id}`:
modal.setCustomId(`lav${interaction.user.id}`);
input
.setLabel("Code de vérification")
.setPlaceholder("******")
.setRequired(true)
.setMinLength(6)
.setMaxLength(6)
.setStyle(TextInputStyle.Short)
.setCustomId("code");
break;
}
const inputComponent =
new ActionRowBuilder<TextInputBuilder>().addComponents(input);
modal.addComponents(inputComponent);
await interaction.showModal(modal);
}
function isVerificationModalSubmission(
interaction: Interaction,
): interaction is ModalSubmitInteraction {
return (
interaction.isModalSubmit() &&
interaction.customId.includes(interaction.user.id)
);
}
async function verifyCode(
interaction: ModalSubmitInteraction,
client: DatadropClient,
) {
const code = interaction.fields.getTextInputValue("code");
return await client.verificationManager.verifyCode(
interaction.user.id,
code,
);
}
function emailIsValid(interaction: ModalSubmitInteraction): boolean {
const regex = /(etu\d{5,}|mdp[a-z]{5,})@henallux\.be/;
const email = interaction.fields.getTextInputValue("email");
return regex.test(email);
}
async function getEmailAndSendCode(
interaction: ModalSubmitInteraction,
client: DatadropClient,
) {
const email = interaction.fields.getTextInputValue("email");
return await client.verificationManager.sendCode(interaction.user.id, {
to: email,
guildId: interaction.guildId,
});
}
async function showVerificationButton(
interaction: RepliableInteraction,
content: string,
client: DatadropClient,
) {
const linkAccountButton = new ButtonBuilder()
.setLabel("Vérifier son code")
.setEmoji("🎰")
.setStyle(ButtonStyle.Primary)
.setCustomId(`lacb${interaction.user.id}`)
.setDisabled(
content.includes(client.errorMessage) ||
content.includes(client.activeAccountMessage),
);
const buttonComponent = new ActionRowBuilder<ButtonBuilder>().addComponents(
linkAccountButton,
);
await interaction.editReply({ content, components: [buttonComponent] });
}
function isUnhandledInteraction(interaction: Interaction) {
return (
(interaction.isButton() ||
interaction.isModalSubmit() ||
interaction.isStringSelectMenu()) &&
!interaction.customId.startsWith("sr-") &&
!interaction.customId.startsWith("ac-") &&
!interaction.customId.includes(interaction.user.id)
);
}