blob: 739741409e4ed38cdddee31f31517bbd03443428 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import type { CommandInteraction, GuildMember, User } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05002import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafane625d782022-05-09 18:04:32 +01004import keyValueList from "../../utils/generateKeyValueList.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01006import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("unban")
11 .setDescription("Unbans a user")
Skyler Grey75ea9172022-08-06 10:22:23 +010012 .addStringOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010013 option.setName("user").setDescription("The user to unban (Username or ID)").setRequired(true)
Skyler Grey75ea9172022-08-06 10:22:23 +010014 );
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan3a02ea32022-08-11 21:35:04 +010016const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000017 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010018 const bans = await interaction.guild.bans.fetch();
PineaFan0d06edc2023-01-17 22:10:31 +000019 const user = interaction.options.get("user")?.value as string;
Skyler Grey75ea9172022-08-06 10:22:23 +010020 let resolved = bans.find((ban) => ban.user.id === user);
Skyler Grey11236ba2022-08-08 21:13:33 +010021 if (!resolved) resolved = bans.find((ban) => ban.user.username.toLowerCase() === user.toLowerCase());
22 if (!resolved) resolved = bans.find((ban) => ban.user.tag.toLowerCase() === user.toLowerCase());
pineafane625d782022-05-09 18:04:32 +010023 if (!resolved) {
Skyler Grey75ea9172022-08-06 10:22:23 +010024 return interaction.reply({
25 embeds: [
26 new EmojiEmbed()
27 .setTitle("Unban")
Skyler Grey11236ba2022-08-08 21:13:33 +010028 .setDescription(`Could not find any user called \`${user}\``)
Skyler Grey75ea9172022-08-06 10:22:23 +010029 .setEmoji("PUNISH.UNBAN.RED")
30 .setStatus("Danger")
31 ],
32 ephemeral: true
33 });
pineafane625d782022-05-09 18:04:32 +010034 }
35 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010036 const confirmation = await new confirmationMessage(interaction)
pineafane625d782022-05-09 18:04:32 +010037 .setEmoji("PUNISH.UNBAN.RED")
38 .setTitle("Unban")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .setDescription(
40 keyValueList({
41 user: `${resolved.user.username} [<@${resolved.user.id}>]`
42 }) + `Are you sure you want to unban <@${resolved.user.id}>?`
43 )
pineafane625d782022-05-09 18:04:32 +010044 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010045 .send();
46 if (confirmation.cancelled) return;
pineafane625d782022-05-09 18:04:32 +010047 if (confirmation.success) {
48 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010049 await interaction.guild.members.unban(resolved.user as User, "Unban");
Skyler Grey75ea9172022-08-06 10:22:23 +010050 const member = resolved.user as User;
Skyler Greyda16adf2023-03-05 10:22:12 +000051 await client.database.history.create(
52 "unban",
53 interaction.guild.id,
54 member,
55 interaction.user,
56 "No reason provided"
57 );
Skyler Grey11236ba2022-08-08 21:13:33 +010058 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010059 const data = {
pineafane625d782022-05-09 18:04:32 +010060 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010061 type: "memberUnban",
62 displayName: "Member Unbanned",
63 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010064 color: NucleusColors.green,
65 emoji: "PUNISH.BAN.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -050066 timestamp: Date.now()
pineafane625d782022-05-09 18:04:32 +010067 },
68 list: {
pineafanda6e5342022-07-03 10:03:16 +010069 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010070 name: entry(member.id, renderUser(member)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050071 unbanned: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey11236ba2022-08-08 21:13:33 +010072 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
PineaFan0d06edc2023-01-17 22:10:31 +000073 accountCreated: entry(member.createdTimestamp, renderDelta(member.createdTimestamp))
pineafane625d782022-05-09 18:04:32 +010074 },
75 hidden: {
76 guild: interaction.guild.id
77 }
pineafan63fc5e22022-08-04 22:04:10 +010078 };
pineafan4edb7762022-06-26 19:21:04 +010079 log(data);
pineafane625d782022-05-09 18:04:32 +010080 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 await interaction.editReply({
82 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.UNBAN.RED")
85 .setTitle("Unban")
Skyler Grey11236ba2022-08-08 21:13:33 +010086 .setDescription("Something went wrong and the user was not unbanned")
Skyler Grey75ea9172022-08-06 10:22:23 +010087 .setStatus("Danger")
88 ],
89 components: []
90 });
pineafane625d782022-05-09 18:04:32 +010091 }
Skyler Grey75ea9172022-08-06 10:22:23 +010092 await interaction.editReply({
93 embeds: [
94 new EmojiEmbed()
95 .setEmoji("PUNISH.UNBAN.GREEN")
96 .setTitle("Unban")
97 .setDescription("The member was unbanned")
98 .setStatus("Success")
99 ],
100 components: []
101 });
pineafane625d782022-05-09 18:04:32 +0100102 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 await interaction.editReply({
104 embeds: [
105 new EmojiEmbed()
106 .setEmoji("PUNISH.UNBAN.GREEN")
107 .setTitle("Unban")
108 .setDescription("No changes were made")
109 .setStatus("Success")
110 ],
111 components: []
112 });
pineafane625d782022-05-09 18:04:32 +0100113 }
pineafan63fc5e22022-08-04 22:04:10 +0100114};
pineafan4f164f32022-02-26 22:07:12 +0000115
TheCodedProff86ba092023-01-27 17:10:07 -0500116const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000117 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500119 // Check if the user has ban_members permission
120 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
121 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000122 const me = interaction.guild.members.me!;
pineafane625d782022-05-09 18:04:32 +0100123 // Check if Nucleus can unban members
PineaFan0d06edc2023-01-17 22:10:31 +0000124 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100125 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100126 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100127 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100128 return true;
129};
pineafan4f164f32022-02-26 22:07:12 +0000130
Skyler Grey75ea9172022-08-06 10:22:23 +0100131export { command, callback, check };