pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 1 | import { Message, MessageButton } from "discord.js"; |
| 2 | import readConfig from '../utils/readConfig.js' |
| 3 | import generateEmojiEmbed from '../utils/generateEmojiEmbed.js' |
| 4 | import { MessageActionRow, MessageSelectMenu } from 'discord.js'; |
| 5 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 6 | |
| 7 | export async function callback(interaction) { |
| 8 | let config = await readConfig(interaction.guild.id); |
| 9 | if (!config.roleMenu.enabled) await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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}) |
| 15 | if (config.roleMenu.options.length === 0) await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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}) |
| 21 | await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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)); } |
| 37 | if (code in interaction.client.roleMenu) continue; |
| 38 | if (itt > 1000) { |
| 39 | itt = 0 |
| 40 | length += 1 |
| 41 | continue; |
| 42 | } |
| 43 | break; |
| 44 | } |
| 45 | interaction.client.roleMenu[code] = { |
| 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({ |
| 52 | embeds: [new generateEmojiEmbed() |
| 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; |
| 70 | try { component = await (m as Message).awaitMessageComponent({time: 2.5 * 60 * 1000}); |
| 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: [ |
| 78 | new generateEmojiEmbed() |
| 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 { |
| 110 | component = await (m as Message).awaitMessageComponent({time: 2.5 * 60 * 1000}); |
| 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") { |
| 118 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 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) { |
| 134 | return await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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") |
| 139 | ]}) |
| 140 | } |
| 141 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 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 | } |