pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { callback as roleMenu } from "../actions/roleMenu.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 2 | import verify from "../reflex/verify.js"; |
| 3 | import create from "../actions/tickets/create.js"; |
| 4 | import close from "../actions/tickets/delete.js"; |
| 5 | import createTranscript from "../premium/createTranscript.js"; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 6 | import Fuse from "fuse.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | import { autocomplete as tagAutocomplete } from "../commands/tag.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 8 | import type { AutocompleteInteraction, Interaction, MessageComponentInteraction } from "discord.js"; |
| 9 | // @ts-expect-error |
| 10 | import type { HaikuClient } from "jshaiku"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 11 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | export const event = "interactionCreate"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 13 | |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame^] | 14 | function getAutocomplete(typed: string, options: string[]): { name: string; value: string }[] { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 26 | return fuse.slice(0, 25).map((option) => ({ name: option.item, value: option.item })); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 27 | } |
| 28 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 29 | function generateStatsChannelAutocomplete(typed: string) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 30 | const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | const autocompletions = []; |
| 32 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | if (beforeLastOpenBracket !== null) { |
| 34 | for (const replacement of validReplacements) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 35 | autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | } |
| 37 | } else { |
| 38 | for (const replacement of validReplacements) { |
| 39 | autocompletions.push(`${typed} {${replacement}}`); |
| 40 | } |
| 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | return getAutocomplete(typed, autocompletions); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 43 | } |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 44 | function generateWelcomeMessageAutocomplete(typed: string) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | const validReplacements = [ |
| 46 | "serverName", |
| 47 | "memberCount", |
| 48 | "memberCount:bots", |
| 49 | "memberCount:humans", |
| 50 | "member:mention", |
| 51 | "member:name" |
| 52 | ]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | const autocompletions = []; |
| 54 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | if (beforeLastOpenBracket !== null) { |
| 56 | for (const replacement of validReplacements) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 57 | autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | } |
| 59 | } else { |
| 60 | for (const replacement of validReplacements) { |
| 61 | autocompletions.push(`${typed} {${replacement}}`); |
| 62 | } |
| 63 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | return getAutocomplete(typed, autocompletions); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 65 | } |
| 66 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 67 | async function interactionCreate(interaction: Interaction) { |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame^] | 68 | if ( |
| 69 | interaction.type === "MESSAGE_COMPONENT" && |
| 70 | (interaction as MessageComponentInteraction).componentType === "BUTTON" |
| 71 | ) { |
| 72 | const int = interaction as MessageComponentInteraction; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 73 | switch (int.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | case "rolemenu": { |
| 75 | return await roleMenu(interaction); |
| 76 | } |
| 77 | case "verifybutton": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 78 | return verify(int); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | } |
| 80 | case "createticket": { |
| 81 | return create(interaction); |
| 82 | } |
| 83 | case "closeticket": { |
| 84 | return close(interaction); |
| 85 | } |
| 86 | case "createtranscript": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 87 | return createTranscript(int); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 89 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 90 | } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") { |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame^] | 91 | const int = interaction as AutocompleteInteraction; |
| 92 | switch (`${int.commandName} ${int.options.getSubcommandGroup(false)} ${int.options.getSubcommand(false)}`) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | case "tag null null": { |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame^] | 94 | return int.respond(getAutocomplete(int.options.getString("tag") ?? "", await tagAutocomplete(int))); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | } |
| 96 | case "settings null stats": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 97 | return int.respond(generateStatsChannelAutocomplete(int.options.getString("name") ?? "")); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | } |
| 99 | case "settings null welcome": { |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame^] | 100 | return int.respond(generateWelcomeMessageAutocomplete(int.options.getString("message") ?? "")); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 102 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 106 | export async function callback(_client: HaikuClient, interaction: Interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | await interactionCreate(interaction); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 108 | } |