blob: 9ee7beeae10f2676c7f14c5a3a94ba7d9d5fbb89 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { callback as roleMenu } from "../actions/roleMenu.js";
pineafan73a7c4a2022-07-24 10:38:04 +01002import verify from "../reflex/verify.js";
3import create from "../actions/tickets/create.js";
4import close from "../actions/tickets/delete.js";
5import createTranscript from "../premium/createTranscript.js";
pineafan02ba0232022-07-24 22:16:15 +01006import Fuse from "fuse.js";
pineafan63fc5e22022-08-04 22:04:10 +01007import { autocomplete as tagAutocomplete } from "../commands/tag.js";
pineafan0f5cc782022-08-12 21:55:42 +01008import type { AutocompleteInteraction, Interaction, MessageComponentInteraction } from "discord.js";
9// @ts-expect-error
10import type { HaikuClient } from "jshaiku";
pineafanad54d752022-04-18 19:01:43 +010011
pineafan63fc5e22022-08-04 22:04:10 +010012export const event = "interactionCreate";
pineafanad54d752022-04-18 19:01:43 +010013
pineafan0f5cc782022-08-12 21:55:42 +010014function getAutocomplete(typed: string, options: string[]): {name: string, value: string}[] {
Skyler Grey75ea9172022-08-06 10:22:23 +010015 options = options.filter((option) => option.length <= 100); // thanks discord. 6000 character limit on slash command inputs but only 100 for autocomplete.
16 if (!typed)
17 return options
18 .slice(0, 25)
19 .sort()
20 .map((option) => ({ name: option, value: option }));
21 const fuse = new Fuse(options, {
22 useExtendedSearch: true,
23 findAllMatches: true,
24 minMatchCharLength: 0
25 }).search(typed);
Skyler Grey11236ba2022-08-08 21:13:33 +010026 return fuse.slice(0, 25).map((option) => ({ name: option.item, value: option.item }));
pineafan02ba0232022-07-24 22:16:15 +010027}
28
pineafan0f5cc782022-08-12 21:55:42 +010029function generateStatsChannelAutocomplete(typed: string) {
Skyler Grey11236ba2022-08-08 21:13:33 +010030 const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"];
pineafan63fc5e22022-08-04 22:04:10 +010031 const autocompletions = [];
32 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010033 if (beforeLastOpenBracket !== null) {
34 for (const replacement of validReplacements) {
Skyler Grey11236ba2022-08-08 21:13:33 +010035 autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010036 }
37 } else {
38 for (const replacement of validReplacements) {
39 autocompletions.push(`${typed} {${replacement}}`);
40 }
41 }
pineafan63fc5e22022-08-04 22:04:10 +010042 return getAutocomplete(typed, autocompletions);
pineafan41d93562022-07-30 22:10:15 +010043}
pineafan0f5cc782022-08-12 21:55:42 +010044function generateWelcomeMessageAutocomplete(typed: string) {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 const validReplacements = [
46 "serverName",
47 "memberCount",
48 "memberCount:bots",
49 "memberCount:humans",
50 "member:mention",
51 "member:name"
52 ];
pineafan63fc5e22022-08-04 22:04:10 +010053 const autocompletions = [];
54 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010055 if (beforeLastOpenBracket !== null) {
56 for (const replacement of validReplacements) {
Skyler Grey11236ba2022-08-08 21:13:33 +010057 autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010058 }
59 } else {
60 for (const replacement of validReplacements) {
61 autocompletions.push(`${typed} {${replacement}}`);
62 }
63 }
pineafan63fc5e22022-08-04 22:04:10 +010064 return getAutocomplete(typed, autocompletions);
pineafan02ba0232022-07-24 22:16:15 +010065}
66
pineafan0f5cc782022-08-12 21:55:42 +010067async function interactionCreate(interaction: Interaction) {
68 if (interaction.type === "MESSAGE_COMPONENT" && (interaction as MessageComponentInteraction).componentType === "BUTTON") {
69 const int = (interaction as MessageComponentInteraction)
70 switch (int.customId) {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 case "rolemenu": {
72 return await roleMenu(interaction);
73 }
74 case "verifybutton": {
pineafan0f5cc782022-08-12 21:55:42 +010075 return verify(int);
Skyler Grey75ea9172022-08-06 10:22:23 +010076 }
77 case "createticket": {
78 return create(interaction);
79 }
80 case "closeticket": {
81 return close(interaction);
82 }
83 case "createtranscript": {
pineafan0f5cc782022-08-12 21:55:42 +010084 return createTranscript(int);
Skyler Grey75ea9172022-08-06 10:22:23 +010085 }
pineafan02ba0232022-07-24 22:16:15 +010086 }
pineafan02ba0232022-07-24 22:16:15 +010087 } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
pineafan0f5cc782022-08-12 21:55:42 +010088 const int = (interaction as AutocompleteInteraction)
Skyler Grey75ea9172022-08-06 10:22:23 +010089 switch (
pineafan0f5cc782022-08-12 21:55:42 +010090 `${int.commandName} ${int.options.getSubcommandGroup(
Skyler Grey75ea9172022-08-06 10:22:23 +010091 false
pineafan0f5cc782022-08-12 21:55:42 +010092 )} ${int.options.getSubcommand(false)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010093 ) {
94 case "tag null null": {
pineafan0f5cc782022-08-12 21:55:42 +010095 return int.respond(
96 getAutocomplete(int.options.getString("tag") ?? "", await tagAutocomplete(int))
Skyler Grey75ea9172022-08-06 10:22:23 +010097 );
98 }
99 case "settings null stats": {
pineafan0f5cc782022-08-12 21:55:42 +0100100 return int.respond(generateStatsChannelAutocomplete(int.options.getString("name") ?? ""));
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 }
102 case "settings null welcome": {
pineafan0f5cc782022-08-12 21:55:42 +0100103 return int.respond(
104 generateWelcomeMessageAutocomplete(int.options.getString("message") ?? "")
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 );
106 }
pineafan02ba0232022-07-24 22:16:15 +0100107 }
pineafanad54d752022-04-18 19:01:43 +0100108 }
109}
110
pineafan0f5cc782022-08-12 21:55:42 +0100111export async function callback(_client: HaikuClient, interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100112 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100113}