pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 1 | import { Message, MessageButton } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 2 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 3 | import { MessageActionRow, MessageSelectMenu } 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"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 7 | |
| 8 | export async function callback(interaction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 9 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 10 | if (!config.roleMenu.enabled) return await interaction.reply({embeds: [new EmojiEmbed() |
| 11 | .setTitle("Roles") |
| 12 | .setDescription("Self roles are currently disabled. Please contact a staff member or try again later.") |
| 13 | .setStatus("Danger") |
| 14 | .setEmoji("CONTROL.BLOCKCROSS") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 15 | ], ephemeral: true}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | if (config.roleMenu.options.length === 0) return await interaction.reply({embeds: [new EmojiEmbed() |
| 17 | .setTitle("Roles") |
| 18 | .setDescription("There are no roles available. Please contact a staff member or try again later.") |
| 19 | .setStatus("Danger") |
| 20 | .setEmoji("CONTROL.BLOCKCROSS") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 21 | ], ephemeral: true}); |
| 22 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 23 | let m; |
| 24 | if (config.roleMenu.allowWebUI) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 25 | let code = ""; |
| 26 | let length = 5; |
| 27 | let itt = 0; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 28 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 29 | let valid = false; |
| 30 | while (!valid) { |
| 31 | itt += 1; |
| 32 | code = ""; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 33 | for (let i = 0; i < length; i++) { code += chars.charAt(Math.floor(Math.random() * chars.length)); } |
| 34 | if (code in client.roleMenu) continue; |
| 35 | if (itt > 1000) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 36 | itt = 0; |
| 37 | length += 1; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 38 | continue; |
| 39 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 40 | valid = true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 41 | } |
| 42 | client.roleMenu[code] = { |
| 43 | guild: interaction.guild.id, |
| 44 | guildName: interaction.guild.name, |
| 45 | guildIcon: interaction.guild.iconURL({format: "png"}), |
| 46 | user: interaction.member.user.id, |
| 47 | username: interaction.member.user.username, |
| 48 | data: config.roleMenu.options, |
| 49 | interaction: interaction |
| 50 | }; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 51 | let up = true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 52 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 53 | const status = await fetch(client.config.baseUrl).then(res => res.status); |
| 54 | if (status !== 200) up = false; |
| 55 | } catch { up = false; } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 56 | m = await interaction.editReply({ |
| 57 | embeds: [new EmojiEmbed() |
| 58 | .setTitle("Roles") |
| 59 | .setDescription("Select how to choose your roles") |
| 60 | .setStatus("Success") |
| 61 | .setEmoji("GUILD.GREEN") |
| 62 | ], components: [new MessageActionRow().addComponents([ |
| 63 | new MessageButton() |
| 64 | .setLabel("Online") |
| 65 | .setStyle("LINK") |
| 66 | .setDisabled(!up) |
pineafan | 6d53741 | 2022-07-28 12:35:33 +0100 | [diff] [blame] | 67 | .setURL(`${client.config.baseUrl}nucleus/rolemenu?code=${code}`), |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 68 | new MessageButton() |
| 69 | .setLabel("Manual") |
| 70 | .setStyle("PRIMARY") |
| 71 | .setCustomId("manual") |
| 72 | ])] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 73 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 74 | } |
| 75 | let component; |
| 76 | try { component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 77 | } catch (e) { return; } |
| 78 | component.deferUpdate(); |
| 79 | let rolesToAdd = []; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 80 | for (let i = 0; i < config.roleMenu.options.length; i++) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 81 | const object = config.roleMenu.options[i]; |
| 82 | const m = await interaction.editReply({ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 83 | embeds: [ |
| 84 | new EmojiEmbed() |
| 85 | .setTitle("Roles") |
| 86 | .setEmoji("GUILD.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 87 | .setDescription(`**${object.name}**` + (object.description ? `\n${object.description}` : "") + |
| 88 | `\n\nSelect ${object.min}` + (object.min !== object.max ? ` to ${object.max}` : "") + ` role${object.max === 1 ? "" : "s"} to add.`) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 89 | .setStatus("Success") |
| 90 | .setFooter({text: `Step ${i + 1}/${config.roleMenu.options.length}`}) |
| 91 | ], |
| 92 | components: [ |
| 93 | new MessageActionRow().addComponents([ |
| 94 | new MessageButton() |
| 95 | .setLabel("Cancel") |
| 96 | .setStyle("DANGER") |
| 97 | .setCustomId("cancel") |
| 98 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 99 | ].concat(object.min === 0 ? [ |
| 100 | new MessageButton() |
| 101 | .setLabel("Skip") |
| 102 | .setStyle("SECONDARY") |
| 103 | .setCustomId("skip") |
| 104 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 105 | ] : [])) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 106 | ].concat([new MessageActionRow().addComponents([new MessageSelectMenu() |
| 107 | .setPlaceholder(`${object.name}`) |
| 108 | .setCustomId("rolemenu") |
| 109 | .setMinValues(object.min) |
| 110 | .setMaxValues(object.max) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 111 | .setOptions(object.options.map(o => { return {label: o.name, description: o.description, value: o.role}; })) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 112 | ])]) |
| 113 | }); |
| 114 | let component; |
| 115 | try { |
| 116 | component = await (m as Message).awaitMessageComponent({time: 300000}); |
| 117 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 118 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 119 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 120 | component.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 121 | if (component.customId === "rolemenu") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 122 | rolesToAdd = rolesToAdd.concat(component.values); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 123 | } else if (component.customId === "cancel") { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 124 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 125 | .setTitle("Roles") |
| 126 | .setDescription("Cancelled. No changes were made.") |
| 127 | .setStatus("Danger") |
| 128 | .setEmoji("GUILD.RED") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 129 | ], components: []}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 132 | let rolesToRemove = config.roleMenu.options.map(o => o.options.map(o => o.role)).flat(); |
| 133 | const memberRoles = interaction.member.roles.cache.map(r => r.id); |
| 134 | rolesToRemove = rolesToRemove.filter(r => memberRoles.includes(r)).filter(r => !rolesToAdd.includes(r)); |
| 135 | rolesToAdd = rolesToAdd.filter(r => !memberRoles.includes(r)); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 136 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 137 | await interaction.member.roles.remove(rolesToRemove); |
| 138 | await interaction.member.roles.add(rolesToAdd); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 139 | } catch (e) { |
| 140 | return await interaction.reply({embeds: [new EmojiEmbed() |
| 141 | .setTitle("Roles") |
| 142 | .setDescription("Something went wrong and your roles were not added. Please contact a staff member or try again later.") |
| 143 | .setStatus("Danger") |
| 144 | .setEmoji("GUILD.RED") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 145 | ], components: []}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 146 | } |
| 147 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 148 | .setTitle("Roles") |
| 149 | .setDescription("Roles have been added. You may close this message.") |
| 150 | .setStatus("Success") |
| 151 | .setEmoji("GUILD.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 152 | ], components: []}); |
| 153 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 154 | } |