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 | |
| 12 | function getAutocomplete(typed: string, options: string[]): object[] { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 13 | options = options.filter(option => option.length <= 100); // thanks discord. 6000 character limit on slash command inputs but only 100 for autocomplete. |
| 14 | if (!typed) return options.slice(0, 25).sort().map(option => ({name: option, value: option})); |
| 15 | const fuse = new Fuse(options, {useExtendedSearch: true, findAllMatches: true, minMatchCharLength: 0}).search(typed); |
| 16 | return fuse.slice(0, 25).map(option => ({name: option.item, value: option.item})); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 17 | } |
| 18 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 19 | function generateStatsChannelAutocomplete(typed) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 20 | const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"]; |
| 21 | const autocompletions = []; |
| 22 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
| 23 | if (beforeLastOpenBracket !== null) { for (const replacement of validReplacements) { autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`); } } |
| 24 | else { for (const replacement of validReplacements) { autocompletions.push(`${typed} {${replacement}}`); } } |
| 25 | return getAutocomplete(typed, autocompletions); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 26 | } |
| 27 | function generateWelcomeMessageAutocomplete(typed) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 28 | const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans", "member:mention", "member:name"]; |
| 29 | const autocompletions = []; |
| 30 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/); |
| 31 | if (beforeLastOpenBracket !== null) { for (const replacement of validReplacements) { autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`); } } |
| 32 | else { for (const replacement of validReplacements) { autocompletions.push(`${typed} {${replacement}}`); } } |
| 33 | return getAutocomplete(typed, autocompletions); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 34 | } |
| 35 | |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 36 | async function interactionCreate(interaction) { |
| 37 | if (interaction.componentType === "BUTTON") { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 38 | switch (interaction.customId) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 39 | case "rolemenu": { return await roleMenu(interaction); } |
| 40 | case "verifybutton": { return verify(interaction); } |
| 41 | case "createticket": { return create(interaction); } |
| 42 | case "closeticket": { return close(interaction); } |
| 43 | case "createtranscript": { return createTranscript(interaction); } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 44 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 45 | } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") { |
| 46 | switch (`${interaction.commandName} ${interaction.options.getSubcommandGroup(false)} ${interaction.options.getSubcommand(false)}`) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 47 | case "tag null null": { return interaction.respond(getAutocomplete(interaction.options.getString("tag"), (await tagAutocomplete(interaction)))); } |
| 48 | case "settings null stats": { return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name"))); } |
| 49 | case "settings null welcome": { return interaction.respond(generateWelcomeMessageAutocomplete(interaction.options.getString("message"))); } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 50 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | export async function callback(client, interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 55 | await interactionCreate(interaction); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 56 | } |