blob: 21e97ec56bb12bf15d94332bf22b739112ec5959 [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import { Message, MessageButton } from "discord.js";
2import EmojiEmbed from '../utils/generateEmojiEmbed.js'
3import { MessageActionRow, MessageSelectMenu } from 'discord.js';
4import 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) {
9 let config = await client.database.guilds.read(interaction.guild.id);
10 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")
15 ], ephemeral: true})
16 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")
21 ], ephemeral: true})
pineafane23c4ec2022-07-27 21:56:27 +010022 await interaction.reply({embeds: LoadingEmbed, ephemeral: true})
pineafan813bdf42022-07-24 10:39:10 +010023 let m;
24 if (config.roleMenu.allowWebUI) {
25 let code = ""
26 let length = 5
27 let itt = 0
28 const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
29 while (true) {
30 itt += 1
31 code = ""
32 for (let i = 0; i < length; i++) { code += chars.charAt(Math.floor(Math.random() * chars.length)); }
33 if (code in client.roleMenu) continue;
34 if (itt > 1000) {
35 itt = 0
36 length += 1
37 continue;
38 }
39 break;
40 }
41 client.roleMenu[code] = {
42 guild: interaction.guild.id,
43 guildName: interaction.guild.name,
44 guildIcon: interaction.guild.iconURL({format: "png"}),
45 user: interaction.member.user.id,
46 username: interaction.member.user.username,
47 data: config.roleMenu.options,
48 interaction: interaction
49 };
50 let up = true
51 try {
52 let status = await fetch(client.config.baseUrl).then(res => res.status);
pineafane23c4ec2022-07-27 21:56:27 +010053 if (status !== 200) up = false
pineafan813bdf42022-07-24 10:39:10 +010054 } catch { up = false }
55 m = await interaction.editReply({
56 embeds: [new EmojiEmbed()
57 .setTitle("Roles")
58 .setDescription("Select how to choose your roles")
59 .setStatus("Success")
60 .setEmoji("GUILD.GREEN")
61 ], components: [new MessageActionRow().addComponents([
62 new MessageButton()
63 .setLabel("Online")
64 .setStyle("LINK")
65 .setDisabled(!up)
pineafan6d537412022-07-28 12:35:33 +010066 .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`),
pineafan813bdf42022-07-24 10:39:10 +010067 new MessageButton()
68 .setLabel("Manual")
69 .setStyle("PRIMARY")
70 .setCustomId("manual")
71 ])]
72 })
73 }
74 let component;
75 try { component = await (m as Message).awaitMessageComponent({time: 300000});
76 } catch (e) { return }
77 component.deferUpdate()
78 let rolesToAdd = []
79 for (let i = 0; i < config.roleMenu.options.length; i++) {
80 let object = config.roleMenu.options[i];
81 let m = await interaction.editReply({
82 embeds: [
83 new EmojiEmbed()
84 .setTitle("Roles")
85 .setEmoji("GUILD.GREEN")
86 .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : ``) +
pineafane23c4ec2022-07-27 21:56:27 +010087 `\n\nSelect ${object.min}` + (object.min !== object.max ? ` to ${object.max}` : ``) + ` role${object.max === 1 ? '' : 's'} to add.`)
pineafan813bdf42022-07-24 10:39:10 +010088 .setStatus("Success")
89 .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`})
90 ],
91 components: [
92 new MessageActionRow().addComponents([
93 new MessageButton()
94 .setLabel("Cancel")
95 .setStyle("DANGER")
96 .setCustomId("cancel")
97 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
pineafane23c4ec2022-07-27 21:56:27 +010098 ].concat(object.min === 0 ? [
pineafan813bdf42022-07-24 10:39:10 +010099 new MessageButton()
100 .setLabel("Skip")
101 .setStyle("SECONDARY")
102 .setCustomId("skip")
103 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
104 ] : []))
105 ].concat([new MessageActionRow().addComponents([new MessageSelectMenu()
106 .setPlaceholder(`${object.name}`)
107 .setCustomId("rolemenu")
108 .setMinValues(object.min)
109 .setMaxValues(object.max)
110 .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role} }))
111 ])])
112 });
113 let component;
114 try {
115 component = await (m as Message).awaitMessageComponent({time: 300000});
116 } catch (e) {
117 return
118 }
119 component.deferUpdate()
pineafane23c4ec2022-07-27 21:56:27 +0100120 if (component.customId === "rolemenu") {
pineafan813bdf42022-07-24 10:39:10 +0100121 rolesToAdd = rolesToAdd.concat(component.values)
pineafane23c4ec2022-07-27 21:56:27 +0100122 } else if (component.customId === "cancel") {
pineafan813bdf42022-07-24 10:39:10 +0100123 return await interaction.editReply({embeds: [new EmojiEmbed()
124 .setTitle("Roles")
125 .setDescription("Cancelled. No changes were made.")
126 .setStatus("Danger")
127 .setEmoji("GUILD.RED")
128 ], components: []})
129 }
130 }
131 let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat()
132 let memberRoles = interaction.member.roles.cache.map(r => r.id)
133 rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r))
134 rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r))
135 try {
136 await interaction.member.roles.remove(rolesToRemove)
137 await interaction.member.roles.add(rolesToAdd)
138 } catch (e) {
139 return await interaction.reply({embeds: [new EmojiEmbed()
140 .setTitle("Roles")
141 .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.")
142 .setStatus("Danger")
143 .setEmoji("GUILD.RED")
144 ], components: []})
145 }
146 await interaction.editReply({embeds: [new EmojiEmbed()
147 .setTitle("Roles")
148 .setDescription("Roles have been added. You may close this message.")
149 .setStatus("Success")
150 .setEmoji("GUILD.GREEN")
151 ], components: []})
152 return
153}