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 |
| 11 | .setName("user") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 12 | .setDescription("Gives or removes a role from someone") |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 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> => { |
| 22 | const { renderUser, renderRole } = client.logger |
| 23 | let action = interaction.options.getString("action"); |
| 24 | // TODO:[Modals] Replace this with a modal |
| 25 | let confirmation = await new confirmationMessage(interaction) |
| 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 | 02ba023 | 2022-07-24 22:16:15 +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") |
| 33 | .send() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame^] | 34 | if (confirmation.cancelled) return |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 35 | if (confirmation.success) { |
| 36 | try { |
| 37 | let member = interaction.options.getMember("user") as GuildMember |
| 38 | let role = interaction.options.getRole("role") as Role |
| 39 | if (interaction.options.getString("action") == "give") { |
| 40 | member.roles.add(role) |
| 41 | } else { |
| 42 | member.roles.remove(role) |
| 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") |
| 50 | ], components: []}) |
| 51 | } |
| 52 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 53 | .setTitle("Role") |
| 54 | .setDescription(`The role has been ${action == "give" ? "given" : "removed"} successfully`) |
| 55 | .setStatus("Success") |
| 56 | .setEmoji("GUILD.ROLES.CREATE") |
| 57 | ], components: []}) |
| 58 | } else { |
| 59 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 60 | .setEmoji("GUILD.ROLES.CREATE") |
| 61 | .setTitle("Role") |
| 62 | .setDescription(`You have cancelled the role change.`) |
| 63 | .setStatus("Danger") |
| 64 | ], components: []}) |
| 65 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 69 | let member = (interaction.member as GuildMember) |
| 70 | let me = (interaction.guild.me as GuildMember) |
| 71 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 72 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 73 | // Check if Nucleus has permission to role |
| 74 | if (!me.permissions.has("MANAGE_ROLES")) throw "I do not have the Manage roles permission"; |
| 75 | // Allow the owner to role anyone |
| 76 | if (member.id == interaction.guild.ownerId) return true |
| 77 | // Check if the user has manage_roles permission |
| 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; |
| 81 | } |
| 82 | |
| 83 | export { command }; |
| 84 | export { callback }; |
| 85 | export { check }; |