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"; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 6 | import { LoadingEmbed } from "../utils/defaultEmbeds.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 7 | |
| 8 | export 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}) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 22 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true}) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 23 | 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); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 53 | if (status !== 200) up = false |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 54 | } 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) |
pineafan | 6d53741 | 2022-07-28 12:35:33 +0100 | [diff] [blame^] | 66 | .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`), |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 67 | 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}` : ``) + |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 87 | `\n\nSelect ${object.min}` + (object.min !== object.max ? ` to ${object.max}` : ``) + ` role${object.max === 1 ? '' : 's'} to add.`) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 88 | .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")) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 98 | ].concat(object.min === 0 ? [ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 99 | 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() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 120 | if (component.customId === "rolemenu") { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 121 | rolesToAdd = rolesToAdd.concat(component.values) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 122 | } else if (component.customId === "cancel") { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 123 | 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 | } |