blob: 88301ebe50b09f2ad4f0b420ff74652070d61ad7 [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")
12 .addStringOption(option => option.setName("user").setDescription("The user to unban (Username or ID)").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000013
pineafanbd02b4a2022-08-05 22:01:38 +010014const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010015 const bans = await interaction.guild.bans.fetch();
16 const user = interaction.options.getString("user");
17 let resolved = bans.find(ban => ban.user.id === user);
18 if (!resolved) resolved = bans.find(ban => ban.user.username.toLowerCase() === user.toLowerCase());
19 if (!resolved) resolved = bans.find(ban => ban.user.tag.toLowerCase() === user.toLowerCase());
pineafane625d782022-05-09 18:04:32 +010020 if (!resolved) {
pineafan4edb7762022-06-26 19:21:04 +010021 return interaction.reply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010022 .setTitle("Unban")
23 .setDescription(`Could not find any user called \`${user}\``)
24 .setEmoji("PUNISH.UNBAN.RED")
25 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010026 ], ephemeral: true});
pineafane625d782022-05-09 18:04:32 +010027 }
28 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010029 const confirmation = await new confirmationMessage(interaction)
pineafane625d782022-05-09 18:04:32 +010030 .setEmoji("PUNISH.UNBAN.RED")
31 .setTitle("Unban")
32 .setDescription(keyValueList({
pineafan63fc5e22022-08-04 22:04:10 +010033 "user": `${resolved.user.username} [<@${resolved.user.id}>]`
pineafane625d782022-05-09 18:04:32 +010034 })
35 + `Are you sure you want to unban <@${resolved.user.id}>?`)
36 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010037 .send();
38 if (confirmation.cancelled) return;
pineafane625d782022-05-09 18:04:32 +010039 if (confirmation.success) {
40 try {
41 await interaction.guild.members.unban(resolved.user as User, "Unban");
pineafan63fc5e22022-08-04 22:04:10 +010042 const member = (resolved.user as User);
43 await client.database.history.create("unban", interaction.guild.id, member, interaction.user);
44 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
45 const data = {
pineafane625d782022-05-09 18:04:32 +010046 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010047 type: "memberUnban",
48 displayName: "Member Unbanned",
49 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010050 color: NucleusColors.green,
51 emoji: "PUNISH.BAN.GREEN",
52 timestamp: new Date().getTime()
53 },
54 list: {
pineafanda6e5342022-07-03 10:03:16 +010055 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010056 name: entry(member.id, renderUser(member)),
57 unbanned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
58 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010059 accountCreated: entry(member.createdAt, renderDelta(member.createdAt))
pineafane625d782022-05-09 18:04:32 +010060 },
61 hidden: {
62 guild: interaction.guild.id
63 }
pineafan63fc5e22022-08-04 22:04:10 +010064 };
pineafan4edb7762022-06-26 19:21:04 +010065 log(data);
pineafane625d782022-05-09 18:04:32 +010066 } catch {
pineafan4edb7762022-06-26 19:21:04 +010067 await interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010068 .setEmoji("PUNISH.UNBAN.RED")
pineafan63fc5e22022-08-04 22:04:10 +010069 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010070 .setDescription("Something went wrong and the user was not unbanned")
71 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010072 ], components: []});
pineafane625d782022-05-09 18:04:32 +010073 }
pineafan4edb7762022-06-26 19:21:04 +010074 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +010075 .setEmoji("PUNISH.UNBAN.GREEN")
76 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010077 .setDescription("The member was unbanned")
78 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010079 ], components: []});
pineafane625d782022-05-09 18:04:32 +010080 } else {
pineafan4edb7762022-06-26 19:21:04 +010081 await interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010082 .setEmoji("PUNISH.UNBAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010083 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010084 .setDescription("No changes were made")
85 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010086 ], components: []});
pineafane625d782022-05-09 18:04:32 +010087 }
pineafan63fc5e22022-08-04 22:04:10 +010088};
pineafan4f164f32022-02-26 22:07:12 +000089
pineafanbd02b4a2022-08-05 22:01:38 +010090const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +010091 const member = (interaction.member as GuildMember);
92 const me = (interaction.guild.me as GuildMember);
pineafane625d782022-05-09 18:04:32 +010093 // Check if Nucleus can unban members
pineafane23c4ec2022-07-27 21:56:27 +010094 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +010095 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +010096 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +010097 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +010098 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +010099 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100100 return true;
101};
pineafan4f164f32022-02-26 22:07:12 +0000102
pineafan8b4b17f2022-02-27 20:42:52 +0000103export { command, callback, check };