blob: eaf4feaf2fdff974a1875f5fc9f5f6aa74c52076 [file] [log] [blame]
pineafan73a7c4a2022-07-24 10:38:04 +01001import { callback as roleMenu } from "../actions/roleMenu.js"
2import verify from "../reflex/verify.js";
3import create from "../actions/tickets/create.js";
4import close from "../actions/tickets/delete.js";
5import createTranscript from "../premium/createTranscript.js";
pineafan02ba0232022-07-24 22:16:15 +01006import Fuse from "fuse.js";
7import { autocomplete as tagAutocomplete } from "../commands/tag.js"
pineafanad54d752022-04-18 19:01:43 +01008
9export const event = 'interactionCreate';
10
pineafan02ba0232022-07-24 22:16:15 +010011
12function 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
19const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"]
20function 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
pineafanad54d752022-04-18 19:01:43 +010028async function interactionCreate(interaction) {
29 if (interaction.componentType === "BUTTON") {
pineafan02ba0232022-07-24 22:16:15 +010030 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 }
pineafanad54d752022-04-18 19:01:43 +010037 } else if (interaction.componentType === "MESSAGE_COMPONENT") {
pineafan02ba0232022-07-24 22:16:15 +010038 } 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)))) }
pineafan0bc04162022-07-25 17:22:26 +010041 case `settings null stats`: { return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name"))) }
pineafan02ba0232022-07-24 22:16:15 +010042 }
pineafanad54d752022-04-18 19:01:43 +010043 }
44}
45
46export async function callback(client, interaction) {
47 await interactionCreate(interaction)
48}