pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, Role } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import client from "../../utils/client.js"; |
| 5 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 7 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | .setName("user") |
| 12 | .setDescription("Gives or removes a role from someone") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | .addUserOption((option) => |
| 14 | option |
| 15 | .setName("user") |
| 16 | .setDescription("The member to give or remove the role from") |
| 17 | .setRequired(true) |
| 18 | ) |
| 19 | .addRoleOption((option) => |
| 20 | option |
| 21 | .setName("role") |
| 22 | .setDescription("The role to give or remove") |
| 23 | .setRequired(true) |
| 24 | ) |
| 25 | .addStringOption((option) => |
| 26 | option |
| 27 | .setName("action") |
| 28 | .setDescription("The action to perform") |
| 29 | .setRequired(true) |
| 30 | .addChoices([ |
| 31 | ["Add", "give"], |
| 32 | ["Remove", "remove"] |
| 33 | ]) |
| 34 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 35 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | const callback = async ( |
| 37 | interaction: CommandInteraction |
| 38 | ): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | const { renderUser, renderRole } = client.logger; |
| 40 | const action = interaction.options.getString("action"); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 41 | // TODO:[Modals] Replace this with a modal |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 43 | .setEmoji("GUILD.ROLES.DELETE") |
| 44 | .setTitle("Role") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | .setDescription( |
| 46 | keyValueList({ |
| 47 | user: renderUser(interaction.options.getUser("user")), |
| 48 | role: renderRole(interaction.options.getRole("role")) |
| 49 | }) + |
| 50 | `\nAre you sure you want to ${ |
| 51 | action === "give" |
| 52 | ? "give the role to" |
| 53 | : "remove the role from" |
| 54 | } ${interaction.options.getUser("user")}?` |
| 55 | ) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 56 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | .send(); |
| 58 | if (confirmation.cancelled) return; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 59 | if (confirmation.success) { |
| 60 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | const member = interaction.options.getMember("user") as GuildMember; |
| 62 | const role = interaction.options.getRole("role") as Role; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 63 | if (interaction.options.getString("action") === "give") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | member.roles.add(role); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 65 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | member.roles.remove(role); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 67 | } |
| 68 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | return await interaction.editReply({ |
| 70 | embeds: [ |
| 71 | new EmojiEmbed() |
| 72 | .setTitle("Role") |
| 73 | .setDescription( |
| 74 | "Something went wrong and the role could not be added" |
| 75 | ) |
| 76 | .setStatus("Danger") |
| 77 | .setEmoji("CONTROL.BLOCKCROSS") |
| 78 | ], |
| 79 | components: [] |
| 80 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 81 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | return await interaction.editReply({ |
| 83 | embeds: [ |
| 84 | new EmojiEmbed() |
| 85 | .setTitle("Role") |
| 86 | .setDescription( |
| 87 | `The role has been ${ |
| 88 | action === "give" ? "given" : "removed" |
| 89 | } successfully` |
| 90 | ) |
| 91 | .setStatus("Success") |
| 92 | .setEmoji("GUILD.ROLES.CREATE") |
| 93 | ], |
| 94 | components: [] |
| 95 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 96 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | await interaction.editReply({ |
| 98 | embeds: [ |
| 99 | new EmojiEmbed() |
| 100 | .setEmoji("GUILD.ROLES.CREATE") |
| 101 | .setTitle("Role") |
| 102 | .setDescription("No changes were made.") |
| 103 | .setStatus("Danger") |
| 104 | ], |
| 105 | components: [] |
| 106 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 107 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 109 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | const check = ( |
| 111 | interaction: CommandInteraction, |
| 112 | _defaultCheck: WrappedCheck |
| 113 | ) => { |
| 114 | const member = interaction.member as GuildMember; |
| 115 | const me = interaction.guild.me!; |
| 116 | const apply = interaction.options.getMember("user") as GuildMember; |
| 117 | if (member === null || me === null || apply === null) |
| 118 | throw "That member is not in the server"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 119 | // Check if Nucleus has permission to role |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 120 | if (!me.permissions.has("MANAGE_ROLES")) |
| 121 | throw "I do not have the *Manage Roles* permission"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 122 | // Allow the owner to role anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 123 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 124 | // Check if the user has manage_roles permission |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | if (!member.permissions.has("MANAGE_ROLES")) |
| 126 | throw "You do not have the *Manage Roles* permission"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 127 | // Allow role |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 128 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 129 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 130 | |
| 131 | export { command }; |
| 132 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 133 | export { check }; |