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