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