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); |
| 23 | return fuse |
| 24 | .slice(0, 25) |
| 25 | .map((option) => ({ name: option.item, value: option.item })); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 26 | } |
| 27 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 28 | function generateStatsChannelAutocomplete(typed) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | const validReplacements = [ |
| 30 | "serverName", |
| 31 | "memberCount", |
| 32 | "memberCount:bots", |
| 33 | "memberCount:humans" |
| 34 | ]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | const autocompletions = []; |
| 36 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | if (beforeLastOpenBracket !== null) { |
| 38 | for (const replacement of validReplacements) { |
| 39 | autocompletions.push( |
| 40 | `${beforeLastOpenBracket[1]} {${replacement}}` |
| 41 | ); |
| 42 | } |
| 43 | } else { |
| 44 | for (const replacement of validReplacements) { |
| 45 | autocompletions.push(`${typed} {${replacement}}`); |
| 46 | } |
| 47 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | return getAutocomplete(typed, autocompletions); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 49 | } |
| 50 | function generateWelcomeMessageAutocomplete(typed) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 51 | const validReplacements = [ |
| 52 | "serverName", |
| 53 | "memberCount", |
| 54 | "memberCount:bots", |
| 55 | "memberCount:humans", |
| 56 | "member:mention", |
| 57 | "member:name" |
| 58 | ]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | const autocompletions = []; |
| 60 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | if (beforeLastOpenBracket !== null) { |
| 62 | for (const replacement of validReplacements) { |
| 63 | autocompletions.push( |
| 64 | `${beforeLastOpenBracket[1]} {${replacement}}` |
| 65 | ); |
| 66 | } |
| 67 | } else { |
| 68 | for (const replacement of validReplacements) { |
| 69 | autocompletions.push(`${typed} {${replacement}}`); |
| 70 | } |
| 71 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | return getAutocomplete(typed, autocompletions); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 73 | } |
| 74 | |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 75 | async function interactionCreate(interaction) { |
| 76 | if (interaction.componentType === "BUTTON") { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 77 | switch (interaction.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | case "rolemenu": { |
| 79 | return await roleMenu(interaction); |
| 80 | } |
| 81 | case "verifybutton": { |
| 82 | return verify(interaction); |
| 83 | } |
| 84 | case "createticket": { |
| 85 | return create(interaction); |
| 86 | } |
| 87 | case "closeticket": { |
| 88 | return close(interaction); |
| 89 | } |
| 90 | case "createtranscript": { |
| 91 | return createTranscript(interaction); |
| 92 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 93 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 94 | } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | switch ( |
| 96 | `${ |
| 97 | interaction.commandName |
| 98 | } ${interaction.options.getSubcommandGroup( |
| 99 | false |
| 100 | )} ${interaction.options.getSubcommand(false)}` |
| 101 | ) { |
| 102 | case "tag null null": { |
| 103 | return interaction.respond( |
| 104 | getAutocomplete( |
| 105 | interaction.options.getString("tag"), |
| 106 | await tagAutocomplete(interaction) |
| 107 | ) |
| 108 | ); |
| 109 | } |
| 110 | case "settings null stats": { |
| 111 | return interaction.respond( |
| 112 | generateStatsChannelAutocomplete( |
| 113 | interaction.options.getString("name") |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | case "settings null welcome": { |
| 118 | return interaction.respond( |
| 119 | generateWelcomeMessageAutocomplete( |
| 120 | interaction.options.getString("message") |
| 121 | ) |
| 122 | ); |
| 123 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 124 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | export async function callback(client, interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 129 | await interactionCreate(interaction); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | } |