blob: d361cf16cb57fa63d123d23d6dee06870818cea8 [file] [log] [blame]
pineafan4edb7762022-06-26 19:21:04 +01001import { CommandInteraction, GuildMember, Role } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01004import client from "../../utils/client.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
6import keyValueList from "../../utils/generateKeyValueList.js";
7import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
11 .setName("user")
pineafan377794f2022-04-18 19:01:01 +010012 .setDescription("Gives or removes a role from someone")
pineafan4edb7762022-06-26 19:21:04 +010013 .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 ]))
pineafan4f164f32022-02-26 22:07:12 +000019
pineafan4edb7762022-06-26 19:21:04 +010020
21const 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"))
pineafane23c4ec2022-07-27 21:56:27 +010031 }) + `\nAre you sure you want to ${action === "give" ? "give the role to" : "remove the role from"} ${interaction.options.getUser("user")}?`)
pineafan4edb7762022-06-26 19:21:04 +010032 .setColor("Danger")
33 .send()
pineafan02ba0232022-07-24 22:16:15 +010034 if (confirmation.cancelled) return
pineafan4edb7762022-06-26 19:21:04 +010035 if (confirmation.success) {
36 try {
37 let member = interaction.options.getMember("user") as GuildMember
38 let role = interaction.options.getRole("role") as Role
pineafane23c4ec2022-07-27 21:56:27 +010039 if (interaction.options.getString("action") === "give") {
pineafan4edb7762022-06-26 19:21:04 +010040 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")
pineafane23c4ec2022-07-27 21:56:27 +010054 .setDescription(`The role has been ${action === "give" ? "given" : "removed"} successfully`)
pineafan4edb7762022-06-26 19:21:04 +010055 .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")
pineafanc1c18792022-08-03 21:41:36 +010062 .setDescription(`No changes were made.`)
pineafan4edb7762022-06-26 19:21:04 +010063 .setStatus("Danger")
64 ], components: []})
65 }
pineafan4f164f32022-02-26 22:07:12 +000066}
67
68const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan4edb7762022-06-26 19:21:04 +010069 let member = (interaction.member as GuildMember)
70 let me = (interaction.guild.me as GuildMember)
71 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +010072 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan4edb7762022-06-26 19:21:04 +010073 // Check if Nucleus has permission to role
pineafane23c4ec2022-07-27 21:56:27 +010074 if (!me.permissions.has("MANAGE_ROLES")) throw "I do not have the *Manage Roles* permission";
pineafan4edb7762022-06-26 19:21:04 +010075 // Allow the owner to role anyone
pineafane23c4ec2022-07-27 21:56:27 +010076 if (member.id === interaction.guild.ownerId) return true
pineafan4edb7762022-06-26 19:21:04 +010077 // Check if the user has manage_roles permission
pineafane23c4ec2022-07-27 21:56:27 +010078 if (! member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission";
pineafan4edb7762022-06-26 19:21:04 +010079 // Allow role
pineafan4f164f32022-02-26 22:07:12 +000080 return true;
81}
82
83export { command };
84export { callback };
85export { check };