blob: e7f1874a94a921df350f7f7a49f7bd021645a8ad [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import { Interaction, MessageButton } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import EmojiEmbed from "../utils/generateEmojiEmbed.js";
3import { MessageActionRow, MessageSelectMenu } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01004import getEmojiByName from "../utils/getEmojiByName.js";
5import client from "../utils/client.js";
pineafane23c4ec2022-07-27 21:56:27 +01006import { LoadingEmbed } from "../utils/defaultEmbeds.js";
Skyler Grey11236ba2022-08-08 21:13:33 +01007import type { GuildConfig } from "../utils/database.js";
pineafan813bdf42022-07-24 10:39:10 +01008
Skyler Grey11236ba2022-08-08 21:13:33 +01009export interface RoleMenuSchema {
10 guild: string;
11 guildName: string;
12 guildIcon: string;
13 user: string;
14 username: string;
15 data: GuildConfig["roleMenu"]["options"];
16 interaction: Interaction;
17}
18
19export async function callback(interaction: Interaction) {
pineafan63fc5e22022-08-04 22:04:10 +010020 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010021 if (!config.roleMenu.enabled)
22 return await interaction.reply({
23 embeds: [
24 new EmojiEmbed()
25 .setTitle("Roles")
26 .setDescription(
27 "Self roles are currently disabled. Please contact a staff member or try again later."
28 )
29 .setStatus("Danger")
30 .setEmoji("CONTROL.BLOCKCROSS")
31 ],
32 ephemeral: true
33 });
34 if (config.roleMenu.options.length === 0)
35 return await interaction.reply({
36 embeds: [
37 new EmojiEmbed()
38 .setTitle("Roles")
Skyler Grey11236ba2022-08-08 21:13:33 +010039 .setDescription("There are no roles available. Please contact a staff member or try again later.")
Skyler Grey75ea9172022-08-06 10:22:23 +010040 .setStatus("Danger")
41 .setEmoji("CONTROL.BLOCKCROSS")
42 ],
43 ephemeral: true
44 });
45 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan813bdf42022-07-24 10:39:10 +010046 let m;
47 if (config.roleMenu.allowWebUI) {
pineafan63fc5e22022-08-04 22:04:10 +010048 let code = "";
49 let length = 5;
50 let itt = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +010051 const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
pineafan63fc5e22022-08-04 22:04:10 +010052 let valid = false;
53 while (!valid) {
54 itt += 1;
55 code = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010056 for (let i = 0; i < length; i++) {
57 code += chars.charAt(Math.floor(Math.random() * chars.length));
58 }
pineafan813bdf42022-07-24 10:39:10 +010059 if (code in client.roleMenu) continue;
60 if (itt > 1000) {
pineafan63fc5e22022-08-04 22:04:10 +010061 itt = 0;
62 length += 1;
pineafan813bdf42022-07-24 10:39:10 +010063 continue;
64 }
pineafan63fc5e22022-08-04 22:04:10 +010065 valid = true;
pineafan813bdf42022-07-24 10:39:10 +010066 }
67 client.roleMenu[code] = {
68 guild: interaction.guild.id,
69 guildName: interaction.guild.name,
Skyler Grey75ea9172022-08-06 10:22:23 +010070 guildIcon: interaction.guild.iconURL({ format: "png" }),
pineafan813bdf42022-07-24 10:39:10 +010071 user: interaction.member.user.id,
72 username: interaction.member.user.username,
73 data: config.roleMenu.options,
74 interaction: interaction
75 };
pineafan63fc5e22022-08-04 22:04:10 +010076 let up = true;
Skyler Grey11236ba2022-08-08 21:13:33 +010077 up = false; // FIXME: Role menu does not yet exist, so the web UI is never up
78 /* try {
79 const status = await fetch(client.config.baseUrl).then((res) => res.status);
pineafan63fc5e22022-08-04 22:04:10 +010080 if (status !== 200) up = false;
Skyler Grey75ea9172022-08-06 10:22:23 +010081 } catch {
82 up = false;
Skyler Grey11236ba2022-08-08 21:13:33 +010083 }*/
pineafan813bdf42022-07-24 10:39:10 +010084 m = await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +010085 embeds: [
86 new EmojiEmbed()
87 .setTitle("Roles")
88 .setDescription("Select how to choose your roles")
89 .setStatus("Success")
90 .setEmoji("GUILD.GREEN")
91 ],
92 components: [
93 new MessageActionRow().addComponents([
94 new MessageButton()
95 .setLabel("Online")
96 .setStyle("LINK")
97 .setDisabled(!up)
Skyler Grey11236ba2022-08-08 21:13:33 +010098 .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`),
99 new MessageButton().setLabel("Manual").setStyle("PRIMARY").setCustomId("manual")
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 ])
101 ]
pineafan63fc5e22022-08-04 22:04:10 +0100102 });
pineafan813bdf42022-07-24 10:39:10 +0100103 }
104 let component;
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 try {
106 component = await m.awaitMessageComponent({ time: 300000 });
107 } catch (e) {
108 return;
109 }
pineafan63fc5e22022-08-04 22:04:10 +0100110 component.deferUpdate();
111 let rolesToAdd = [];
pineafan813bdf42022-07-24 10:39:10 +0100112 for (let i = 0; i < config.roleMenu.options.length; i++) {
pineafan63fc5e22022-08-04 22:04:10 +0100113 const object = config.roleMenu.options[i];
114 const m = await interaction.editReply({
pineafan813bdf42022-07-24 10:39:10 +0100115 embeds: [
116 new EmojiEmbed()
117 .setTitle("Roles")
118 .setEmoji("GUILD.GREEN")
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 .setDescription(
120 `**${object.name}**` +
Skyler Grey11236ba2022-08-08 21:13:33 +0100121 (object.description ? `\n${object.description}` : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 `\n\nSelect ${object.min}` +
Skyler Grey11236ba2022-08-08 21:13:33 +0100123 (object.min !== object.max ? ` to ${object.max}` : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 ` role${object.max === 1 ? "" : "s"} to add.`
125 )
pineafan813bdf42022-07-24 10:39:10 +0100126 .setStatus("Success")
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 .setFooter({
128 text: `Step ${i + 1}/${config.roleMenu.options.length}`
129 })
pineafan813bdf42022-07-24 10:39:10 +0100130 ],
131 components: [
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 new MessageActionRow().addComponents(
133 [
134 new MessageButton()
135 .setLabel("Cancel")
136 .setStyle("DANGER")
137 .setCustomId("cancel")
138 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
139 ].concat(
140 object.min === 0
141 ? [
142 new MessageButton()
143 .setLabel("Skip")
144 .setStyle("SECONDARY")
145 .setCustomId("skip")
Skyler Grey11236ba2022-08-08 21:13:33 +0100146 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 ]
148 : []
149 )
150 )
151 ].concat([
pineafan813bdf42022-07-24 10:39:10 +0100152 new MessageActionRow().addComponents([
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 new MessageSelectMenu()
154 .setPlaceholder(`${object.name}`)
155 .setCustomId("rolemenu")
156 .setMinValues(object.min)
157 .setMaxValues(object.max)
158 .setOptions(
159 object.options.map((o) => {
160 return {
161 label: o.name,
162 description: o.description,
163 value: o.role
164 };
165 })
166 )
167 ])
168 ])
pineafan813bdf42022-07-24 10:39:10 +0100169 });
170 let component;
171 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100172 component = await m.awaitMessageComponent({ time: 300000 });
pineafan813bdf42022-07-24 10:39:10 +0100173 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100174 return;
pineafan813bdf42022-07-24 10:39:10 +0100175 }
pineafan63fc5e22022-08-04 22:04:10 +0100176 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100177 if (component.customId === "rolemenu") {
pineafan63fc5e22022-08-04 22:04:10 +0100178 rolesToAdd = rolesToAdd.concat(component.values);
pineafane23c4ec2022-07-27 21:56:27 +0100179 } else if (component.customId === "cancel") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 return await interaction.editReply({
181 embeds: [
182 new EmojiEmbed()
183 .setTitle("Roles")
184 .setDescription("Cancelled. No changes were made.")
185 .setStatus("Danger")
186 .setEmoji("GUILD.RED")
187 ],
188 components: []
189 });
pineafan813bdf42022-07-24 10:39:10 +0100190 }
191 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100192 let rolesToRemove = config.roleMenu.options.map((o) => o.options.map((o) => o.role)).flat();
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 const memberRoles = interaction.member.roles.cache.map((r) => r.id);
Skyler Grey11236ba2022-08-08 21:13:33 +0100194 rolesToRemove = rolesToRemove.filter((r) => memberRoles.includes(r)).filter((r) => !rolesToAdd.includes(r));
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 rolesToAdd = rolesToAdd.filter((r) => !memberRoles.includes(r));
pineafan813bdf42022-07-24 10:39:10 +0100196 try {
pineafan63fc5e22022-08-04 22:04:10 +0100197 await interaction.member.roles.remove(rolesToRemove);
198 await interaction.member.roles.add(rolesToAdd);
pineafan813bdf42022-07-24 10:39:10 +0100199 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100200 return await interaction.reply({
201 embeds: [
202 new EmojiEmbed()
203 .setTitle("Roles")
204 .setDescription(
205 "Something went wrong and your roles were not added. Please contact a staff member or try again later."
206 )
207 .setStatus("Danger")
208 .setEmoji("GUILD.RED")
209 ],
210 components: []
211 });
pineafan813bdf42022-07-24 10:39:10 +0100212 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100213 await interaction.editReply({
214 embeds: [
215 new EmojiEmbed()
216 .setTitle("Roles")
Skyler Grey11236ba2022-08-08 21:13:33 +0100217 .setDescription("Roles have been added. You may close this message.")
Skyler Grey75ea9172022-08-06 10:22:23 +0100218 .setStatus("Success")
219 .setEmoji("GUILD.GREEN")
220 ],
221 components: []
222 });
pineafan63fc5e22022-08-04 22:04:10 +0100223 return;
pineafan813bdf42022-07-24 10:39:10 +0100224}