-
Notifications
You must be signed in to change notification settings - Fork 13
/
suggestion.ts
537 lines (474 loc) · 15.5 KB
/
suggestion.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import {
SlashCommandBuilder,
Colors,
escapeMarkdown,
cleanContent,
GuildMember,
EmbedBuilder,
Message,
Snowflake,
User,
} from "discord.js";
import client, { guild } from "../client.js";
import CONSTANTS from "../common/CONSTANTS.js";
import giveXp from "../common/xp.js";
import { escapeLinks } from "../util/markdown.js";
import { getAllMessages, paginate, reactAll } from "../util/discord.js";
import { truncateText } from "../util/text.js";
import type { ChatInputCommand } from "../common/types/command.js";
export const SUGGESTION_EMOJIS: [string, string][] = [
["👍", "👎"], // These are the emojis that are currently used.
["959117513088720926", "🍅"],
["575851403558256642", "575851403600330792"],
["✅", "613912745699442698"],
["613912747578621952", "613912747440209930"],
["613912747612045322", "613913094984564736"],
["613912745837985832", "613912745691054080"],
["😀", "😔"],
["❤", "💔"],
["749005259682086964", "749005284403445790"],
];
const ANSWERS = [
{ name: "Unanswered", color: Colors.Greyple, description: "This hasn’t yet been answered" },
{
color: Colors.Green,
description: "This will probably be added if anyone codes it",
name: "Good Idea",
},
{
color: Colors.Orange,
description: "This already exists in Scratch or in Scratch Addons",
name: "Implemented",
},
{
color: Colors.Red,
description: "This isn’t something we may add for technical reasons",
name: "Impossible",
},
{
color: Colors.LuminousVividPink,
description: "This is possible, but it’d require lots of code and isn’t worth it",
name: "Impractical",
},
{
color: Colors.Gold,
description: "Someone is currently working on this",
name: "In Development",
},
{
color: Colors.DarkGreen,
description:
"This is possible, but it could be rejected for things like ethical or technical reasons",
name: "Possible",
},
{
color: Colors.DarkRed,
description: "Wouldn’t work for non-SA users or users who don’t have the addon/option on",
name: "Incompatible",
},
{
color: Colors.Purple,
description: "We don’t want to add this for some reason",
name: "Rejected",
},
] as const;
const RATELIMIT_TIMEOUT = 3_000;
const RATELIMT_MESSAGE =
"If the thread title doesn’t update immediately, you may have been ratelimited. I will automatically change the title once the ratelimit is up (within the next hour).";
const cooldowns: Record<Snowflake, number> = {};
const COOLDOWN = 60_000;
/**
* Get the member who made a suggestion.
*
* @param message - The message to get the member from.
*
* @returns The member who made the suggestion.
*/
async function getUserFromSuggestion(message: Message<true>): Promise<GuildMember | User> {
const author =
message.author.id === CONSTANTS.robotop
? message.embeds[0]?.footer?.text.split(": ")[1]
: /\/(?<userId>\d+)\//.exec(message.embeds[0]?.author?.iconURL ?? "")?.groups?.userId;
if (author) {
const fetchedMember =
(await guild?.members.fetch(author).catch(() => undefined)) ||
(await client?.users.fetch(author).catch(() => undefined));
if (fetchedMember) return fetchedMember;
}
return message.member ?? message.author;
}
function parseSuggestionTitle(title: string) {
const segments = title.split(" | ");
const found = ANSWERS.find(({ name }) => name === segments[0]);
if (found) {
segments.shift();
return { answer: found, name: segments.join(" | ") };
} else {
const found = ANSWERS.find(({ name }) => name === segments.at(-1));
segments.pop();
return { answer: found || ANSWERS[0], name: segments.join(" | ") };
}
}
const CHANNEL_TAG = `#${CONSTANTS.channels.suggestions?.name}`;
const minDescLength = 30 * +(process.env.NODE_ENV === "production");
const info: ChatInputCommand = {
data: new SlashCommandBuilder()
.setDescription(`Commands to manage suggestions in ${CHANNEL_TAG}`)
.addSubcommand((subcommand) =>
subcommand
.setName("create")
.setDescription(`Create a new suggestion in ${CHANNEL_TAG}`)
.addStringOption((option) =>
option
.setName("title")
.setDescription(`A short summary of the suggestion `)
.setRequired(true)
.setMaxLength(100),
)
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("A detailed description of the suggestion")
.setRequired(true)
.setMinLength(minDescLength),
),
)
.addSubcommand((subcommand) =>
subcommand
.setName("answer")
.setDescription(
`(Devs only; For use in ${CHANNEL_TAG}’s threads) Answer a suggestion`,
)
.addStringOption((option) => {
const newOption = option
.setName("answer")
.setDescription("Answer to the suggestion")
.setRequired(true);
for (const [index, answer] of ANSWERS.entries()) {
if (index)
newOption.addChoices({
name: `${answer.name} (${answer.description})`,
value: answer.name,
});
}
return newOption;
}),
)
.addSubcommand((subcommand) =>
subcommand
.setName("edit")
.setDescription(
`(OP/Mods only; For use in ${CHANNEL_TAG}’s threads) Edit a suggestion`,
)
.addStringOption((option) =>
option
.setName("title")
.setDescription(`A short summary of the suggestion`)
.setRequired(false)
.setMaxLength(100),
)
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("A detailed description of the suggestion")
.setRequired(false)
.setMinLength(minDescLength),
),
)
.addSubcommand((subcommand) =>
subcommand
.setName("get-top")
.setDescription("Get the top suggestions")
.addUserOption((input) =>
input
.setName("user")
.setDescription("Filter suggestions to only get those by a certain user")
.setRequired(false),
)
.addStringOption((option) => {
const newOption = option
.setName("answer")
.setDescription(
"Filter suggestions to only get those with a certain answer",
)
.setRequired(false);
for (const answer of ANSWERS)
newOption.addChoices({
name: `${answer.name} (${answer.description})`,
value: answer.name,
});
return newOption;
}),
),
async interaction(interaction) {
if (!CONSTANTS.channels.suggestions)
throw new ReferenceError("Could not find suggestions channel!");
const command = interaction.options.getSubcommand(true);
const starter =
interaction.channel?.isThread() && (await interaction.channel?.fetchStarterMessage());
if (starter && starter.author.id === client.user?.id) {
const emoji = starter.reactions.valueOf().first()?.emoji;
await reactAll(
starter,
SUGGESTION_EMOJIS.find(([one]) => one === emoji?.id || emoji?.name) ||
SUGGESTION_EMOJIS[0] ||
[],
);
}
switch (command) {
case "create": {
const author = interaction.member;
if (!(author instanceof GuildMember))
throw new TypeError("interaction.member must be a GuildMember");
const title = escapeMarkdown(interaction.options.getString("title", true));
const embed = new EmbedBuilder()
.setColor(ANSWERS[0].color)
.setAuthor({
iconURL: author.displayAvatarURL(),
name: author.displayName ?? interaction.user.username,
})
.setTitle(title)
.setDescription(interaction.options.getString("suggestion", true))
.setFooter({ text: ANSWERS[0].name });
if ((cooldowns[author.id] || 0) > Date.now()) {
return await interaction.reply({
content: `${
CONSTANTS.emojis.statuses.no
} You can only post a suggestion every ${Math.max(
1,
Math.round(COOLDOWN / 1_000),
)} seconds. Please wait ${Math.max(
1,
Math.round(((cooldowns[author.id] || 0) - Date.now()) / 1_000),
)} seconds before posting another suggestion.`,
ephemeral: true,
});
}
cooldowns[author.id] = Date.now() + COOLDOWN;
const message = await CONSTANTS.channels.suggestions.send({ embeds: [embed] });
const thread = await message.startThread({
name: `${title ?? ""} | Unanswered`,
reason: `Suggestion by ${interaction.user.tag}`,
});
await Promise.all([
thread.members.add(interaction.user.id),
reactAll(message, SUGGESTION_EMOJIS[0] || []),
giveXp(author),
interaction.reply({
content: `${CONSTANTS.emojis.statuses.yes} Suggestion posted! See ${
message.thread?.toString() ?? ""
}. If you made any mistakes, you can fix them with \`/suggestion edit\`.`,
ephemeral: true,
}),
]);
break;
}
case "answer": {
const answer = interaction.options.getString("answer", true);
if (
!interaction.channel?.isThread() ||
interaction.channel.parent?.id !== CONSTANTS.channels.suggestions?.id
) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} This command can only be used in threads in ${CONSTANTS.channels.suggestions}.`,
ephemeral: true,
});
return;
}
if (!(interaction.member instanceof GuildMember))
throw new TypeError("interaction.member must be a GuildMember");
if (
!CONSTANTS.roles.dev ||
!interaction.member.roles.resolve(CONSTANTS.roles.dev.id)
) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} You don’t have permission to run this command!`,
ephemeral: true,
});
return;
}
const { name, answer: oldAnswer } = parseSuggestionTitle(interaction.channel.name);
if (oldAnswer.name === answer) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} That's already the answer!`,
ephemeral: true,
});
return;
}
const promises = [
Promise.race([
new Promise((resolve) => setTimeout(resolve, RATELIMIT_TIMEOUT)),
interaction.channel.setName(
(name ? name + " | " : "") + answer,
`Suggestion answered by ${interaction.user.tag}`,
),
]),
];
if (starter && starter?.author.id === client.user?.id) {
const embed = starter.embeds[0]
? EmbedBuilder.from(starter.embeds[0])
: new EmbedBuilder();
embed
.setColor(
ANSWERS.find(({ name }) => answer === name)?.color ?? ANSWERS[0].color,
)
.setFooter({ text: answer });
promises.push(starter.edit({ embeds: [embed] }));
}
promises.push(
interaction.reply({
content:
`${
CONSTANTS.emojis.statuses.yes
} Successfully answered suggestion as **${escapeMarkdown(
answer,
)}**! *${escapeMarkdown(
ANSWERS.find(({ name }) => name === answer)?.description || "",
)}*.` +
(interaction.channel.name.startsWith(answer + " |")
? ""
: "\n" + RATELIMT_MESSAGE),
ephemeral: false,
}),
);
await Promise.all(promises);
break;
}
case "edit": {
const body = interaction.options.getString("suggestion"),
title = escapeMarkdown(interaction.options.getString("title") ?? "");
if (
!interaction.channel?.isThread() ||
interaction.channel.parent?.id !== CONSTANTS.channels.suggestions?.id
) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} This command may only be used in threads in ${CONSTANTS.channels.suggestions}.`,
ephemeral: true,
});
return false;
}
if (!starter || starter.author.id !== client.user?.id) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} Cannot edit this suggestion.`,
ephemeral: true,
});
return false;
}
const user = await getUserFromSuggestion(starter);
if (!(interaction.member instanceof GuildMember))
throw new TypeError("interaction.member must be a GuildMember");
const isMod =
CONSTANTS.roles.mod && interaction.member.roles.resolve(CONSTANTS.roles.mod.id);
if (interaction.user.id !== user?.id && (!isMod || (isMod && body))) {
await interaction.reply({
content: `${CONSTANTS.emojis.statuses.no} You don’t have permission to use this command.`,
ephemeral: true,
});
return false;
}
const embed = starter.embeds[0]
? EmbedBuilder.from(starter.embeds[0])
: new EmbedBuilder();
if (body) embed.setDescription(body);
const { answer } = parseSuggestionTitle(interaction.channel.name);
await Promise.all([
Promise.race([
interaction.channel.setName(
(title || embed.data.title) + " | " + answer.name,
"Suggestion edited",
),
new Promise((resolve) => setTimeout(resolve, RATELIMIT_TIMEOUT)),
]),
starter.edit({
embeds: [embed.setTitle(title || embed.data.title || "")],
}),
interaction.reply({
content: `${CONSTANTS.emojis.statuses.yes} Successfully edited suggestion!${
title && (await interaction.channel.fetch()).name.endsWith(title)
? " " + RATELIMT_MESSAGE
: ""
}`,
ephemeral:
interaction.user.id === (await getUserFromSuggestion(starter)).id,
}),
]);
break;
}
case "get-top": {
const requestedUser = interaction.options.getUser("user");
const requestedAnswer = interaction.options.getString("answer");
await interaction.deferReply();
const unfiltered = await getAllMessages(CONSTANTS.channels.suggestions);
// todo: asyncFilter
const all = (
await Promise.all(
unfiltered.map(async (message) => {
const count = SUGGESTION_EMOJIS.map(([upvote, downvote]) => {
const upvoteReaction = message.reactions.resolve(upvote);
const downvoteReaction = message.reactions.resolve(downvote);
if (!upvoteReaction || !downvoteReaction) return false;
return (upvoteReaction.count ?? 0) - (downvoteReaction.count ?? 0);
}).find((currentCount) => typeof currentCount === "number");
if (typeof count !== "number") return;
const answer = message.thread
? parseSuggestionTitle(message.thread.name).answer
: ANSWERS[0];
if (
requestedAnswer &&
answer.name.toLowerCase() !== requestedAnswer.toLowerCase()
)
return;
const embed = message.embeds[0];
const description =
embed?.title ??
(embed?.description &&
cleanContent(embed?.description, message.channel)) ??
(embed?.image?.url ? embed?.image?.url : message.content);
const author = await getUserFromSuggestion(message);
if (requestedUser && author?.id !== requestedUser?.id) return;
return {
answer: answer.name,
author,
count,
url: message.url,
title: truncateText(description, 100),
};
}),
)
)
.filter(
(suggestion): suggestion is NonNullable<typeof suggestion> =>
suggestion !== undefined,
)
.sort(
(suggestionOne, suggestionTwo) =>
(suggestionTwo?.count ?? 0) - (suggestionOne?.count ?? 0),
);
const nick =
requestedUser &&
(await guild.members.fetch(requestedUser.id).catch(() => {}))?.displayName;
await paginate(
all,
(suggestion) =>
`**${suggestion.count}** ${
suggestion.count > 0
? SUGGESTION_EMOJIS[0]?.[0]
: SUGGESTION_EMOJIS[0]?.[1]
} [${escapeLinks(suggestion.title)}](${suggestion.url} "${
suggestion.answer
}")${
suggestion.author && !requestedUser
? ` by ${suggestion.author.toString()}`
: ""
}`,
"No suggestions found. Try changing any filters you may have used.",
`Top suggestions${requestedUser ? ` by ${nick}` : ""}${
requestedAnswer ? ` answered with ${requestedAnswer}` : ""
}`,
(data) => interaction.editReply(data),
);
}
}
},
};
export default info;