pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 1 | import { Message, MessageButton } from "discord.js"; |
| 2 | import EmojiEmbed from '../utils/generateEmojiEmbed.js' |
| 3 | import { MessageActionRow, MessageSelectMenu } from 'discord.js'; |
| 4 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 5 | import client from "../utils/client.js"; |
| 6 | |
| 7 | export async function callback(interaction) { |
| 8 | let config = await client.database.guilds.read(interaction.guild.id); |
| 9 | if (!config.roleMenu.enabled) return await interaction.reply({embeds: [new EmojiEmbed() |
| 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) return await interaction.reply({embeds: [new EmojiEmbed() |
| 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 EmojiEmbed() |
| 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 client.roleMenu) continue; |
| 38 | if (itt > 1000) { |
| 39 | itt = 0 |
| 40 | length += 1 |
| 41 | continue; |
| 42 | } |
| 43 | break; |
| 44 | } |
| 45 | client.roleMenu[code] = { |
| 46 | guild: interaction.guild.id, |
| 47 | guildName: interaction.guild.name, |
| 48 | guildIcon: interaction.guild.iconURL({format: "png"}), |
| 49 | user: interaction.member.user.id, |
| 50 | username: interaction.member.user.username, |
| 51 | data: config.roleMenu.options, |
| 52 | interaction: interaction |
| 53 | }; |
| 54 | let up = true |
| 55 | try { |
| 56 | let status = await fetch(client.config.baseUrl).then(res => res.status); |
| 57 | if (status != 200) up = false |
| 58 | } catch { up = false } |
| 59 | m = await interaction.editReply({ |
| 60 | embeds: [new EmojiEmbed() |
| 61 | .setTitle("Roles") |
| 62 | .setDescription("Select how to choose your roles") |
| 63 | .setStatus("Success") |
| 64 | .setEmoji("GUILD.GREEN") |
| 65 | ], components: [new MessageActionRow().addComponents([ |
| 66 | new MessageButton() |
| 67 | .setLabel("Online") |
| 68 | .setStyle("LINK") |
| 69 | .setDisabled(!up) |
| 70 | .setURL(`${client.config.baseUrl}/nucleus/rolemenu?code=${code}`), |
| 71 | new MessageButton() |
| 72 | .setLabel("Manual") |
| 73 | .setStyle("PRIMARY") |
| 74 | .setCustomId("manual") |
| 75 | ])] |
| 76 | }) |
| 77 | } |
| 78 | let component; |
| 79 | try { component = await (m as Message).awaitMessageComponent({time: 300000}); |
| 80 | } catch (e) { return } |
| 81 | component.deferUpdate() |
| 82 | let rolesToAdd = [] |
| 83 | for (let i = 0; i < config.roleMenu.options.length; i++) { |
| 84 | let object = config.roleMenu.options[i]; |
| 85 | let m = await interaction.editReply({ |
| 86 | embeds: [ |
| 87 | new EmojiEmbed() |
| 88 | .setTitle("Roles") |
| 89 | .setEmoji("GUILD.GREEN") |
| 90 | .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : ``) + |
| 91 | `\n\nSelect ${object.min}` + (object.min != object.max ? ` to ${object.max}` : ``) + ` role${object.max == 1 ? '' : 's'} to add.`) |
| 92 | .setStatus("Success") |
| 93 | .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`}) |
| 94 | ], |
| 95 | components: [ |
| 96 | new MessageActionRow().addComponents([ |
| 97 | new MessageButton() |
| 98 | .setLabel("Cancel") |
| 99 | .setStyle("DANGER") |
| 100 | .setCustomId("cancel") |
| 101 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 102 | ].concat(object.min == 0 ? [ |
| 103 | new MessageButton() |
| 104 | .setLabel("Skip") |
| 105 | .setStyle("SECONDARY") |
| 106 | .setCustomId("skip") |
| 107 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 108 | ] : [])) |
| 109 | ].concat([new MessageActionRow().addComponents([new MessageSelectMenu() |
| 110 | .setPlaceholder(`${object.name}`) |
| 111 | .setCustomId("rolemenu") |
| 112 | .setMinValues(object.min) |
| 113 | .setMaxValues(object.max) |
| 114 | .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role} })) |
| 115 | ])]) |
| 116 | }); |
| 117 | let component; |
| 118 | try { |
| 119 | component = await (m as Message).awaitMessageComponent({time: 300000}); |
| 120 | } catch (e) { |
| 121 | return |
| 122 | } |
| 123 | component.deferUpdate() |
| 124 | if (component.customId == "rolemenu") { |
| 125 | rolesToAdd = rolesToAdd.concat(component.values) |
| 126 | } else if (component.customId == "cancel") { |
| 127 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 128 | .setTitle("Roles") |
| 129 | .setDescription("Cancelled. No changes were made.") |
| 130 | .setStatus("Danger") |
| 131 | .setEmoji("GUILD.RED") |
| 132 | ], components: []}) |
| 133 | } |
| 134 | } |
| 135 | let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat() |
| 136 | let memberRoles = interaction.member.roles.cache.map(r => r.id) |
| 137 | rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r)) |
| 138 | rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r)) |
| 139 | try { |
| 140 | await interaction.member.roles.remove(rolesToRemove) |
| 141 | await interaction.member.roles.add(rolesToAdd) |
| 142 | } catch (e) { |
| 143 | return await interaction.reply({embeds: [new EmojiEmbed() |
| 144 | .setTitle("Roles") |
| 145 | .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.") |
| 146 | .setStatus("Danger") |
| 147 | .setEmoji("GUILD.RED") |
| 148 | ], components: []}) |
| 149 | } |
| 150 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 151 | .setTitle("Roles") |
| 152 | .setDescription("Roles have been added. You may close this message.") |
| 153 | .setStatus("Success") |
| 154 | .setEmoji("GUILD.GREEN") |
| 155 | ], components: []}) |
| 156 | return |
| 157 | } |