blob: e82c82c3ed31b9a713dc2e039be6e6b0e1f72ce2 [file] [log] [blame]
pineafanad54d752022-04-18 19:01:43 +01001import { Message, MessageButton } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01002import EmojiEmbed from '../utils/generateEmojiEmbed.js'
pineafanad54d752022-04-18 19:01:43 +01003import { MessageActionRow, MessageSelectMenu } from 'discord.js';
4import getEmojiByName from "../utils/getEmojiByName.js";
pineafan6702cef2022-06-13 17:52:37 +01005import client from "../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +01006
7export async function callback(interaction) {
pineafan4edb7762022-06-26 19:21:04 +01008 let config = await client.database.guilds.read(interaction.guild.id);
9 if (!config.roleMenu.enabled) return await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010010 .setTitle("Roles")
11 .setDescription("Self roles are currently disabled. Please contact a staff member or try again later.")
12 .setStatus("Danger")
13 .setEmoji("CONTROL.BLOCKCROSS")
14 ], ephemeral: true})
pineafan4edb7762022-06-26 19:21:04 +010015 if (config.roleMenu.options.length === 0) return await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010016 .setTitle("Roles")
17 .setDescription("There are no roles available. Please contact a staff member or try again later.")
18 .setStatus("Danger")
19 .setEmoji("CONTROL.BLOCKCROSS")
20 ], ephemeral: true})
pineafan4edb7762022-06-26 19:21:04 +010021 await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010022 .setTitle("Roles")
23 .setDescription("Loading...")
24 .setStatus("Success")
25 .setEmoji("NUCLEUS.LOADING")
26 ], ephemeral: true})
27 let m;
28 if (config.roleMenu.allowWebUI) {
29 let code = ""
30 let length = 5
31 let itt = 0
32 const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
33 while (true) {
34 itt += 1
35 code = ""
36 for (let i = 0; i < length; i++) { code += chars.charAt(Math.floor(Math.random() * chars.length)); }
PineappleFanb3dd83c2022-06-17 10:53:48 +010037 if (code in client.roleMenu) continue;
pineafanad54d752022-04-18 19:01:43 +010038 if (itt > 1000) {
39 itt = 0
40 length += 1
41 continue;
42 }
43 break;
44 }
PineappleFanb3dd83c2022-06-17 10:53:48 +010045 client.roleMenu[code] = {
pineafanad54d752022-04-18 19:01:43 +010046 guild: interaction.guild.id,
47 guildIcon: interaction.guild.iconURL({format: "png"}),
48 user: interaction.member.user.id,
49 interaction: interaction
50 };
51 m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +010052 embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010053 .setTitle("Roles")
54 .setDescription("Select how to choose your roles")
55 .setStatus("Success")
56 .setEmoji("GUILD.GREEN")
57 ], components: [new MessageActionRow().addComponents([
58 new MessageButton()
59 .setLabel("Online")
60 .setStyle("LINK")
pineafane625d782022-05-09 18:04:32 +010061 .setURL(`https://clicks.codes/nuclues/rolemenu?code=${code}`),
pineafanad54d752022-04-18 19:01:43 +010062 new MessageButton()
63 .setLabel("Manual")
64 .setStyle("PRIMARY")
65 .setCustomId("manual")
66 ])]
67 })
68 }
69 let component;
pineafanc6158ab2022-06-17 16:34:07 +010070 try { component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +010071 } catch (e) { return }
72 component.deferUpdate()
73 let rolesToAdd = []
74 for (let i = 0; i < config.roleMenu.options.length; i++) {
75 let object = config.roleMenu.options[i];
76 let m = await interaction.editReply({
77 embeds: [
pineafan4edb7762022-06-26 19:21:04 +010078 new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010079 .setTitle("Roles")
80 .setEmoji("GUILD.GREEN")
81 .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : ``) +
82 `\n\nSelect ${object.min}` + (object.min != object.max ? ` to ${object.max}` : ``) + ` role${object.max == 1 ? '' : 's'} to add.`)
83 .setStatus("Success")
84 .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`})
85 ],
86 components: [
87 new MessageActionRow().addComponents([
88 new MessageButton()
89 .setLabel("Cancel")
90 .setStyle("DANGER")
91 .setCustomId("cancel")
92 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
93 ].concat(object.min == 0 ? [
94 new MessageButton()
95 .setLabel("Skip")
96 .setStyle("SECONDARY")
97 .setCustomId("skip")
98 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
99 ] : []))
100 ].concat([new MessageActionRow().addComponents([new MessageSelectMenu()
101 .setPlaceholder(`${object.name}`)
102 .setCustomId("rolemenu")
103 .setMinValues(object.min)
104 .setMaxValues(object.max)
105 .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role} }))
106 ])])
107 });
108 let component;
109 try {
pineafanc6158ab2022-06-17 16:34:07 +0100110 component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +0100111 } catch (e) {
112 return
113 }
114 component.deferUpdate()
115 if (component.customId == "rolemenu") {
116 rolesToAdd = rolesToAdd.concat(component.values)
117 } else if (component.customId == "cancel") {
pineafan4edb7762022-06-26 19:21:04 +0100118 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100119 .setTitle("Roles")
120 .setDescription("Cancelled. No changes were made.")
121 .setStatus("Danger")
122 .setEmoji("GUILD.RED")
123 ], components: []})
124 }
125 }
126 let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat()
127 let memberRoles = interaction.member.roles.cache.map(r => r.id)
128 rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r))
129 rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r))
130 try {
131 await interaction.member.roles.remove(rolesToRemove)
132 await interaction.member.roles.add(rolesToAdd)
133 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +0100134 return await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100135 .setTitle("Roles")
136 .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.")
137 .setStatus("Danger")
138 .setEmoji("GUILD.RED")
pineafan6702cef2022-06-13 17:52:37 +0100139 ], components: []})
pineafanad54d752022-04-18 19:01:43 +0100140 }
pineafan4edb7762022-06-26 19:21:04 +0100141 await interaction.editReply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100142 .setTitle("Roles")
143 .setDescription("Roles have been added. You may close this message.")
144 .setStatus("Success")
145 .setEmoji("GUILD.GREEN")
146 ], components: []})
147 return
148}