blob: 9ba10e144568cf3daeb50d980d79ce1046ed2369 [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
Skyler Grey11236ba2022-08-08 21:13:33 +010016const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010017 const bans = await interaction.guild.bans.fetch();
18 const user = interaction.options.getString("user");
Skyler Grey75ea9172022-08-06 10:22:23 +010019 let resolved = bans.find((ban) => ban.user.id === user);
Skyler Grey11236ba2022-08-08 21:13:33 +010020 if (!resolved) resolved = bans.find((ban) => ban.user.username.toLowerCase() === user.toLowerCase());
21 if (!resolved) resolved = bans.find((ban) => ban.user.tag.toLowerCase() === user.toLowerCase());
pineafane625d782022-05-09 18:04:32 +010022 if (!resolved) {
Skyler Grey75ea9172022-08-06 10:22:23 +010023 return interaction.reply({
24 embeds: [
25 new EmojiEmbed()
26 .setTitle("Unban")
Skyler Grey11236ba2022-08-08 21:13:33 +010027 .setDescription(`Could not find any user called \`${user}\``)
Skyler Grey75ea9172022-08-06 10:22:23 +010028 .setEmoji("PUNISH.UNBAN.RED")
29 .setStatus("Danger")
30 ],
31 ephemeral: true
32 });
pineafane625d782022-05-09 18:04:32 +010033 }
34 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010035 const confirmation = await new confirmationMessage(interaction)
pineafane625d782022-05-09 18:04:32 +010036 .setEmoji("PUNISH.UNBAN.RED")
37 .setTitle("Unban")
Skyler Grey75ea9172022-08-06 10:22:23 +010038 .setDescription(
39 keyValueList({
40 user: `${resolved.user.username} [<@${resolved.user.id}>]`
41 }) + `Are you sure you want to unban <@${resolved.user.id}>?`
42 )
pineafane625d782022-05-09 18:04:32 +010043 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010044 .send();
45 if (confirmation.cancelled) return;
pineafane625d782022-05-09 18:04:32 +010046 if (confirmation.success) {
47 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010048 await interaction.guild.members.unban(resolved.user as User, "Unban");
Skyler Grey75ea9172022-08-06 10:22:23 +010049 const member = resolved.user as User;
Skyler Grey11236ba2022-08-08 21:13:33 +010050 await client.database.history.create("unban", interaction.guild.id, member, interaction.user);
51 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010052 const data = {
pineafane625d782022-05-09 18:04:32 +010053 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010054 type: "memberUnban",
55 displayName: "Member Unbanned",
56 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010057 color: NucleusColors.green,
58 emoji: "PUNISH.BAN.GREEN",
59 timestamp: new Date().getTime()
60 },
61 list: {
pineafanda6e5342022-07-03 10:03:16 +010062 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010063 name: entry(member.id, renderUser(member)),
Skyler Grey11236ba2022-08-08 21:13:33 +010064 unbanned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
65 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
66 accountCreated: entry(member.createdAt, renderDelta(member.createdAt))
pineafane625d782022-05-09 18:04:32 +010067 },
68 hidden: {
69 guild: interaction.guild.id
70 }
pineafan63fc5e22022-08-04 22:04:10 +010071 };
pineafan4edb7762022-06-26 19:21:04 +010072 log(data);
pineafane625d782022-05-09 18:04:32 +010073 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 await interaction.editReply({
75 embeds: [
76 new EmojiEmbed()
77 .setEmoji("PUNISH.UNBAN.RED")
78 .setTitle("Unban")
Skyler Grey11236ba2022-08-08 21:13:33 +010079 .setDescription("Something went wrong and the user was not unbanned")
Skyler Grey75ea9172022-08-06 10:22:23 +010080 .setStatus("Danger")
81 ],
82 components: []
83 });
pineafane625d782022-05-09 18:04:32 +010084 }
Skyler Grey75ea9172022-08-06 10:22:23 +010085 await interaction.editReply({
86 embeds: [
87 new EmojiEmbed()
88 .setEmoji("PUNISH.UNBAN.GREEN")
89 .setTitle("Unban")
90 .setDescription("The member was unbanned")
91 .setStatus("Success")
92 ],
93 components: []
94 });
pineafane625d782022-05-09 18:04:32 +010095 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010096 await interaction.editReply({
97 embeds: [
98 new EmojiEmbed()
99 .setEmoji("PUNISH.UNBAN.GREEN")
100 .setTitle("Unban")
101 .setDescription("No changes were made")
102 .setStatus("Success")
103 ],
104 components: []
105 });
pineafane625d782022-05-09 18:04:32 +0100106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107};
pineafan4f164f32022-02-26 22:07:12 +0000108
pineafanbd02b4a2022-08-05 22:01:38 +0100109const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 const member = interaction.member as GuildMember;
111 const me = interaction.guild.me!;
pineafane625d782022-05-09 18:04:32 +0100112 // Check if Nucleus can unban members
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100114 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100115 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100116 // Check if the user has ban_members permission
Skyler Grey11236ba2022-08-08 21:13:33 +0100117 if (!member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100118 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100119 return true;
120};
pineafan4f164f32022-02-26 22:07:12 +0000121
Skyler Grey75ea9172022-08-06 10:22:23 +0100122export { command, callback, check };