pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 1 | import { callback as roleMenu } from "../actions/roleMenu.js" |
| 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"; |
| 7 | import { autocomplete as tagAutocomplete } from "../commands/tag.js" |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 8 | |
| 9 | export const event = 'interactionCreate'; |
| 10 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 11 | |
| 12 | function getAutocomplete(typed: string, options: string[]): object[] { |
| 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})) |
| 17 | } |
| 18 | |
| 19 | const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"] |
| 20 | function generateStatsChannelAutocomplete(typed) { |
| 21 | let autocompletions = [] |
| 22 | const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/) |
| 23 | if (beforeLastOpenBracket !== null) { for (let replacement of validReplacements) { autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`) } } |
| 24 | else { for (let replacement of validReplacements) { autocompletions.push(`${typed} {${replacement}}`) } } |
| 25 | return getAutocomplete(typed, autocompletions) |
| 26 | } |
| 27 | |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 28 | async function interactionCreate(interaction) { |
| 29 | if (interaction.componentType === "BUTTON") { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 30 | switch (interaction.customId) { |
| 31 | case "rolemenu": { return await roleMenu(interaction) } |
| 32 | case "verifybutton": { return verify(interaction) } |
| 33 | case "createticket": { return create(interaction) } |
| 34 | case "closeticket": { return close(interaction) } |
| 35 | case "createtranscript": { return createTranscript(interaction) } |
| 36 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 37 | } else if (interaction.componentType === "MESSAGE_COMPONENT") { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 38 | } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") { |
| 39 | switch (`${interaction.commandName} ${interaction.options.getSubcommandGroup(false)} ${interaction.options.getSubcommand(false)}`) { |
| 40 | case `tag null null`: { return interaction.respond(getAutocomplete(interaction.options.getString("tag"), (await tagAutocomplete(interaction)))) } |
| 41 | case `settings stats set`: { return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name"))) } |
| 42 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | export async function callback(client, interaction) { |
| 47 | await interactionCreate(interaction) |
| 48 | } |