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