blob: 318a0ddc340d513e3597b64910fc70017170c1dd [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,
pineafanda6e5342022-07-03 10:03:16 +010047 guildName: interaction.guild.name,
pineafanad54d752022-04-18 19:01:43 +010048 guildIcon: interaction.guild.iconURL({format: "png"}),
49 user: interaction.member.user.id,
pineafanda6e5342022-07-03 10:03:16 +010050 username: interaction.member.user.username,
51 data: config.roleMenu.options,
pineafanad54d752022-04-18 19:01:43 +010052 interaction: interaction
53 };
54 m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +010055 embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010056 .setTitle("Roles")
57 .setDescription("Select how to choose your roles")
58 .setStatus("Success")
59 .setEmoji("GUILD.GREEN")
60 ], components: [new MessageActionRow().addComponents([
61 new MessageButton()
62 .setLabel("Online")
63 .setStyle("LINK")
pineafanda6e5342022-07-03 10:03:16 +010064 .setDisabled(false) // TODO check if the server is up
65 .setURL(`${client.config.baseUrl}/nucleus/rolemenu?code=${code}`),
pineafanad54d752022-04-18 19:01:43 +010066 new MessageButton()
67 .setLabel("Manual")
68 .setStyle("PRIMARY")
69 .setCustomId("manual")
70 ])]
71 })
72 }
73 let component;
pineafanc6158ab2022-06-17 16:34:07 +010074 try { component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +010075 } catch (e) { return }
76 component.deferUpdate()
77 let rolesToAdd = []
78 for (let i = 0; i < config.roleMenu.options.length; i++) {
79 let object = config.roleMenu.options[i];
80 let m = await interaction.editReply({
81 embeds: [
pineafan4edb7762022-06-26 19:21:04 +010082 new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010083 .setTitle("Roles")
84 .setEmoji("GUILD.GREEN")
85 .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : ``) +
86 `\n\nSelect ${object.min}` + (object.min != object.max ? ` to ${object.max}` : ``) + ` role${object.max == 1 ? '' : 's'} to add.`)
87 .setStatus("Success")
88 .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`})
89 ],
90 components: [
91 new MessageActionRow().addComponents([
92 new MessageButton()
93 .setLabel("Cancel")
94 .setStyle("DANGER")
95 .setCustomId("cancel")
96 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
97 ].concat(object.min == 0 ? [
98 new MessageButton()
99 .setLabel("Skip")
100 .setStyle("SECONDARY")
101 .setCustomId("skip")
102 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
103 ] : []))
104 ].concat([new MessageActionRow().addComponents([new MessageSelectMenu()
105 .setPlaceholder(`${object.name}`)
106 .setCustomId("rolemenu")
107 .setMinValues(object.min)
108 .setMaxValues(object.max)
109 .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role} }))
110 ])])
111 });
112 let component;
113 try {
pineafanc6158ab2022-06-17 16:34:07 +0100114 component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +0100115 } catch (e) {
116 return
117 }
118 component.deferUpdate()
119 if (component.customId == "rolemenu") {
120 rolesToAdd = rolesToAdd.concat(component.values)
121 } else if (component.customId == "cancel") {
pineafan4edb7762022-06-26 19:21:04 +0100122 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100123 .setTitle("Roles")
124 .setDescription("Cancelled. No changes were made.")
125 .setStatus("Danger")
126 .setEmoji("GUILD.RED")
127 ], components: []})
128 }
129 }
130 let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat()
131 let memberRoles = interaction.member.roles.cache.map(r => r.id)
132 rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r))
133 rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r))
134 try {
135 await interaction.member.roles.remove(rolesToRemove)
136 await interaction.member.roles.add(rolesToAdd)
137 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +0100138 return await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100139 .setTitle("Roles")
140 .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.")
141 .setStatus("Danger")
142 .setEmoji("GUILD.RED")
pineafan6702cef2022-06-13 17:52:37 +0100143 ], components: []})
pineafanad54d752022-04-18 19:01:43 +0100144 }
pineafan4edb7762022-06-26 19:21:04 +0100145 await interaction.editReply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100146 .setTitle("Roles")
147 .setDescription("Roles have been added. You may close this message.")
148 .setStatus("Success")
149 .setEmoji("GUILD.GREEN")
150 ], components: []})
151 return
152}