pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 1 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; |
| 2 | import generateEmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 3 | import confirmationMessage from "../../../utils/confirmationMessage.js"; |
| 4 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 5 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 6 | import { WrappedCheck } from "jshaiku"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 7 | import client from "../../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
| 11 | .setName("role") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 12 | .setDescription("Sets or shows the role given to users after using /verify") |
| 13 | .addRoleOption(option => option.setName("role").setDescription("The role to give after verifying")) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 15 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 16 | let m; |
| 17 | m = await interaction.reply({embeds: [new generateEmojiEmbed() |
| 18 | .setTitle("Loading") |
| 19 | .setStatus("Danger") |
| 20 | .setEmoji("NUCLEUS.LOADING") |
| 21 | ], ephemeral: true, fetchReply: true}); |
| 22 | if (interaction.options.getRole("role")) { |
| 23 | let role |
| 24 | try { |
| 25 | role = interaction.options.getRole("role") |
| 26 | } catch { |
| 27 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 28 | .setEmoji("GUILD.ROLES.DELETE") |
| 29 | .setTitle("Verify Role") |
| 30 | .setDescription("The role you provided is not a valid role") |
| 31 | .setStatus("Danger") |
| 32 | ]}) |
| 33 | } |
| 34 | role = role as Discord.Role |
| 35 | if (role.guild.id != interaction.guild.id) { |
| 36 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 37 | .setTitle("Verify Role") |
| 38 | .setDescription(`You must choose a role in this server`) |
| 39 | .setStatus("Danger") |
| 40 | .setEmoji("GUILD.ROLES.DELETE") |
| 41 | ]}); |
| 42 | } |
| 43 | let confirmation = await new confirmationMessage(interaction) |
| 44 | .setEmoji("GUILD.ROLES.EDIT") |
| 45 | .setTitle("Verify Role") |
| 46 | .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`) |
| 47 | .setColor("Warning") |
| 48 | .setInverted(true) |
| 49 | .send(true) |
| 50 | if (confirmation.success) { |
| 51 | try { |
| 52 | await client.database.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true}); |
| 53 | } catch (e) { |
| 54 | console.log(e) |
| 55 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 56 | .setTitle("Verify Role") |
| 57 | .setDescription(`Something went wrong while setting the verify role`) |
| 58 | .setStatus("Danger") |
| 59 | .setEmoji("GUILD.ROLES.DELETE") |
| 60 | ], components: []}); |
| 61 | } |
| 62 | } else { |
| 63 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 64 | .setTitle("Verify Role") |
| 65 | .setDescription(`No changes were made`) |
| 66 | .setStatus("Success") |
| 67 | .setEmoji("GUILD.ROLES.CREATE") |
| 68 | ], components: []}); |
| 69 | } |
| 70 | } |
| 71 | let clicks = 0; |
| 72 | let data = await client.database.read(interaction.guild.id); |
| 73 | let role = data.verify.role; |
| 74 | while (true) { |
| 75 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 76 | .setTitle("Verify Role") |
| 77 | .setDescription(role ? `Your verify role is currently set to <@&${role}>` : `You have not set a verify role`) |
| 78 | .setStatus("Success") |
| 79 | .setEmoji("GUILD.ROLES.CREATE") |
| 80 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 81 | .setCustomId("clear") |
| 82 | .setLabel(clicks ? "Click again to confirm" : "Reset role") |
| 83 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 84 | .setStyle("DANGER") |
| 85 | .setDisabled(!role) |
| 86 | ])]}); |
| 87 | let i; |
| 88 | try { |
| 89 | i = await m.awaitMessageComponent({time: 600000}); |
| 90 | } catch(e) { break } |
| 91 | i.deferUpdate() |
| 92 | if (i.component.customId == "clear") { |
| 93 | clicks += 1; |
| 94 | if (clicks == 2) { |
| 95 | clicks = 0; |
| 96 | await client.database.write(interaction.guild.id, {}, ["verify.role", "verify.enabled"]) |
| 97 | role = undefined; |
| 98 | } |
| 99 | } else { |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 104 | .setTitle("Verify Role") |
| 105 | .setDescription(role ? `Your verify role is currently set to <@&${role}}>` : `You have not set a verify role`) |
| 106 | .setStatus("Success") |
| 107 | .setEmoji("GUILD.ROLE.CREATE") |
| 108 | .setFooter({text: "Message closed"}) |
| 109 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 110 | .setCustomId("clear") |
| 111 | .setLabel("Clear") |
| 112 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 113 | .setStyle("SECONDARY") |
| 114 | .setDisabled(true) |
| 115 | ])]}); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 119 | let member = (interaction.member as Discord.GuildMember) |
| 120 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the `manage_server` permission to use this command" |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
| 124 | export { command }; |
| 125 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 126 | export { check }; |