pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 1 | import { MessageButton } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 3 | import { MessageActionRow, MessageSelectMenu } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 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) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 9 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 10 | if (!config.roleMenu.enabled) |
| 11 | return await interaction.reply({ |
| 12 | embeds: [ |
| 13 | new EmojiEmbed() |
| 14 | .setTitle("Roles") |
| 15 | .setDescription( |
| 16 | "Self roles are currently disabled. Please contact a staff member or try again later." |
| 17 | ) |
| 18 | .setStatus("Danger") |
| 19 | .setEmoji("CONTROL.BLOCKCROSS") |
| 20 | ], |
| 21 | ephemeral: true |
| 22 | }); |
| 23 | if (config.roleMenu.options.length === 0) |
| 24 | return await interaction.reply({ |
| 25 | embeds: [ |
| 26 | new EmojiEmbed() |
| 27 | .setTitle("Roles") |
| 28 | .setDescription( |
| 29 | "There are no roles available. Please contact a staff member or try again later." |
| 30 | ) |
| 31 | .setStatus("Danger") |
| 32 | .setEmoji("CONTROL.BLOCKCROSS") |
| 33 | ], |
| 34 | ephemeral: true |
| 35 | }); |
| 36 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 37 | let m; |
| 38 | if (config.roleMenu.allowWebUI) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | let code = ""; |
| 40 | let length = 5; |
| 41 | let itt = 0; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | const chars = |
| 43 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | let valid = false; |
| 45 | while (!valid) { |
| 46 | itt += 1; |
| 47 | code = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | for (let i = 0; i < length; i++) { |
| 49 | code += chars.charAt(Math.floor(Math.random() * chars.length)); |
| 50 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 51 | if (code in client.roleMenu) continue; |
| 52 | if (itt > 1000) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | itt = 0; |
| 54 | length += 1; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 55 | continue; |
| 56 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | valid = true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 58 | } |
| 59 | client.roleMenu[code] = { |
| 60 | guild: interaction.guild.id, |
| 61 | guildName: interaction.guild.name, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | guildIcon: interaction.guild.iconURL({ format: "png" }), |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 63 | user: interaction.member.user.id, |
| 64 | username: interaction.member.user.username, |
| 65 | data: config.roleMenu.options, |
| 66 | interaction: interaction |
| 67 | }; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | let up = true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 69 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | const status = await fetch(client.config.baseUrl).then( |
| 71 | (res) => res.status |
| 72 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | if (status !== 200) up = false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | } catch { |
| 75 | up = false; |
| 76 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 77 | m = await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | embeds: [ |
| 79 | new EmojiEmbed() |
| 80 | .setTitle("Roles") |
| 81 | .setDescription("Select how to choose your roles") |
| 82 | .setStatus("Success") |
| 83 | .setEmoji("GUILD.GREEN") |
| 84 | ], |
| 85 | components: [ |
| 86 | new MessageActionRow().addComponents([ |
| 87 | new MessageButton() |
| 88 | .setLabel("Online") |
| 89 | .setStyle("LINK") |
| 90 | .setDisabled(!up) |
| 91 | .setURL( |
| 92 | `${client.config.baseUrl}nucleus/rolemenu?code=${code}` |
| 93 | ), |
| 94 | new MessageButton() |
| 95 | .setLabel("Manual") |
| 96 | .setStyle("PRIMARY") |
| 97 | .setCustomId("manual") |
| 98 | ]) |
| 99 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 101 | } |
| 102 | let component; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | try { |
| 104 | component = await m.awaitMessageComponent({ time: 300000 }); |
| 105 | } catch (e) { |
| 106 | return; |
| 107 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | component.deferUpdate(); |
| 109 | let rolesToAdd = []; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 110 | for (let i = 0; i < config.roleMenu.options.length; i++) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | const object = config.roleMenu.options[i]; |
| 112 | const m = await interaction.editReply({ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 113 | embeds: [ |
| 114 | new EmojiEmbed() |
| 115 | .setTitle("Roles") |
| 116 | .setEmoji("GUILD.GREEN") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | .setDescription( |
| 118 | `**${object.name}**` + |
| 119 | (object.description |
| 120 | ? `\n${object.description}` |
| 121 | : "") + |
| 122 | `\n\nSelect ${object.min}` + |
| 123 | (object.min !== object.max |
| 124 | ? ` to ${object.max}` |
| 125 | : "") + |
| 126 | ` role${object.max === 1 ? "" : "s"} to add.` |
| 127 | ) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 128 | .setStatus("Success") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | .setFooter({ |
| 130 | text: `Step ${i + 1}/${config.roleMenu.options.length}` |
| 131 | }) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 132 | ], |
| 133 | components: [ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 134 | new MessageActionRow().addComponents( |
| 135 | [ |
| 136 | new MessageButton() |
| 137 | .setLabel("Cancel") |
| 138 | .setStyle("DANGER") |
| 139 | .setCustomId("cancel") |
| 140 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 141 | ].concat( |
| 142 | object.min === 0 |
| 143 | ? [ |
| 144 | new MessageButton() |
| 145 | .setLabel("Skip") |
| 146 | .setStyle("SECONDARY") |
| 147 | .setCustomId("skip") |
| 148 | .setEmoji( |
| 149 | getEmojiByName("CONTROL.RIGHT", "id") |
| 150 | ) |
| 151 | ] |
| 152 | : [] |
| 153 | ) |
| 154 | ) |
| 155 | ].concat([ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 156 | new MessageActionRow().addComponents([ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | new MessageSelectMenu() |
| 158 | .setPlaceholder(`${object.name}`) |
| 159 | .setCustomId("rolemenu") |
| 160 | .setMinValues(object.min) |
| 161 | .setMaxValues(object.max) |
| 162 | .setOptions( |
| 163 | object.options.map((o) => { |
| 164 | return { |
| 165 | label: o.name, |
| 166 | description: o.description, |
| 167 | value: o.role |
| 168 | }; |
| 169 | }) |
| 170 | ) |
| 171 | ]) |
| 172 | ]) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 173 | }); |
| 174 | let component; |
| 175 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 176 | component = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 177 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 179 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 180 | component.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 181 | if (component.customId === "rolemenu") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 182 | rolesToAdd = rolesToAdd.concat(component.values); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 183 | } else if (component.customId === "cancel") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 184 | return await interaction.editReply({ |
| 185 | embeds: [ |
| 186 | new EmojiEmbed() |
| 187 | .setTitle("Roles") |
| 188 | .setDescription("Cancelled. No changes were made.") |
| 189 | .setStatus("Danger") |
| 190 | .setEmoji("GUILD.RED") |
| 191 | ], |
| 192 | components: [] |
| 193 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 196 | let rolesToRemove = config.roleMenu.options |
| 197 | .map((o) => o.options.map((o) => o.role)) |
| 198 | .flat(); |
| 199 | const memberRoles = interaction.member.roles.cache.map((r) => r.id); |
| 200 | rolesToRemove = rolesToRemove |
| 201 | .filter((r) => memberRoles.includes(r)) |
| 202 | .filter((r) => !rolesToAdd.includes(r)); |
| 203 | rolesToAdd = rolesToAdd.filter((r) => !memberRoles.includes(r)); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 204 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 205 | await interaction.member.roles.remove(rolesToRemove); |
| 206 | await interaction.member.roles.add(rolesToAdd); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 207 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 208 | return await interaction.reply({ |
| 209 | embeds: [ |
| 210 | new EmojiEmbed() |
| 211 | .setTitle("Roles") |
| 212 | .setDescription( |
| 213 | "Something went wrong and your roles were not added. Please contact a staff member or try again later." |
| 214 | ) |
| 215 | .setStatus("Danger") |
| 216 | .setEmoji("GUILD.RED") |
| 217 | ], |
| 218 | components: [] |
| 219 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 220 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 221 | await interaction.editReply({ |
| 222 | embeds: [ |
| 223 | new EmojiEmbed() |
| 224 | .setTitle("Roles") |
| 225 | .setDescription( |
| 226 | "Roles have been added. You may close this message." |
| 227 | ) |
| 228 | .setStatus("Success") |
| 229 | .setEmoji("GUILD.GREEN") |
| 230 | ], |
| 231 | components: [] |
| 232 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 233 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 234 | } |