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