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