blob: 736e247ac7b69edd81e124636244e20aa5679c1d [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")
Skyler Grey75ea9172022-08-06 10:22:23 +010013 .addUserOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010014 option.setName("user").setDescription("The member to give or remove the role from").setRequired(true)
Skyler Grey75ea9172022-08-06 10:22:23 +010015 )
16 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010017 option.setName("role").setDescription("The role to give or remove").setRequired(true)
Skyler Grey75ea9172022-08-06 10:22:23 +010018 )
19 .addStringOption((option) =>
20 option
21 .setName("action")
22 .setDescription("The action to perform")
23 .setRequired(true)
24 .addChoices([
25 ["Add", "give"],
26 ["Remove", "remove"]
27 ])
28 );
pineafan4f164f32022-02-26 22:07:12 +000029
pineafan3a02ea32022-08-11 21:35:04 +010030const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010031 const { renderUser, renderRole } = client.logger;
32 const action = interaction.options.getString("action");
pineafan4edb7762022-06-26 19:21:04 +010033 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010034 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010035 .setEmoji("GUILD.ROLES.DELETE")
36 .setTitle("Role")
Skyler Grey75ea9172022-08-06 10:22:23 +010037 .setDescription(
38 keyValueList({
39 user: renderUser(interaction.options.getUser("user")),
40 role: renderRole(interaction.options.getRole("role"))
41 }) +
42 `\nAre you sure you want to ${
Skyler Grey11236ba2022-08-08 21:13:33 +010043 action === "give" ? "give the role to" : "remove the role from"
Skyler Grey75ea9172022-08-06 10:22:23 +010044 } ${interaction.options.getUser("user")}?`
45 )
pineafan4edb7762022-06-26 19:21:04 +010046 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010047 .send();
48 if (confirmation.cancelled) return;
pineafan4edb7762022-06-26 19:21:04 +010049 if (confirmation.success) {
50 try {
pineafan63fc5e22022-08-04 22:04:10 +010051 const member = interaction.options.getMember("user") as GuildMember;
52 const role = interaction.options.getRole("role") as Role;
pineafane23c4ec2022-07-27 21:56:27 +010053 if (interaction.options.getString("action") === "give") {
pineafan63fc5e22022-08-04 22:04:10 +010054 member.roles.add(role);
pineafan4edb7762022-06-26 19:21:04 +010055 } else {
pineafan63fc5e22022-08-04 22:04:10 +010056 member.roles.remove(role);
pineafan4edb7762022-06-26 19:21:04 +010057 }
58 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010059 return await interaction.editReply({
60 embeds: [
61 new EmojiEmbed()
62 .setTitle("Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010063 .setDescription("Something went wrong and the role could not be added")
Skyler Grey75ea9172022-08-06 10:22:23 +010064 .setStatus("Danger")
65 .setEmoji("CONTROL.BLOCKCROSS")
66 ],
67 components: []
68 });
pineafan4edb7762022-06-26 19:21:04 +010069 }
Skyler Grey75ea9172022-08-06 10:22:23 +010070 return await interaction.editReply({
71 embeds: [
72 new EmojiEmbed()
73 .setTitle("Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010074 .setDescription(`The role has been ${action === "give" ? "given" : "removed"} successfully`)
Skyler Grey75ea9172022-08-06 10:22:23 +010075 .setStatus("Success")
76 .setEmoji("GUILD.ROLES.CREATE")
77 ],
78 components: []
79 });
pineafan4edb7762022-06-26 19:21:04 +010080 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 await interaction.editReply({
82 embeds: [
83 new EmojiEmbed()
84 .setEmoji("GUILD.ROLES.CREATE")
85 .setTitle("Role")
86 .setDescription("No changes were made.")
87 .setStatus("Danger")
88 ],
89 components: []
90 });
pineafan4edb7762022-06-26 19:21:04 +010091 }
pineafan63fc5e22022-08-04 22:04:10 +010092};
pineafan4f164f32022-02-26 22:07:12 +000093
Skyler Grey11236ba2022-08-08 21:13:33 +010094const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 const member = interaction.member as GuildMember;
96 const me = interaction.guild.me!;
97 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +010098 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan4edb7762022-06-26 19:21:04 +010099 // Check if Nucleus has permission to role
pineafan3a02ea32022-08-11 21:35:04 +0100100 if (!me.permissions.has("MANAGE_ROLES")) throw new Error("I do not have the *Manage Roles* permission");
pineafan4edb7762022-06-26 19:21:04 +0100101 // Allow the owner to role anyone
pineafan63fc5e22022-08-04 22:04:10 +0100102 if (member.id === interaction.guild.ownerId) return true;
pineafan4edb7762022-06-26 19:21:04 +0100103 // Check if the user has manage_roles permission
pineafan3a02ea32022-08-11 21:35:04 +0100104 if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission");
pineafan4edb7762022-06-26 19:21:04 +0100105 // Allow role
pineafan4f164f32022-02-26 22:07:12 +0000106 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100107};
pineafan4f164f32022-02-26 22:07:12 +0000108
109export { command };
110export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100111export { check };