Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { unknownServerIcon } from "./../utils/defaults.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 2 | import { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 3 | ButtonBuilder, |
| 4 | CommandInteraction, |
| 5 | ButtonStyle, |
| 6 | ButtonInteraction, |
| 7 | StringSelectMenuOptionBuilder, |
| 8 | StringSelectMenuBuilder, |
| 9 | GuildMemberRoleManager, |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 10 | Role, |
| 11 | ContextMenuCommandInteraction |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 12 | } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 14 | import { ActionRowBuilder } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 15 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 16 | import client from "../utils/client.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 17 | import { LoadingEmbed } from "../utils/defaults.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 18 | import type { GuildConfig } from "../utils/database.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 19 | import { roleException } from "../utils/createTemporaryStorage.js"; |
| 20 | import addPlural from "../utils/plurals.js"; |
| 21 | import createPageIndicator from "../utils/createPageIndicator.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 22 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 23 | export interface RoleMenuSchema { |
| 24 | guild: string; |
| 25 | guildName: string; |
| 26 | guildIcon: string; |
| 27 | user: string; |
| 28 | username: string; |
| 29 | data: GuildConfig["roleMenu"]["options"]; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 30 | interaction: CommandInteraction | ButtonInteraction | ContextMenuCommandInteraction; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 31 | } |
| 32 | |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 33 | interface ObjectSchema { |
| 34 | name: string; |
| 35 | description: string; |
| 36 | min: number; |
| 37 | max: number; |
| 38 | options: { |
| 39 | name: string; |
| 40 | description: string | null; |
| 41 | role: string; |
| 42 | }[]; |
| 43 | } |
| 44 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 45 | export const configToDropdown = ( |
| 46 | placeholder: string, |
| 47 | currentPageData: ObjectSchema, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame^] | 48 | selectedRoles?: string[], |
| 49 | disabled?: boolean |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 50 | ): ActionRowBuilder<StringSelectMenuBuilder> => { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 51 | return new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 52 | new StringSelectMenuBuilder() |
| 53 | .setCustomId("roles") |
| 54 | .setPlaceholder(placeholder) |
| 55 | .setMinValues(currentPageData.min) |
| 56 | .setMaxValues(currentPageData.max) |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame^] | 57 | .setDisabled(disabled) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 58 | .addOptions( |
| 59 | currentPageData.options.map((option: { name: string; description: string | null; role: string }) => { |
| 60 | const builder = new StringSelectMenuOptionBuilder() |
| 61 | .setLabel(option.name) |
| 62 | .setValue(option.role) |
| 63 | .setDefault(selectedRoles ? selectedRoles.includes(option.role) : false); |
| 64 | if (option.description) builder.setDescription(option.description); |
| 65 | return builder; |
| 66 | }) |
| 67 | ) |
| 68 | ); |
| 69 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 70 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 71 | export async function callback(interaction: CommandInteraction | ButtonInteraction) { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 72 | if (!interaction.member) return; |
| 73 | if (!interaction.guild) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | const config = await client.database.guilds.read(interaction.guild.id); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 75 | if (!config.roleMenu.enabled) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 76 | return await interaction.reply({ |
| 77 | embeds: [ |
| 78 | new EmojiEmbed() |
| 79 | .setTitle("Roles") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 80 | .setDescription( |
| 81 | "Self roles are currently disabled. Please contact a staff member or try again later." |
| 82 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | .setStatus("Danger") |
TheCodedProf | 48865eb | 2023-03-05 15:25:25 -0500 | [diff] [blame] | 84 | .setEmoji("GUILD.RED") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | ], |
| 86 | ephemeral: true |
| 87 | }); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 88 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | if (config.roleMenu.options.length === 0) |
| 90 | return await interaction.reply({ |
| 91 | embeds: [ |
| 92 | new EmojiEmbed() |
| 93 | .setTitle("Roles") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 94 | .setDescription( |
| 95 | "There are no roles available. Please contact a staff member if you believe this is a mistake." |
| 96 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | .setStatus("Danger") |
TheCodedProf | 48865eb | 2023-03-05 15:25:25 -0500 | [diff] [blame] | 98 | .setEmoji("GUILD.RED") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 99 | ], |
| 100 | ephemeral: true |
| 101 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 102 | const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | if (config.roleMenu.allowWebUI) { |
| 104 | // TODO: Make rolemenu web ui |
| 105 | const loginMethods: { webUI: boolean } = { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 106 | webUI: false |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 107 | }; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 108 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 109 | const status = await fetch(client.config.baseUrl).then((res) => res.status); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 110 | if (status !== 200) loginMethods.webUI = false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 111 | } catch (e) { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 112 | loginMethods.webUI = false; |
| 113 | } |
| 114 | if (Object.values(loginMethods).some((i) => i)) { |
| 115 | let code = ""; |
| 116 | if (loginMethods.webUI) { |
| 117 | let length = 5; |
| 118 | let itt = 0; |
| 119 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 120 | let valid = false; |
| 121 | while (!valid) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 122 | itt++; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 123 | code = ""; |
| 124 | for (let i = 0; i < length; i++) { |
| 125 | code += chars.charAt(Math.floor(Math.random() * chars.length)); |
| 126 | } |
| 127 | if (code in client.roleMenu) continue; |
| 128 | if (itt > 1000) { |
| 129 | itt = 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 130 | length++; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | valid = true; |
| 134 | } |
| 135 | client.roleMenu[code] = { |
| 136 | guild: interaction.guild.id, |
| 137 | guildName: interaction.guild.name, |
| 138 | guildIcon: interaction.guild.iconURL({ extension: "png" }) ?? unknownServerIcon, |
| 139 | user: interaction.member!.user.id, |
| 140 | username: interaction.member!.user.username, |
| 141 | data: config.roleMenu.options, |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 142 | interaction: interaction as CommandInteraction | ButtonInteraction |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 143 | }; |
| 144 | } |
| 145 | await interaction.editReply({ |
| 146 | embeds: [ |
| 147 | new EmojiEmbed() |
| 148 | .setTitle("Roles") |
| 149 | .setDescription("Select how to choose your roles") |
| 150 | .setStatus("Success") |
| 151 | .setEmoji("GUILD.GREEN") |
| 152 | ], |
| 153 | components: [ |
| 154 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 155 | new ButtonBuilder() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 156 | .setLabel("Online") |
| 157 | .setStyle(ButtonStyle.Link) |
| 158 | .setDisabled(!loginMethods.webUI) |
| 159 | .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 160 | new ButtonBuilder().setLabel("In Discord").setStyle(ButtonStyle.Primary).setCustomId("discord") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 161 | ]) |
| 162 | ] |
| 163 | }); |
| 164 | let component; |
| 165 | try { |
| 166 | component = await m.awaitMessageComponent({ |
| 167 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 168 | filter: (i) => { |
| 169 | return ( |
| 170 | i.user.id === interaction.user.id && |
| 171 | i.channelId === interaction.channelId && |
| 172 | i.message.id === m.id |
| 173 | ); |
| 174 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 175 | }); |
| 176 | } catch (e) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 177 | console.log(e); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 178 | return; |
| 179 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 180 | await component.deferUpdate(); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | const options = config.roleMenu.options; |
| 185 | const selectedRoles: string[][] = []; |
| 186 | const maxPage = options.length; |
| 187 | const completedPages: boolean[] = options.map((option) => option.min === 0); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 188 | for (let i = 0; i < maxPage; i++) { |
| 189 | selectedRoles.push([]); |
| 190 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 191 | |
| 192 | let page = 0; |
| 193 | let complete = completedPages.every((page) => page); |
| 194 | let done = false; |
| 195 | |
| 196 | while (!(complete && done)) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 197 | const currentPageData = options[page]!; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 198 | const embed = new EmojiEmbed() |
| 199 | .setTitle("Roles") |
| 200 | .setDescription( |
| 201 | `**${currentPageData.name}**\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 202 | `> ${currentPageData.description}\n\n` + |
| 203 | (currentPageData.min === currentPageData.max |
| 204 | ? `Select ${addPlural(currentPageData.min, "role")}` |
| 205 | : `Select between ${currentPageData.min} and ${currentPageData.max} roles then press next`) + |
| 206 | "\n\n" + |
| 207 | createPageIndicator(maxPage, page) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 208 | ) |
| 209 | .setStatus("Success") |
| 210 | .setEmoji("GUILD.GREEN"); |
| 211 | const components = [ |
| 212 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 213 | new ButtonBuilder() |
| 214 | .setStyle(ButtonStyle.Secondary) |
| 215 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 216 | .setCustomId("back") |
| 217 | .setDisabled(page === 0), |
| 218 | new ButtonBuilder() |
| 219 | .setStyle(ButtonStyle.Secondary) |
| 220 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 221 | .setCustomId("next") |
| 222 | .setDisabled(page === maxPage - 1), |
| 223 | new ButtonBuilder() |
| 224 | .setStyle(ButtonStyle.Success) |
| 225 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 226 | .setCustomId("done") |
| 227 | .setDisabled(!complete) |
| 228 | ), |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 229 | configToDropdown("Select...", currentPageData, selectedRoles[page]) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 230 | ]; |
| 231 | await interaction.editReply({ |
| 232 | embeds: [embed], |
| 233 | components: components |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 234 | }); |
| 235 | let component; |
| 236 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 237 | component = await m.awaitMessageComponent({ |
| 238 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 239 | filter: (i) => { |
| 240 | return ( |
| 241 | i.user.id === interaction.user.id && |
| 242 | i.channel!.id === interaction.channel!.id && |
| 243 | i.message.id === m.id |
| 244 | ); |
| 245 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 246 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 247 | } catch (e) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 248 | console.log(e); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 249 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 250 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 251 | await component.deferUpdate(); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 252 | if (component.customId === "back") { |
| 253 | page = Math.max(0, page - 1); |
| 254 | } else if (component.customId === "next") { |
| 255 | page = Math.min(maxPage - 1, page + 1); |
| 256 | } else if (component.customId === "done") { |
| 257 | done = true; |
| 258 | } else if (component.customId === "roles" && component.isStringSelectMenu()) { |
| 259 | selectedRoles[page] = component.values; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 260 | completedPages[page] = true; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 261 | page = Math.min(maxPage - 1, page + 1); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 262 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 263 | complete = completedPages.every((page) => page); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 264 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 265 | |
| 266 | const fullRoleList: string[] = selectedRoles.flat(); |
| 267 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 268 | const memberRoles = (interaction.member.roles as GuildMemberRoleManager).cache.map((r) => r.id); // IDs |
| 269 | let rolesToRemove = config.roleMenu.options.map((o) => o.options.map((o) => o.role)).flat(); // IDs |
| 270 | rolesToRemove = rolesToRemove.filter((r) => memberRoles.includes(r)).filter((r) => !fullRoleList.includes(r)); // IDs |
| 271 | let roleObjectsToAdd = fullRoleList |
| 272 | .map((r) => interaction.guild!.roles.cache.get(r)) // Role objects |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 273 | .filter((r) => r !== undefined) as Role[]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 274 | roleObjectsToAdd = roleObjectsToAdd.filter((r) => !memberRoles.includes(r.id)); // Role objects |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 275 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 276 | roleException(interaction.guild.id, interaction.user.id); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 277 | await (interaction.member.roles as GuildMemberRoleManager).remove(rolesToRemove); |
| 278 | await (interaction.member.roles as GuildMemberRoleManager).add(roleObjectsToAdd); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 279 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 280 | return await interaction.reply({ |
| 281 | embeds: [ |
| 282 | new EmojiEmbed() |
| 283 | .setTitle("Roles") |
| 284 | .setDescription( |
| 285 | "Something went wrong and your roles were not added. Please contact a staff member or try again later." |
| 286 | ) |
| 287 | .setStatus("Danger") |
| 288 | .setEmoji("GUILD.RED") |
| 289 | ], |
| 290 | components: [] |
| 291 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 292 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 293 | await interaction.editReply({ |
| 294 | embeds: [ |
| 295 | new EmojiEmbed() |
| 296 | .setTitle("Roles") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 297 | .setDescription("Roles have been updated") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 298 | .setStatus("Success") |
| 299 | .setEmoji("GUILD.GREEN") |
| 300 | ], |
| 301 | components: [] |
| 302 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 303 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 304 | } |