blob: a4bb8c38baf9cc22bb91ec3a18ed618a0bf7fefd [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
pineafan02ba0232022-07-24 22:16:15 +010019function generateStatsChannelAutocomplete(typed) {
pineafan41d93562022-07-30 22:10:15 +010020 const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"]
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}
27function generateWelcomeMessageAutocomplete(typed) {
28 const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans", "member:mention", "member:name"]
pineafan02ba0232022-07-24 22:16:15 +010029 let autocompletions = []
30 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/)
31 if (beforeLastOpenBracket !== null) { for (let replacement of validReplacements) { autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`) } }
32 else { for (let replacement of validReplacements) { autocompletions.push(`${typed} {${replacement}}`) } }
33 return getAutocomplete(typed, autocompletions)
34}
35
pineafanad54d752022-04-18 19:01:43 +010036async function interactionCreate(interaction) {
37 if (interaction.componentType === "BUTTON") {
pineafan02ba0232022-07-24 22:16:15 +010038 switch (interaction.customId) {
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) }
44 }
pineafanad54d752022-04-18 19:01:43 +010045 } else if (interaction.componentType === "MESSAGE_COMPONENT") {
pineafan02ba0232022-07-24 22:16:15 +010046 } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
47 switch (`${interaction.commandName} ${interaction.options.getSubcommandGroup(false)} ${interaction.options.getSubcommand(false)}`) {
48 case `tag null null`: { return interaction.respond(getAutocomplete(interaction.options.getString("tag"), (await tagAutocomplete(interaction)))) }
pineafan0bc04162022-07-25 17:22:26 +010049 case `settings null stats`: { return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name"))) }
pineafan41d93562022-07-30 22:10:15 +010050 case `settings null welcome`: { return interaction.respond(generateWelcomeMessageAutocomplete(interaction.options.getString("message"))) }
pineafan02ba0232022-07-24 22:16:15 +010051 }
pineafanad54d752022-04-18 19:01:43 +010052 }
53}
54
55export async function callback(client, interaction) {
56 await interactionCreate(interaction)
57}