blob: 08fcc77386eff33d537dfdedc451bbda652740d5 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { callback as roleMenu } from "../actions/roleMenu.js";
pineafan73a7c4a2022-07-24 10:38:04 +01002import 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";
pineafan63fc5e22022-08-04 22:04:10 +01007import { autocomplete as tagAutocomplete } from "../commands/tag.js";
pineafanad54d752022-04-18 19:01:43 +01008
pineafan63fc5e22022-08-04 22:04:10 +01009export const event = "interactionCreate";
pineafanad54d752022-04-18 19:01:43 +010010
pineafan02ba0232022-07-24 22:16:15 +010011
12function getAutocomplete(typed: string, options: string[]): object[] {
pineafan63fc5e22022-08-04 22:04:10 +010013 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}));
pineafan02ba0232022-07-24 22:16:15 +010017}
18
pineafan02ba0232022-07-24 22:16:15 +010019function generateStatsChannelAutocomplete(typed) {
pineafan63fc5e22022-08-04 22:04:10 +010020 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);
pineafan41d93562022-07-30 22:10:15 +010026}
27function generateWelcomeMessageAutocomplete(typed) {
pineafan63fc5e22022-08-04 22:04:10 +010028 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);
pineafan02ba0232022-07-24 22:16:15 +010034}
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) {
pineafan63fc5e22022-08-04 22:04:10 +010039 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); }
pineafan02ba0232022-07-24 22:16:15 +010044 }
pineafan02ba0232022-07-24 22:16:15 +010045 } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
46 switch (`${interaction.commandName} ${interaction.options.getSubcommandGroup(false)} ${interaction.options.getSubcommand(false)}`) {
pineafan63fc5e22022-08-04 22:04:10 +010047 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"))); }
pineafan02ba0232022-07-24 22:16:15 +010050 }
pineafanad54d752022-04-18 19:01:43 +010051 }
52}
53
54export async function callback(client, interaction) {
pineafan63fc5e22022-08-04 22:04:10 +010055 await interactionCreate(interaction);
pineafanad54d752022-04-18 19:01:43 +010056}