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 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +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) { |
| 68 | if (interaction.type === "MESSAGE_COMPONENT" && (interaction as MessageComponentInteraction).componentType === "BUTTON") { |
| 69 | const int = (interaction as MessageComponentInteraction) |
| 70 | switch (int.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | case "rolemenu": { |
| 72 | return await roleMenu(interaction); |
| 73 | } |
| 74 | case "verifybutton": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 75 | return verify(int); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 76 | } |
| 77 | case "createticket": { |
| 78 | return create(interaction); |
| 79 | } |
| 80 | case "closeticket": { |
| 81 | return close(interaction); |
| 82 | } |
| 83 | case "createtranscript": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 84 | return createTranscript(int); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 86 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 87 | } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 88 | const int = (interaction as AutocompleteInteraction) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | switch ( |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 90 | `${int.commandName} ${int.options.getSubcommandGroup( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | false |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 92 | )} ${int.options.getSubcommand(false)}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | ) { |
| 94 | case "tag null null": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 95 | return int.respond( |
| 96 | getAutocomplete(int.options.getString("tag") ?? "", await tagAutocomplete(int)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | ); |
| 98 | } |
| 99 | case "settings null stats": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 100 | return int.respond(generateStatsChannelAutocomplete(int.options.getString("name") ?? "")); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | } |
| 102 | case "settings null welcome": { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 103 | return int.respond( |
| 104 | generateWelcomeMessageAutocomplete(int.options.getString("message") ?? "") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | ); |
| 106 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 107 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 111 | export async function callback(_client: HaikuClient, interaction: Interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | await interactionCreate(interaction); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | } |