blob: 9a8e80a4044c03acdca243ac1cb4a0333b76b28a [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);
pineafan813bdf42022-07-24 10:39:10 +010010 if (!config.roleMenu.enabled) return await interaction.reply({embeds: [new EmojiEmbed()
11 .setTitle("Roles")
12 .setDescription("Self roles are currently disabled. Please contact a staff member or try again later.")
13 .setStatus("Danger")
14 .setEmoji("CONTROL.BLOCKCROSS")
pineafan63fc5e22022-08-04 22:04:10 +010015 ], ephemeral: true});
pineafan813bdf42022-07-24 10:39:10 +010016 if (config.roleMenu.options.length === 0) return await interaction.reply({embeds: [new EmojiEmbed()
17 .setTitle("Roles")
18 .setDescription("There are no roles available. Please contact a staff member or try again later.")
19 .setStatus("Danger")
20 .setEmoji("CONTROL.BLOCKCROSS")
pineafan63fc5e22022-08-04 22:04:10 +010021 ], ephemeral: true});
22 await interaction.reply({embeds: LoadingEmbed, ephemeral: true});
pineafan813bdf42022-07-24 10:39:10 +010023 let m;
24 if (config.roleMenu.allowWebUI) {
pineafan63fc5e22022-08-04 22:04:10 +010025 let code = "";
26 let length = 5;
27 let itt = 0;
pineafan813bdf42022-07-24 10:39:10 +010028 const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
pineafan63fc5e22022-08-04 22:04:10 +010029 let valid = false;
30 while (!valid) {
31 itt += 1;
32 code = "";
pineafan813bdf42022-07-24 10:39:10 +010033 for (let i = 0; i < length; i++) { code += chars.charAt(Math.floor(Math.random() * chars.length)); }
34 if (code in client.roleMenu) continue;
35 if (itt > 1000) {
pineafan63fc5e22022-08-04 22:04:10 +010036 itt = 0;
37 length += 1;
pineafan813bdf42022-07-24 10:39:10 +010038 continue;
39 }
pineafan63fc5e22022-08-04 22:04:10 +010040 valid = true;
pineafan813bdf42022-07-24 10:39:10 +010041 }
42 client.roleMenu[code] = {
43 guild: interaction.guild.id,
44 guildName: interaction.guild.name,
45 guildIcon: interaction.guild.iconURL({format: "png"}),
46 user: interaction.member.user.id,
47 username: interaction.member.user.username,
48 data: config.roleMenu.options,
49 interaction: interaction
50 };
pineafan63fc5e22022-08-04 22:04:10 +010051 let up = true;
pineafan813bdf42022-07-24 10:39:10 +010052 try {
pineafan63fc5e22022-08-04 22:04:10 +010053 const status = await fetch(client.config.baseUrl).then(res => res.status);
54 if (status !== 200) up = false;
55 } catch { up = false; }
pineafan813bdf42022-07-24 10:39:10 +010056 m = await interaction.editReply({
57 embeds: [new EmojiEmbed()
58 .setTitle("Roles")
59 .setDescription("Select how to choose your roles")
60 .setStatus("Success")
61 .setEmoji("GUILD.GREEN")
62 ], components: [new MessageActionRow().addComponents([
63 new MessageButton()
64 .setLabel("Online")
65 .setStyle("LINK")
66 .setDisabled(!up)
pineafan6d537412022-07-28 12:35:33 +010067 .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`),
pineafan813bdf42022-07-24 10:39:10 +010068 new MessageButton()
69 .setLabel("Manual")
70 .setStyle("PRIMARY")
71 .setCustomId("manual")
72 ])]
pineafan63fc5e22022-08-04 22:04:10 +010073 });
pineafan813bdf42022-07-24 10:39:10 +010074 }
75 let component;
pineafanbd02b4a2022-08-05 22:01:38 +010076 try { component = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +010077 } catch (e) { return; }
78 component.deferUpdate();
79 let rolesToAdd = [];
pineafan813bdf42022-07-24 10:39:10 +010080 for (let i = 0; i < config.roleMenu.options.length; i++) {
pineafan63fc5e22022-08-04 22:04:10 +010081 const object = config.roleMenu.options[i];
82 const m = await interaction.editReply({
pineafan813bdf42022-07-24 10:39:10 +010083 embeds: [
84 new EmojiEmbed()
85 .setTitle("Roles")
86 .setEmoji("GUILD.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010087 .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : "") +
88 `\n\nSelect ${object.min}` + (object.min !== object.max ? ` to ${object.max}` : "") + ` role${object.max === 1 ? "" : "s"} to add.`)
pineafan813bdf42022-07-24 10:39:10 +010089 .setStatus("Success")
90 .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`})
91 ],
92 components: [
93 new MessageActionRow().addComponents([
94 new MessageButton()
95 .setLabel("Cancel")
96 .setStyle("DANGER")
97 .setCustomId("cancel")
98 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
pineafan63fc5e22022-08-04 22:04:10 +010099 ].concat(object.min === 0 ? [
100 new MessageButton()
101 .setLabel("Skip")
102 .setStyle("SECONDARY")
103 .setCustomId("skip")
104 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
105 ] : []))
pineafan813bdf42022-07-24 10:39:10 +0100106 ].concat([new MessageActionRow().addComponents([new MessageSelectMenu()
107 .setPlaceholder(`${object.name}`)
108 .setCustomId("rolemenu")
109 .setMinValues(object.min)
110 .setMaxValues(object.max)
pineafan63fc5e22022-08-04 22:04:10 +0100111 .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role}; }))
pineafan813bdf42022-07-24 10:39:10 +0100112 ])])
113 });
114 let component;
115 try {
pineafanbd02b4a2022-08-05 22:01:38 +0100116 component = await m.awaitMessageComponent({time: 300000});
pineafan813bdf42022-07-24 10:39:10 +0100117 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100118 return;
pineafan813bdf42022-07-24 10:39:10 +0100119 }
pineafan63fc5e22022-08-04 22:04:10 +0100120 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100121 if (component.customId === "rolemenu") {
pineafan63fc5e22022-08-04 22:04:10 +0100122 rolesToAdd = rolesToAdd.concat(component.values);
pineafane23c4ec2022-07-27 21:56:27 +0100123 } else if (component.customId === "cancel") {
pineafan813bdf42022-07-24 10:39:10 +0100124 return await interaction.editReply({embeds: [new EmojiEmbed()
125 .setTitle("Roles")
126 .setDescription("Cancelled. No changes were made.")
127 .setStatus("Danger")
128 .setEmoji("GUILD.RED")
pineafan63fc5e22022-08-04 22:04:10 +0100129 ], components: []});
pineafan813bdf42022-07-24 10:39:10 +0100130 }
131 }
pineafan63fc5e22022-08-04 22:04:10 +0100132 let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat();
133 const memberRoles = interaction.member.roles.cache.map(r => r.id);
134 rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r));
135 rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r));
pineafan813bdf42022-07-24 10:39:10 +0100136 try {
pineafan63fc5e22022-08-04 22:04:10 +0100137 await interaction.member.roles.remove(rolesToRemove);
138 await interaction.member.roles.add(rolesToAdd);
pineafan813bdf42022-07-24 10:39:10 +0100139 } catch (e) {
140 return await interaction.reply({embeds: [new EmojiEmbed()
141 .setTitle("Roles")
142 .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.")
143 .setStatus("Danger")
144 .setEmoji("GUILD.RED")
pineafan63fc5e22022-08-04 22:04:10 +0100145 ], components: []});
pineafan813bdf42022-07-24 10:39:10 +0100146 }
147 await interaction.editReply({embeds: [new EmojiEmbed()
148 .setTitle("Roles")
149 .setDescription("Roles have been added. You may close this message.")
150 .setStatus("Success")
151 .setEmoji("GUILD.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100152 ], components: []});
153 return;
pineafan813bdf42022-07-24 10:39:10 +0100154}