This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KEP_myExamsCmdImpl.ts
150 lines (131 loc) · 6.58 KB
/
KEP_myExamsCmdImpl.ts
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
import { APIEmbedField, ApplicationCommandType, ChatInputApplicationCommandData, ChatInputCommandInteraction, Collection, CommandInteraction, InteractionReplyOptions, Message, ReplyMessageOptions, RESTJSONErrorCodes, Snowflake } from "discord.js";
import { calendar_v3 } from "googleapis";
import moment from "moment";
import 'moment/locale/el';
import urlRegex from "url-regex";
import { guilds } from "../../..";
import { guildId as kepGuildId } from "../../../../values/KEP/IDs.json";
import { examsPrefix } from "../../../../values/KEP/literals.json";
import { commandLiteral } from "../../../Entities/Generic/command";
import { KepGuild } from "../../../Handlers/Guilds/Impl/KepGuild";
import { fetchCommandID } from "../../../Queries/Generic/Commands";
import { textSimilarity } from "../../../tools/cmptxt";
import { sliceToEmbeds } from "../../../tools/Embed";
import { AbstractGuildCommand } from "../AbstractGuildCommand";
import { KEP_myExamsCmd } from "../Interf/KEP_myExamsCmd";
moment.locale('el');
//TODO: cleanup
const fieldBuilder = ((ev: calendar_v3.Schema$Event): APIEmbedField => ({
name: `• 📅 ${moment(ev.start.dateTime).format('LL')}, ${moment(ev.start.dateTime).tz("Europe/Athens").format("kk:mm")} - ${moment(ev.end.dateTime).tz("Europe/Athens").format("kk:mm")}`,
value: `[**${ev.summary}**](${ev.description.match(urlRegex({ strict: true })).toString()})`
}));
export class KEP_myExamsCmdImpl extends AbstractGuildCommand implements KEP_myExamsCmd {
id: Collection<Snowflake, Snowflake> = new Collection(null);
readonly keyword = `myexams`;
readonly guide = `Εμφανίζει τα επερχόμενα εξεταζόμενα μαθήματά σας`;
readonly usage = `${this.keyword}`;
private constructor() { super() }
static async init(): Promise<KEP_myExamsCmd> {
const cmd = new KEP_myExamsCmdImpl();
cmd.id = await fetchCommandID(cmd.keyword);
return cmd;
}
readonly aliases = this.mergeAliases
(
['my_exams', 'exams', 'myexams'], this.keyword
);
getCommandData(guild_id: Snowflake): ChatInputApplicationCommandData {
return {
name: this.keyword,
description: this.guide,
type: ApplicationCommandType.ChatInput,
}
}
async interactiveExecute(interaction: ChatInputCommandInteraction): Promise<unknown> {
return this.handleRequest(interaction);
}
async execute(message: Message, { }: commandLiteral): Promise<unknown> {
return this.handleRequest(message);
}
handleRequest(source: ChatInputCommandInteraction | Message) {
const user = source instanceof ChatInputCommandInteraction ?
source.user :
source.author;
const courses = (guilds.get(kepGuildId) as KepGuild).students.get(user.id)?.courses;
const events = (guilds.get(kepGuildId) as KepGuild).events
.filter(ev => ev.summary?.startsWith(examsPrefix));
const responseBuilder = (response: string): ReplyMessageOptions | InteractionReplyOptions =>
source instanceof ChatInputCommandInteraction ?
{ content: response, ephemeral: true } :
{ content: response };
if (!courses || courses.size === 0)
return this.respond(source, { content: "Δεν έχετε επιλέξει μαθήματα" })
if (events.length === 0)
return this.respond(source, { content: "Δεν βρέθηκαν προγραμματισμένα μαθήματα" });
const studentCourseEvents = events
.map(ev => ({
...ev,
summary: ev.summary.replace(examsPrefix, '')
.trimStart()
.trimEnd()
})
)
.filter(ev => courses
.find(c => textSimilarity(
c.name,
ev.summary
) > 0.85
)
)
if (studentCourseEvents.length === 0)
return this.respond(source, { content: 'Δεν βρέθηκαν προγραμματισμένα μαθήματα' });
const [first, last] = [studentCourseEvents[0], studentCourseEvents[studentCourseEvents.length - 1]]
.map(ev => moment(ev.start.dateTime).format('LL'));
const responseEmbeds = sliceToEmbeds({
data: studentCourseEvents.map(fieldBuilder),
headerEmbed: {
title: `MyExams`,
description: `Η εξεταστική σας ξεκινάει **${first}** και ολοκληρώνεται **${last}**`
}
})
user.send({ embeds: responseEmbeds })
.then(msg => {
msg.react("🗑");
if (source instanceof CommandInteraction)
this.respond(source, { content: `Σας το έστειλα στα DMs` })
else
source.react('👌')
})
.catch(async err => {
if (err.code === RESTJSONErrorCodes.CannotSendMessagesToThisUser) {
if (source instanceof ChatInputCommandInteraction) {
const interaction = source;
const resp: InteractionReplyOptions = {
content: `Τα DMs σας ειναι κλειστά, το αποστέλλω εδώ`,
embeds: responseEmbeds,
ephemeral: true
}
return interaction.replied ?
interaction.followUp(resp) :
interaction.reply(resp)
}
else {
const emoji = "📨";
const msg = await source.reply(`Έχετε κλειστά DMs. Εαν θέλετε να το στείλω εδώ, πατήστε το ${emoji}`);
await msg.react(emoji);
await msg.react("🗑️");
const collected = await msg.awaitReactions({
filter: (reaction, user) => ['🗑️', '🗑', emoji].includes(reaction.emoji.name) && !user.bot,
time: 10000,
max: 1
});
if (collected.first().emoji.name === emoji)
await source.reply({ embeds: responseEmbeds });
await msg.delete(); //delete prompt either way
}
}
else
throw err;
})
}
}