blob: ac94b4794d723d1f7bd014eec7a20fbff569dbf9 [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";
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;
31 const action = interaction.options.getString("action");
pineafan4edb7762022-06-26 19:21:04 +010032 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010033 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010034 .setEmoji("GUILD.ROLES.DELETE")
35 .setTitle("Role")
Skyler Grey75ea9172022-08-06 10:22:23 +010036 .setDescription(
37 keyValueList({
38 user: renderUser(interaction.options.getUser("user")),
TheCodedProf21c08592022-09-13 14:14:43 -040039 role: renderRole(interaction.options.get("role"))
Skyler Grey75ea9172022-08-06 10:22:23 +010040 }) +
41 `\nAre you sure you want to ${
Skyler Grey11236ba2022-08-08 21:13:33 +010042 action === "give" ? "give the role to" : "remove the role from"
Skyler Grey75ea9172022-08-06 10:22:23 +010043 } ${interaction.options.getUser("user")}?`
44 )
pineafan4edb7762022-06-26 19:21:04 +010045 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010046 .send();
47 if (confirmation.cancelled) return;
pineafan4edb7762022-06-26 19:21:04 +010048 if (confirmation.success) {
49 try {
pineafan63fc5e22022-08-04 22:04:10 +010050 const member = interaction.options.getMember("user") as GuildMember;
TheCodedProf21c08592022-09-13 14:14:43 -040051 const role = interaction.options.get("role") as unknown as Role;
pineafane23c4ec2022-07-27 21:56:27 +010052 if (interaction.options.getString("action") === "give") {
pineafan63fc5e22022-08-04 22:04:10 +010053 member.roles.add(role);
pineafan4edb7762022-06-26 19:21:04 +010054 } else {
pineafan63fc5e22022-08-04 22:04:10 +010055 member.roles.remove(role);
pineafan4edb7762022-06-26 19:21:04 +010056 }
57 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010058 return await interaction.editReply({
59 embeds: [
60 new EmojiEmbed()
61 .setTitle("Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010062 .setDescription("Something went wrong and the role could not be added")
Skyler Grey75ea9172022-08-06 10:22:23 +010063 .setStatus("Danger")
64 .setEmoji("CONTROL.BLOCKCROSS")
65 ],
66 components: []
67 });
pineafan4edb7762022-06-26 19:21:04 +010068 }
Skyler Grey75ea9172022-08-06 10:22:23 +010069 return await interaction.editReply({
70 embeds: [
71 new EmojiEmbed()
72 .setTitle("Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010073 .setDescription(`The role has been ${action === "give" ? "given" : "removed"} successfully`)
Skyler Grey75ea9172022-08-06 10:22:23 +010074 .setStatus("Success")
75 .setEmoji("GUILD.ROLES.CREATE")
76 ],
77 components: []
78 });
pineafan4edb7762022-06-26 19:21:04 +010079 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 await interaction.editReply({
81 embeds: [
82 new EmojiEmbed()
83 .setEmoji("GUILD.ROLES.CREATE")
84 .setTitle("Role")
85 .setDescription("No changes were made.")
86 .setStatus("Danger")
87 ],
88 components: []
89 });
pineafan4edb7762022-06-26 19:21:04 +010090 }
pineafan63fc5e22022-08-04 22:04:10 +010091};
pineafan4f164f32022-02-26 22:07:12 +000092
PineaFan64486c42022-12-28 09:21:04 +000093const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 const member = interaction.member as GuildMember;
95 const me = interaction.guild.me!;
Skyler Greyad002172022-08-16 18:48:26 +010096 const apply = interaction.options.getMember("user") as GuildMember | null;
97 if (apply === null) throw new Error("That member is not in the server");
pineafan4edb7762022-06-26 19:21:04 +010098 // Check if Nucleus has permission to role
pineafan3a02ea32022-08-11 21:35:04 +010099 if (!me.permissions.has("MANAGE_ROLES")) throw new Error("I do not have the *Manage Roles* permission");
pineafan4edb7762022-06-26 19:21:04 +0100100 // Allow the owner to role anyone
pineafan63fc5e22022-08-04 22:04:10 +0100101 if (member.id === interaction.guild.ownerId) return true;
pineafan4edb7762022-06-26 19:21:04 +0100102 // Check if the user has manage_roles permission
pineafan3a02ea32022-08-11 21:35:04 +0100103 if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission");
pineafan4edb7762022-06-26 19:21:04 +0100104 // Allow role
pineafan4f164f32022-02-26 22:07:12 +0000105 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100106};
pineafan4f164f32022-02-26 22:07:12 +0000107
108export { command };
109export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100110export { check };