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