blob: d34f303fa81ab3a2ef8f8c7abbf033ffad114a53 [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import { CommandInteraction, GuildMember, User } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
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();
19 const user = interaction.options.getString("user");
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 Grey11236ba2022-08-08 21:13:33 +010051 await client.database.history.create("unban", interaction.guild.id, member, interaction.user);
52 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010053 const data = {
pineafane625d782022-05-09 18:04:32 +010054 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010055 type: "memberUnban",
56 displayName: "Member Unbanned",
57 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010058 color: NucleusColors.green,
59 emoji: "PUNISH.BAN.GREEN",
60 timestamp: new Date().getTime()
61 },
62 list: {
pineafanda6e5342022-07-03 10:03:16 +010063 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010064 name: entry(member.id, renderUser(member)),
Skyler Grey11236ba2022-08-08 21:13:33 +010065 unbanned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
66 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
67 accountCreated: entry(member.createdAt, renderDelta(member.createdAt))
pineafane625d782022-05-09 18:04:32 +010068 },
69 hidden: {
70 guild: interaction.guild.id
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 };
pineafan4edb7762022-06-26 19:21:04 +010073 log(data);
pineafane625d782022-05-09 18:04:32 +010074 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 await interaction.editReply({
76 embeds: [
77 new EmojiEmbed()
78 .setEmoji("PUNISH.UNBAN.RED")
79 .setTitle("Unban")
Skyler Grey11236ba2022-08-08 21:13:33 +010080 .setDescription("Something went wrong and the user was not unbanned")
Skyler Grey75ea9172022-08-06 10:22:23 +010081 .setStatus("Danger")
82 ],
83 components: []
84 });
pineafane625d782022-05-09 18:04:32 +010085 }
Skyler Grey75ea9172022-08-06 10:22:23 +010086 await interaction.editReply({
87 embeds: [
88 new EmojiEmbed()
89 .setEmoji("PUNISH.UNBAN.GREEN")
90 .setTitle("Unban")
91 .setDescription("The member was unbanned")
92 .setStatus("Success")
93 ],
94 components: []
95 });
pineafane625d782022-05-09 18:04:32 +010096 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010097 await interaction.editReply({
98 embeds: [
99 new EmojiEmbed()
100 .setEmoji("PUNISH.UNBAN.GREEN")
101 .setTitle("Unban")
102 .setDescription("No changes were made")
103 .setStatus("Success")
104 ],
105 components: []
106 });
pineafane625d782022-05-09 18:04:32 +0100107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108};
pineafan4f164f32022-02-26 22:07:12 +0000109
pineafanbd02b4a2022-08-05 22:01:38 +0100110const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000111 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 const member = interaction.member as GuildMember;
113 const me = interaction.guild.me!;
pineafane625d782022-05-09 18:04:32 +0100114 // Check if Nucleus can unban members
pineafan3a02ea32022-08-11 21:35:04 +0100115 if (!me.permissions.has("BAN_MEMBERS")) throw new Error("I do not have the *Ban Members* permission");
pineafane625d782022-05-09 18:04:32 +0100116 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100117 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100118 // Check if the user has ban_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100119 if (!member.permissions.has("BAN_MEMBERS")) throw new Error("You do not have the *Ban Members* permission");
pineafane625d782022-05-09 18:04:32 +0100120 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100121 return true;
122};
pineafan4f164f32022-02-26 22:07:12 +0000123
Skyler Grey75ea9172022-08-06 10:22:23 +0100124export { command, callback, check };