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