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, |
| 47 | guildIcon: interaction.guild.iconURL({format: "png"}), |
| 48 | user: interaction.member.user.id, |
| 49 | interaction: interaction |
| 50 | }; |
| 51 | m = await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 52 | embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 53 | .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") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 61 | .setURL(`https://clicks.codes/nuclues/rolemenu?code=${code}`), |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 62 | new MessageButton() |
| 63 | .setLabel("Manual") |
| 64 | .setStyle("PRIMARY") |
| 65 | .setCustomId("manual") |
| 66 | ])] |
| 67 | }) |
| 68 | } |
| 69 | let component; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 70 | try { component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 71 | } 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: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 78 | new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 79 | .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 { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 110 | component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 111 | } 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") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 118 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 119 | .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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 134 | return await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 135 | .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") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 139 | ], components: []}) |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 140 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 141 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 142 | .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 | } |