blob: 2557b4b337391ffe083c744058f993819547bfca [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) =>
14 option
15 .setName("user")
16 .setDescription("The member to give or remove the role from")
17 .setRequired(true)
18 )
19 .addRoleOption((option) =>
20 option
21 .setName("role")
22 .setDescription("The role to give or remove")
23 .setRequired(true)
24 )
25 .addStringOption((option) =>
26 option
27 .setName("action")
28 .setDescription("The action to perform")
29 .setRequired(true)
30 .addChoices([
31 ["Add", "give"],
32 ["Remove", "remove"]
33 ])
34 );
pineafan4f164f32022-02-26 22:07:12 +000035
Skyler Grey75ea9172022-08-06 10:22:23 +010036const callback = async (
37 interaction: CommandInteraction
38): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010039 const { renderUser, renderRole } = client.logger;
40 const action = interaction.options.getString("action");
pineafan4edb7762022-06-26 19:21:04 +010041 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010042 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010043 .setEmoji("GUILD.ROLES.DELETE")
44 .setTitle("Role")
Skyler Grey75ea9172022-08-06 10:22:23 +010045 .setDescription(
46 keyValueList({
47 user: renderUser(interaction.options.getUser("user")),
48 role: renderRole(interaction.options.getRole("role"))
49 }) +
50 `\nAre you sure you want to ${
51 action === "give"
52 ? "give the role to"
53 : "remove the role from"
54 } ${interaction.options.getUser("user")}?`
55 )
pineafan4edb7762022-06-26 19:21:04 +010056 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010057 .send();
58 if (confirmation.cancelled) return;
pineafan4edb7762022-06-26 19:21:04 +010059 if (confirmation.success) {
60 try {
pineafan63fc5e22022-08-04 22:04:10 +010061 const member = interaction.options.getMember("user") as GuildMember;
62 const role = interaction.options.getRole("role") as Role;
pineafane23c4ec2022-07-27 21:56:27 +010063 if (interaction.options.getString("action") === "give") {
pineafan63fc5e22022-08-04 22:04:10 +010064 member.roles.add(role);
pineafan4edb7762022-06-26 19:21:04 +010065 } else {
pineafan63fc5e22022-08-04 22:04:10 +010066 member.roles.remove(role);
pineafan4edb7762022-06-26 19:21:04 +010067 }
68 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 return await interaction.editReply({
70 embeds: [
71 new EmojiEmbed()
72 .setTitle("Role")
73 .setDescription(
74 "Something went wrong and the role could not be added"
75 )
76 .setStatus("Danger")
77 .setEmoji("CONTROL.BLOCKCROSS")
78 ],
79 components: []
80 });
pineafan4edb7762022-06-26 19:21:04 +010081 }
Skyler Grey75ea9172022-08-06 10:22:23 +010082 return await interaction.editReply({
83 embeds: [
84 new EmojiEmbed()
85 .setTitle("Role")
86 .setDescription(
87 `The role has been ${
88 action === "give" ? "given" : "removed"
89 } successfully`
90 )
91 .setStatus("Success")
92 .setEmoji("GUILD.ROLES.CREATE")
93 ],
94 components: []
95 });
pineafan4edb7762022-06-26 19:21:04 +010096 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010097 await interaction.editReply({
98 embeds: [
99 new EmojiEmbed()
100 .setEmoji("GUILD.ROLES.CREATE")
101 .setTitle("Role")
102 .setDescription("No changes were made.")
103 .setStatus("Danger")
104 ],
105 components: []
106 });
pineafan4edb7762022-06-26 19:21:04 +0100107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108};
pineafan4f164f32022-02-26 22:07:12 +0000109
Skyler Grey75ea9172022-08-06 10:22:23 +0100110const check = (
111 interaction: CommandInteraction,
112 _defaultCheck: WrappedCheck
113) => {
114 const member = interaction.member as GuildMember;
115 const me = interaction.guild.me!;
116 const apply = interaction.options.getMember("user") as GuildMember;
117 if (member === null || me === null || apply === null)
118 throw "That member is not in the server";
pineafan4edb7762022-06-26 19:21:04 +0100119 // Check if Nucleus has permission to role
Skyler Grey75ea9172022-08-06 10:22:23 +0100120 if (!me.permissions.has("MANAGE_ROLES"))
121 throw "I do not have the *Manage Roles* permission";
pineafan4edb7762022-06-26 19:21:04 +0100122 // Allow the owner to role anyone
pineafan63fc5e22022-08-04 22:04:10 +0100123 if (member.id === interaction.guild.ownerId) return true;
pineafan4edb7762022-06-26 19:21:04 +0100124 // Check if the user has manage_roles permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 if (!member.permissions.has("MANAGE_ROLES"))
126 throw "You do not have the *Manage Roles* permission";
pineafan4edb7762022-06-26 19:21:04 +0100127 // Allow role
pineafan4f164f32022-02-26 22:07:12 +0000128 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100129};
pineafan4f164f32022-02-26 22:07:12 +0000130
131export { command };
132export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100133export { check };