blob: d2b05e95010df6b666af99f429e9c8630e3321f2 [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
pineafan63fc5e22022-08-04 22:04:10 +010011 .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 ]));
pineafan4f164f32022-02-26 22:07:12 +000019
pineafan4edb7762022-06-26 19:21:04 +010020
21const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010022 const { renderUser, renderRole } = client.logger;
23 const action = interaction.options.getString("action");
pineafan4edb7762022-06-26 19:21:04 +010024 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010025 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010026 .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")
pineafan63fc5e22022-08-04 22:04:10 +010033 .send();
34 if (confirmation.cancelled) return;
pineafan4edb7762022-06-26 19:21:04 +010035 if (confirmation.success) {
36 try {
pineafan63fc5e22022-08-04 22:04:10 +010037 const member = interaction.options.getMember("user") as GuildMember;
38 const role = interaction.options.getRole("role") as Role;
pineafane23c4ec2022-07-27 21:56:27 +010039 if (interaction.options.getString("action") === "give") {
pineafan63fc5e22022-08-04 22:04:10 +010040 member.roles.add(role);
pineafan4edb7762022-06-26 19:21:04 +010041 } else {
pineafan63fc5e22022-08-04 22:04:10 +010042 member.roles.remove(role);
pineafan4edb7762022-06-26 19:21:04 +010043 }
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")
pineafan63fc5e22022-08-04 22:04:10 +010050 ], components: []});
pineafan4edb7762022-06-26 19:21:04 +010051 }
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")
pineafan63fc5e22022-08-04 22:04:10 +010057 ], components: []});
pineafan4edb7762022-06-26 19:21:04 +010058 } else {
59 await interaction.editReply({embeds: [new EmojiEmbed()
60 .setEmoji("GUILD.ROLES.CREATE")
61 .setTitle("Role")
pineafan63fc5e22022-08-04 22:04:10 +010062 .setDescription("No changes were made.")
pineafan4edb7762022-06-26 19:21:04 +010063 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010064 ], components: []});
pineafan4edb7762022-06-26 19:21:04 +010065 }
pineafan63fc5e22022-08-04 22:04:10 +010066};
pineafan4f164f32022-02-26 22:07:12 +000067
68const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +010069 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";
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
pineafan63fc5e22022-08-04 22:04:10 +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;
pineafan63fc5e22022-08-04 22:04:10 +010081};
pineafan4f164f32022-02-26 22:07:12 +000082
83export { command };
84export { callback };
85export { check };