blob: faf1f08566f60cfe0959b18e66631bfaf8dbb477 [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";
3import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafane625d782022-05-09 18:04:32 +01005import keyValueList from "../../utils/generateKeyValueList.js";
6import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("unban")
12 .setDescription("Unbans a user")
13 .addStringOption(option => option.setName("user").setDescription("The user to unban (Username or ID)").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan4edb7762022-06-26 19:21:04 +010015const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const bans = await interaction.guild.bans.fetch();
17 const user = interaction.options.getString("user");
18 let resolved = bans.find(ban => ban.user.id === user);
19 if (!resolved) resolved = bans.find(ban => ban.user.username.toLowerCase() === user.toLowerCase());
20 if (!resolved) resolved = bans.find(ban => ban.user.tag.toLowerCase() === user.toLowerCase());
pineafane625d782022-05-09 18:04:32 +010021 if (!resolved) {
pineafan4edb7762022-06-26 19:21:04 +010022 return interaction.reply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010023 .setTitle("Unban")
24 .setDescription(`Could not find any user called \`${user}\``)
25 .setEmoji("PUNISH.UNBAN.RED")
26 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010027 ], ephemeral: true});
pineafane625d782022-05-09 18:04:32 +010028 }
29 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010030 const confirmation = await new confirmationMessage(interaction)
pineafane625d782022-05-09 18:04:32 +010031 .setEmoji("PUNISH.UNBAN.RED")
32 .setTitle("Unban")
33 .setDescription(keyValueList({
pineafan63fc5e22022-08-04 22:04:10 +010034 "user": `${resolved.user.username} [<@${resolved.user.id}>]`
pineafane625d782022-05-09 18:04:32 +010035 })
36 + `Are you sure you want to unban <@${resolved.user.id}>?`)
37 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010038 .send();
39 if (confirmation.cancelled) return;
pineafane625d782022-05-09 18:04:32 +010040 if (confirmation.success) {
41 try {
42 await interaction.guild.members.unban(resolved.user as User, "Unban");
pineafan63fc5e22022-08-04 22:04:10 +010043 const member = (resolved.user as User);
44 await client.database.history.create("unban", interaction.guild.id, member, interaction.user);
45 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
46 const data = {
pineafane625d782022-05-09 18:04:32 +010047 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010048 type: "memberUnban",
49 displayName: "Member Unbanned",
50 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010051 color: NucleusColors.green,
52 emoji: "PUNISH.BAN.GREEN",
53 timestamp: new Date().getTime()
54 },
55 list: {
pineafanda6e5342022-07-03 10:03:16 +010056 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010057 name: entry(member.id, renderUser(member)),
58 unbanned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
59 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010060 accountCreated: entry(member.createdAt, renderDelta(member.createdAt))
pineafane625d782022-05-09 18:04:32 +010061 },
62 hidden: {
63 guild: interaction.guild.id
64 }
pineafan63fc5e22022-08-04 22:04:10 +010065 };
pineafan4edb7762022-06-26 19:21:04 +010066 log(data);
pineafane625d782022-05-09 18:04:32 +010067 } catch {
pineafan4edb7762022-06-26 19:21:04 +010068 await interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010069 .setEmoji("PUNISH.UNBAN.RED")
pineafan63fc5e22022-08-04 22:04:10 +010070 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010071 .setDescription("Something went wrong and the user was not unbanned")
72 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010073 ], components: []});
pineafane625d782022-05-09 18:04:32 +010074 }
pineafan4edb7762022-06-26 19:21:04 +010075 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +010076 .setEmoji("PUNISH.UNBAN.GREEN")
77 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010078 .setDescription("The member was unbanned")
79 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010080 ], components: []});
pineafane625d782022-05-09 18:04:32 +010081 } else {
pineafan4edb7762022-06-26 19:21:04 +010082 await interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010083 .setEmoji("PUNISH.UNBAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010084 .setTitle("Unban")
pineafane625d782022-05-09 18:04:32 +010085 .setDescription("No changes were made")
86 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010087 ], components: []});
pineafane625d782022-05-09 18:04:32 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089};
pineafan4f164f32022-02-26 22:07:12 +000090
91const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +010092 const member = (interaction.member as GuildMember);
93 const me = (interaction.guild.me as GuildMember);
pineafane625d782022-05-09 18:04:32 +010094 // Check if Nucleus can unban members
pineafane23c4ec2022-07-27 21:56:27 +010095 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +010096 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +010097 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +010098 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +010099 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100100 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100101 return true;
102};
pineafan4f164f32022-02-26 22:07:12 +0000103
pineafan8b4b17f2022-02-27 20:42:52 +0000104export { command, callback, check };