pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 1 | import { Message, MessageButton } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 2 | import EmojiEmbed from '../utils/generateEmojiEmbed.js' |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 3 | import { MessageActionRow, MessageSelectMenu } from 'discord.js'; |
| 4 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 5 | import client from "../utils/client.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 6 | |
| 7 | export async function callback(interaction) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 8 | let config = await client.database.guilds.read(interaction.guild.id); |
| 9 | if (!config.roleMenu.enabled) return await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 10 | .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}) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 15 | if (config.roleMenu.options.length === 0) return await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 16 | .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}) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 21 | await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 22 | .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)); } |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 37 | if (code in client.roleMenu) continue; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 38 | if (itt > 1000) { |
| 39 | itt = 0 |
| 40 | length += 1 |
| 41 | continue; |
| 42 | } |
| 43 | break; |
| 44 | } |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 45 | client.roleMenu[code] = { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 46 | guild: interaction.guild.id, |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 47 | guildName: interaction.guild.name, |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 48 | guildIcon: interaction.guild.iconURL({format: "png"}), |
| 49 | user: interaction.member.user.id, |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 50 | username: interaction.member.user.username, |
| 51 | data: config.roleMenu.options, |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 52 | interaction: interaction |
| 53 | }; |
| 54 | m = await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 55 | embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 56 | .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") |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 64 | .setDisabled(false) // TODO check if the server is up |
| 65 | .setURL(`${client.config.baseUrl}/nucleus/rolemenu?code=${code}`), |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 66 | new MessageButton() |
| 67 | .setLabel("Manual") |
| 68 | .setStyle("PRIMARY") |
| 69 | .setCustomId("manual") |
| 70 | ])] |
| 71 | }) |
| 72 | } |
| 73 | let component; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 74 | try { component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 75 | } 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: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 82 | new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 83 | .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 { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 114 | component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 115 | } 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") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 122 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 123 | .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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 138 | return await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 139 | .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") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 143 | ], components: []}) |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 144 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 145 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 146 | .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 | } |