blob: 60b90e323edb3df37a2fcc94cb030717048093d0 [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);
23 return fuse
24 .slice(0, 25)
25 .map((option) => ({ name: option.item, value: option.item }));
pineafan02ba0232022-07-24 22:16:15 +010026}
27
pineafan02ba0232022-07-24 22:16:15 +010028function generateStatsChannelAutocomplete(typed) {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 const validReplacements = [
30 "serverName",
31 "memberCount",
32 "memberCount:bots",
33 "memberCount:humans"
34 ];
pineafan63fc5e22022-08-04 22:04:10 +010035 const autocompletions = [];
36 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010037 if (beforeLastOpenBracket !== null) {
38 for (const replacement of validReplacements) {
39 autocompletions.push(
40 `${beforeLastOpenBracket[1]} {${replacement}}`
41 );
42 }
43 } else {
44 for (const replacement of validReplacements) {
45 autocompletions.push(`${typed} {${replacement}}`);
46 }
47 }
pineafan63fc5e22022-08-04 22:04:10 +010048 return getAutocomplete(typed, autocompletions);
pineafan41d93562022-07-30 22:10:15 +010049}
50function generateWelcomeMessageAutocomplete(typed) {
Skyler Grey75ea9172022-08-06 10:22:23 +010051 const validReplacements = [
52 "serverName",
53 "memberCount",
54 "memberCount:bots",
55 "memberCount:humans",
56 "member:mention",
57 "member:name"
58 ];
pineafan63fc5e22022-08-04 22:04:10 +010059 const autocompletions = [];
60 const beforeLastOpenBracket = typed.match(/(.*){[^{}]{0,15}$/);
Skyler Grey75ea9172022-08-06 10:22:23 +010061 if (beforeLastOpenBracket !== null) {
62 for (const replacement of validReplacements) {
63 autocompletions.push(
64 `${beforeLastOpenBracket[1]} {${replacement}}`
65 );
66 }
67 } else {
68 for (const replacement of validReplacements) {
69 autocompletions.push(`${typed} {${replacement}}`);
70 }
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 return getAutocomplete(typed, autocompletions);
pineafan02ba0232022-07-24 22:16:15 +010073}
74
pineafanad54d752022-04-18 19:01:43 +010075async function interactionCreate(interaction) {
76 if (interaction.componentType === "BUTTON") {
pineafan02ba0232022-07-24 22:16:15 +010077 switch (interaction.customId) {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 case "rolemenu": {
79 return await roleMenu(interaction);
80 }
81 case "verifybutton": {
82 return verify(interaction);
83 }
84 case "createticket": {
85 return create(interaction);
86 }
87 case "closeticket": {
88 return close(interaction);
89 }
90 case "createtranscript": {
91 return createTranscript(interaction);
92 }
pineafan02ba0232022-07-24 22:16:15 +010093 }
pineafan02ba0232022-07-24 22:16:15 +010094 } else if (interaction.type === "APPLICATION_COMMAND_AUTOCOMPLETE") {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 switch (
96 `${
97 interaction.commandName
98 } ${interaction.options.getSubcommandGroup(
99 false
100 )} ${interaction.options.getSubcommand(false)}`
101 ) {
102 case "tag null null": {
103 return interaction.respond(
104 getAutocomplete(
105 interaction.options.getString("tag"),
106 await tagAutocomplete(interaction)
107 )
108 );
109 }
110 case "settings null stats": {
111 return interaction.respond(
112 generateStatsChannelAutocomplete(
113 interaction.options.getString("name")
114 )
115 );
116 }
117 case "settings null welcome": {
118 return interaction.respond(
119 generateWelcomeMessageAutocomplete(
120 interaction.options.getString("message")
121 )
122 );
123 }
pineafan02ba0232022-07-24 22:16:15 +0100124 }
pineafanad54d752022-04-18 19:01:43 +0100125 }
126}
127
128export async function callback(client, interaction) {
pineafan63fc5e22022-08-04 22:04:10 +0100129 await interactionCreate(interaction);
Skyler Grey75ea9172022-08-06 10:22:23 +0100130}