blob: ac4823ebb75363fbd4e428c9c2da81b949e99b39 [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;
PineaFan0d06edc2023-01-17 22:10:31 +000051 await client.database.history.create("unban", interaction.guild.id, member, interaction.user, "No reason provided");
Skyler Grey11236ba2022-08-08 21:13:33 +010052 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)),
PineaFan0d06edc2023-01-17 22:10:31 +000067 accountCreated: entry(member.createdTimestamp, renderDelta(member.createdTimestamp))
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
TheCodedProff86ba092023-01-27 17:10:07 -0500110const check = (interaction: CommandInteraction, partial: boolean = false) => {
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;
TheCodedProff86ba092023-01-27 17:10:07 -0500113 // Check if the user has ban_members permission
114 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
115 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000116 const me = interaction.guild.members.me!;
pineafane625d782022-05-09 18:04:32 +0100117 // Check if Nucleus can unban members
PineaFan0d06edc2023-01-17 22:10:31 +0000118 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100119 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100120 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100121 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100122 return true;
123};
pineafan4f164f32022-02-26 22:07:12 +0000124
Skyler Grey75ea9172022-08-06 10:22:23 +0100125export { command, callback, check };