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