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