blob: 5b0aff19eb2284e520c982580b66ba7cd945f75e [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 +010011function getAutocomplete(typed: string, options: string[]): object[] {
Skyler Grey75ea9172022-08-06 10:22:23 +010012 options = options.filter((option) => option.length <= 100); // thanks discord. 6000 character limit on slash command inputs but only 100 for autocomplete.
13 if (!typed)
14 return options
15 .slice(0, 25)
16 .sort()
17 .map((option) => ({ name: option, value: option }));
18 const fuse = new Fuse(options, {
19 useExtendedSearch: true,
20 findAllMatches: true,
21 minMatchCharLength: 0
22 }).search(typed);
Skyler Grey11236ba2022-08-08 21:13:33 +010023 return fuse.slice(0, 25).map((option) => ({ name: option.item, value: option.item }));
pineafan02ba0232022-07-24 22:16:15 +010024}
25
pineafan02ba0232022-07-24 22:16:15 +010026function generateStatsChannelAutocomplete(typed) {
Skyler Grey11236ba2022-08-08 21:13:33 +010027 const validReplacements = ["serverName", "memberCount", "memberCount:bots", "memberCount:humans"];
pineafan63fc5e22022-08-04 22:04:10 +010028 const autocompletions = [];
29 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (beforeLastOpenBracket !== null) {
31 for (const replacement of validReplacements) {
Skyler Grey11236ba2022-08-08 21:13:33 +010032 autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010033 }
34 } else {
35 for (const replacement of validReplacements) {
36 autocompletions.push(`${typed} {${replacement}}`);
37 }
38 }
pineafan63fc5e22022-08-04 22:04:10 +010039 return getAutocomplete(typed, autocompletions);
pineafan41d93562022-07-30 22:10:15 +010040}
41function generateWelcomeMessageAutocomplete(typed) {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 const validReplacements = [
43 "serverName",
44 "memberCount",
45 "memberCount:bots",
46 "memberCount:humans",
47 "member:mention",
48 "member:name"
49 ];
pineafan63fc5e22022-08-04 22:04:10 +010050 const autocompletions = [];
51 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010052 if (beforeLastOpenBracket !== null) {
53 for (const replacement of validReplacements) {
Skyler Grey11236ba2022-08-08 21:13:33 +010054 autocompletions.push(`${beforeLastOpenBracket[1]} {${replacement}}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010055 }
56 } else {
57 for (const replacement of validReplacements) {
58 autocompletions.push(`${typed} {${replacement}}`);
59 }
60 }
pineafan63fc5e22022-08-04 22:04:10 +010061 return getAutocomplete(typed, autocompletions);
pineafan02ba0232022-07-24 22:16:15 +010062}
63
pineafanad54d752022-04-18 19:01:43 +010064async function interactionCreate(interaction) {
65 if (interaction.componentType === "BUTTON") {
pineafan02ba0232022-07-24 22:16:15 +010066 switch (interaction.customId) {
Skyler Grey75ea9172022-08-06 10:22:23 +010067 case "rolemenu": {
68 return await roleMenu(interaction);
69 }
70 case "verifybutton": {
71 return verify(interaction);
72 }
73 case "createticket": {
74 return create(interaction);
75 }
76 case "closeticket": {
77 return close(interaction);
78 }
79 case "createtranscript": {
80 return createTranscript(interaction);
81 }
pineafan02ba0232022-07-24 22:16:15 +010082 }
pineafan02ba0232022-07-24 22:16:15 +010083 } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
Skyler Grey75ea9172022-08-06 10:22:23 +010084 switch (
Skyler Grey11236ba2022-08-08 21:13:33 +010085 `${interaction.commandName} ${interaction.options.getSubcommandGroup(
Skyler Grey75ea9172022-08-06 10:22:23 +010086 false
87 )} ${interaction.options.getSubcommand(false)}`
88 ) {
89 case "tag null null": {
90 return interaction.respond(
Skyler Grey11236ba2022-08-08 21:13:33 +010091 getAutocomplete(interaction.options.getString("tag"), await tagAutocomplete(interaction))
Skyler Grey75ea9172022-08-06 10:22:23 +010092 );
93 }
94 case "settings null stats": {
Skyler Grey11236ba2022-08-08 21:13:33 +010095 return interaction.respond(generateStatsChannelAutocomplete(interaction.options.getString("name")));
Skyler Grey75ea9172022-08-06 10:22:23 +010096 }
97 case "settings null welcome": {
98 return interaction.respond(
Skyler Grey11236ba2022-08-08 21:13:33 +010099 generateWelcomeMessageAutocomplete(interaction.options.getString("message"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 );
101 }
pineafan02ba0232022-07-24 22:16:15 +0100102 }
pineafanad54d752022-04-18 19:01:43 +0100103 }
104}
105
106export async function callback(client, interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100107 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100108}