PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 1 | import type { CommandInteraction, GuildMember, Role, User } from "discord.js"; |
| 2 | import type { 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; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 31 | const action = interaction.options.get("action")?.value as string; |
| 32 | const role: Role = (await interaction.guild!.roles.fetch(interaction.options.get("role")?.value as string))!; |
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({ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 39 | user: renderUser(interaction.options.getUser("user")! as User), |
| 40 | role: renderRole(role) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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 | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 47 | .setFailedMessage("No changes were made", "Success", "GUILD.ROLES.CREATE") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | .send(); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 49 | if (confirmation.cancelled || !confirmation.success) return; |
| 50 | try { |
| 51 | const member = interaction.options.getMember("user") as GuildMember; |
| 52 | if ((interaction.options.get("action")?.value as string) === "give") { |
| 53 | member.roles.add(role); |
| 54 | } else { |
| 55 | member.roles.remove(role); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 56 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 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") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [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") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 64 | .setEmoji("CONTROL.BLOCKCROSS") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | ], |
| 66 | components: [] |
| 67 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 68 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 69 | return await interaction.editReply({ |
| 70 | embeds: [ |
| 71 | new EmojiEmbed() |
| 72 | .setTitle("Role") |
| 73 | .setDescription(`The role has been ${action === "give" ? "given" : "removed"} successfully`) |
| 74 | .setStatus("Success") |
| 75 | .setEmoji("GUILD.ROLES.CREATE") |
| 76 | ], |
| 77 | components: [] |
| 78 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 80 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 81 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | const member = interaction.member as GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 83 | if (!interaction.guild) return |
| 84 | const me = interaction.guild.members.me!; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 85 | const apply = interaction.options.getMember("user") as GuildMember | null; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 86 | if (apply === null) return "That member is not in the server"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 87 | // Check if Nucleus has permission to role |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 88 | if (!me.permissions.has("ManageRoles")) return "I do not have the *Manage Roles* permission"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 89 | // Allow the owner to role anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 91 | // Check if the user has manage_roles permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame^] | 92 | if (!member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 93 | // Allow role |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 94 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 96 | |
| 97 | export { command }; |
| 98 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 99 | export { check }; |