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