blob: 4ec7f3e4a40e567bfa9abd3c676bcdfa6b5aba0e [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import type { CommandInteraction, GuildMember, Role, User } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05002import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01003import client from "../../utils/client.js";
4import confirmationMessage from "../../utils/confirmationMessage.js";
5import keyValueList from "../../utils/generateKeyValueList.js";
6import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("user")
11 .setDescription("Gives or removes a role from someone")
Skyler Grey75ea9172022-08-06 10:22:23 +010012 .addUserOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010013 option.setName("user").setDescription("The member to give or remove the role from").setRequired(true)
Skyler Grey75ea9172022-08-06 10:22:23 +010014 )
15 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010016 option.setName("role").setDescription("The role to give or remove").setRequired(true)
Skyler Grey75ea9172022-08-06 10:22:23 +010017 )
18 .addStringOption((option) =>
19 option
20 .setName("action")
21 .setDescription("The action to perform")
22 .setRequired(true)
PineaFan64486c42022-12-28 09:21:04 +000023 .addChoices(
24 {name: "Add", value: "give"},
25 {name: "Remove", value: "remove"}
26 )
Skyler Grey75ea9172022-08-06 10:22:23 +010027 );
pineafan4f164f32022-02-26 22:07:12 +000028
pineafan3a02ea32022-08-11 21:35:04 +010029const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010030 const { renderUser, renderRole } = client.logger;
PineaFan0d06edc2023-01-17 22:10:31 +000031 const action = interaction.options.get("action")?.value as string;
32 const role: Role = (await interaction.guild!.roles.fetch(interaction.options.get("role")?.value as string))!;
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({
PineaFan0d06edc2023-01-17 22:10:31 +000039 user: renderUser(interaction.options.getUser("user")! as User),
40 role: renderRole(role)
Skyler Grey75ea9172022-08-06 10:22:23 +010041 }) +
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")
PineaFan0d06edc2023-01-17 22:10:31 +000047 .setFailedMessage("No changes were made", "Success", "GUILD.ROLES.CREATE")
pineafan63fc5e22022-08-04 22:04:10 +010048 .send();
PineaFan0d06edc2023-01-17 22:10:31 +000049 if (confirmation.cancelled || !confirmation.success) return;
50 try {
51 const member = interaction.options.getMember("user") as GuildMember;
52 if ((interaction.options.get("action")?.value as string) === "give") {
53 member.roles.add(role);
54 } else {
55 member.roles.remove(role);
pineafan4edb7762022-06-26 19:21:04 +010056 }
PineaFan0d06edc2023-01-17 22:10:31 +000057 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010058 return await interaction.editReply({
59 embeds: [
60 new EmojiEmbed()
61 .setTitle("Role")
PineaFan0d06edc2023-01-17 22:10:31 +000062 .setDescription("Something went wrong and the role could not be added")
Skyler Grey75ea9172022-08-06 10:22:23 +010063 .setStatus("Danger")
PineaFan0d06edc2023-01-17 22:10:31 +000064 .setEmoji("CONTROL.BLOCKCROSS")
Skyler Grey75ea9172022-08-06 10:22:23 +010065 ],
66 components: []
67 });
pineafan4edb7762022-06-26 19:21:04 +010068 }
PineaFan0d06edc2023-01-17 22:10:31 +000069 return await interaction.editReply({
70 embeds: [
71 new EmojiEmbed()
72 .setTitle("Role")
73 .setDescription(`The role has been ${action === "give" ? "given" : "removed"} successfully`)
74 .setStatus("Success")
75 .setEmoji("GUILD.ROLES.CREATE")
76 ],
77 components: []
78 });
pineafan63fc5e22022-08-04 22:04:10 +010079};
pineafan4f164f32022-02-26 22:07:12 +000080
TheCodedProff86ba092023-01-27 17:10:07 -050081const check = (interaction: CommandInteraction, partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -050083 // Check if the user has manage_roles permission
84 if (!member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission";
85 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +000086 if (!interaction.guild) return
87 const me = interaction.guild.members.me!;
Skyler Greyad002172022-08-16 18:48:26 +010088 const apply = interaction.options.getMember("user") as GuildMember | null;
PineaFan0d06edc2023-01-17 22:10:31 +000089 if (apply === null) return "That member is not in the server";
pineafan4edb7762022-06-26 19:21:04 +010090 // Check if Nucleus has permission to role
PineaFan0d06edc2023-01-17 22:10:31 +000091 if (!me.permissions.has("ManageRoles")) return "I do not have the *Manage Roles* permission";
pineafan4edb7762022-06-26 19:21:04 +010092 // Allow the owner to role anyone
pineafan63fc5e22022-08-04 22:04:10 +010093 if (member.id === interaction.guild.ownerId) return true;
pineafan4edb7762022-06-26 19:21:04 +010094 // Allow role
pineafan4f164f32022-02-26 22:07:12 +000095 return true;
pineafan63fc5e22022-08-04 22:04:10 +010096};
pineafan4f164f32022-02-26 22:07:12 +000097
98export { command };
99export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100100export { check };