blob: dc224e8c85acd1a756e42ce445118633704549a0 [file] [log] [blame]
pineafanbd02b4a2022-08-05 22:01:38 +01001import { 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";
pineafan813bdf42022-07-24 10:39:10 +01007
8export async function callback(interaction) {
pineafan63fc5e22022-08-04 22:04:10 +01009 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010010 if (!config.roleMenu.enabled)
11 return await interaction.reply({
12 embeds: [
13 new EmojiEmbed()
14 .setTitle("Roles")
15 .setDescription(
16 "Self roles are currently disabled. Please contact a staff member or try again later."
17 )
18 .setStatus("Danger")
19 .setEmoji("CONTROL.BLOCKCROSS")
20 ],
21 ephemeral: true
22 });
23 if (config.roleMenu.options.length === 0)
24 return await interaction.reply({
25 embeds: [
26 new EmojiEmbed()
27 .setTitle("Roles")
28 .setDescription(
29 "There are no roles available. Please contact a staff member or try again later."
30 )
31 .setStatus("Danger")
32 .setEmoji("CONTROL.BLOCKCROSS")
33 ],
34 ephemeral: true
35 });
36 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan813bdf42022-07-24 10:39:10 +010037 let m;
38 if (config.roleMenu.allowWebUI) {
pineafan63fc5e22022-08-04 22:04:10 +010039 let code = "";
40 let length = 5;
41 let itt = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +010042 const chars =
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
pineafan63fc5e22022-08-04 22:04:10 +010044 let valid = false;
45 while (!valid) {
46 itt += 1;
47 code = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010048 for (let i = 0; i < length; i++) {
49 code += chars.charAt(Math.floor(Math.random() * chars.length));
50 }
pineafan813bdf42022-07-24 10:39:10 +010051 if (code in client.roleMenu) continue;
52 if (itt > 1000) {
pineafan63fc5e22022-08-04 22:04:10 +010053 itt = 0;
54 length += 1;
pineafan813bdf42022-07-24 10:39:10 +010055 continue;
56 }
pineafan63fc5e22022-08-04 22:04:10 +010057 valid = true;
pineafan813bdf42022-07-24 10:39:10 +010058 }
59 client.roleMenu[code] = {
60 guild: interaction.guild.id,
61 guildName: interaction.guild.name,
Skyler Grey75ea9172022-08-06 10:22:23 +010062 guildIcon: interaction.guild.iconURL({ format: "png" }),
pineafan813bdf42022-07-24 10:39:10 +010063 user: interaction.member.user.id,
64 username: interaction.member.user.username,
65 data: config.roleMenu.options,
66 interaction: interaction
67 };
pineafan63fc5e22022-08-04 22:04:10 +010068 let up = true;
pineafan813bdf42022-07-24 10:39:10 +010069 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 const status = await fetch(client.config.baseUrl).then(
71 (res) => res.status
72 );
pineafan63fc5e22022-08-04 22:04:10 +010073 if (status !== 200) up = false;
Skyler Grey75ea9172022-08-06 10:22:23 +010074 } catch {
75 up = false;
76 }
pineafan813bdf42022-07-24 10:39:10 +010077 m = await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +010078 embeds: [
79 new EmojiEmbed()
80 .setTitle("Roles")
81 .setDescription("Select how to choose your roles")
82 .setStatus("Success")
83 .setEmoji("GUILD.GREEN")
84 ],
85 components: [
86 new MessageActionRow().addComponents([
87 new MessageButton()
88 .setLabel("Online")
89 .setStyle("LINK")
90 .setDisabled(!up)
91 .setURL(
92 `${client.config.baseUrl}nucleus/rolemenu?code=${code}`
93 ),
94 new MessageButton()
95 .setLabel("Manual")
96 .setStyle("PRIMARY")
97 .setCustomId("manual")
98 ])
99 ]
pineafan63fc5e22022-08-04 22:04:10 +0100100 });
pineafan813bdf42022-07-24 10:39:10 +0100101 }
102 let component;
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 try {
104 component = await m.awaitMessageComponent({ time: 300000 });
105 } catch (e) {
106 return;
107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108 component.deferUpdate();
109 let rolesToAdd = [];
pineafan813bdf42022-07-24 10:39:10 +0100110 for (let i = 0; i < config.roleMenu.options.length; i++) {
pineafan63fc5e22022-08-04 22:04:10 +0100111 const object = config.roleMenu.options[i];
112 const m = await interaction.editReply({
pineafan813bdf42022-07-24 10:39:10 +0100113 embeds: [
114 new EmojiEmbed()
115 .setTitle("Roles")
116 .setEmoji("GUILD.GREEN")
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 .setDescription(
118 `**${object.name}**` +
119 (object.description
120 ? `\n${object.description}`
121 : "") +
122 `\n\nSelect ${object.min}` +
123 (object.min !== object.max
124 ? ` to ${object.max}`
125 : "") +
126 ` role${object.max === 1 ? "" : "s"} to add.`
127 )
pineafan813bdf42022-07-24 10:39:10 +0100128 .setStatus("Success")
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 .setFooter({
130 text: `Step ${i + 1}/${config.roleMenu.options.length}`
131 })
pineafan813bdf42022-07-24 10:39:10 +0100132 ],
133 components: [
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 new MessageActionRow().addComponents(
135 [
136 new MessageButton()
137 .setLabel("Cancel")
138 .setStyle("DANGER")
139 .setCustomId("cancel")
140 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
141 ].concat(
142 object.min === 0
143 ? [
144 new MessageButton()
145 .setLabel("Skip")
146 .setStyle("SECONDARY")
147 .setCustomId("skip")
148 .setEmoji(
149 getEmojiByName("CONTROL.RIGHT", "id")
150 )
151 ]
152 : []
153 )
154 )
155 ].concat([
pineafan813bdf42022-07-24 10:39:10 +0100156 new MessageActionRow().addComponents([
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 new MessageSelectMenu()
158 .setPlaceholder(`${object.name}`)
159 .setCustomId("rolemenu")
160 .setMinValues(object.min)
161 .setMaxValues(object.max)
162 .setOptions(
163 object.options.map((o) => {
164 return {
165 label: o.name,
166 description: o.description,
167 value: o.role
168 };
169 })
170 )
171 ])
172 ])
pineafan813bdf42022-07-24 10:39:10 +0100173 });
174 let component;
175 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 component = await m.awaitMessageComponent({ time: 300000 });
pineafan813bdf42022-07-24 10:39:10 +0100177 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100178 return;
pineafan813bdf42022-07-24 10:39:10 +0100179 }
pineafan63fc5e22022-08-04 22:04:10 +0100180 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100181 if (component.customId === "rolemenu") {
pineafan63fc5e22022-08-04 22:04:10 +0100182 rolesToAdd = rolesToAdd.concat(component.values);
pineafane23c4ec2022-07-27 21:56:27 +0100183 } else if (component.customId === "cancel") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 return await interaction.editReply({
185 embeds: [
186 new EmojiEmbed()
187 .setTitle("Roles")
188 .setDescription("Cancelled. No changes were made.")
189 .setStatus("Danger")
190 .setEmoji("GUILD.RED")
191 ],
192 components: []
193 });
pineafan813bdf42022-07-24 10:39:10 +0100194 }
195 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 let rolesToRemove = config.roleMenu.options
197 .map((o) => o.options.map((o) => o.role))
198 .flat();
199 const memberRoles = interaction.member.roles.cache.map((r) => r.id);
200 rolesToRemove = rolesToRemove
201 .filter((r) => memberRoles.includes(r))
202 .filter((r) => !rolesToAdd.includes(r));
203 rolesToAdd = rolesToAdd.filter((r) => !memberRoles.includes(r));
pineafan813bdf42022-07-24 10:39:10 +0100204 try {
pineafan63fc5e22022-08-04 22:04:10 +0100205 await interaction.member.roles.remove(rolesToRemove);
206 await interaction.member.roles.add(rolesToAdd);
pineafan813bdf42022-07-24 10:39:10 +0100207 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100208 return await interaction.reply({
209 embeds: [
210 new EmojiEmbed()
211 .setTitle("Roles")
212 .setDescription(
213 "Something went wrong and your roles were not added. Please contact a staff member or try again later."
214 )
215 .setStatus("Danger")
216 .setEmoji("GUILD.RED")
217 ],
218 components: []
219 });
pineafan813bdf42022-07-24 10:39:10 +0100220 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100221 await interaction.editReply({
222 embeds: [
223 new EmojiEmbed()
224 .setTitle("Roles")
225 .setDescription(
226 "Roles have been added. You may close this message."
227 )
228 .setStatus("Success")
229 .setEmoji("GUILD.GREEN")
230 ],
231 components: []
232 });
pineafan63fc5e22022-08-04 22:04:10 +0100233 return;
pineafan813bdf42022-07-24 10:39:10 +0100234}