blob: 7f605d977ea2b5a835cdd1a89c1df6610fbdcdc0 [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
11 .setName("unban")
12 .setDescription("Unbans a user")
pineafane625d782022-05-09 18:04:32 +010013 .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> => {
pineafane625d782022-05-09 18:04:32 +010016 let bans = await interaction.guild.bans.fetch()
17 let 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())
21 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")
27 ], ephemeral: true})
28 }
29 // TODO:[Modals] Replace this with a modal
30 let confirmation = await new confirmationMessage(interaction)
31 .setEmoji("PUNISH.UNBAN.RED")
32 .setTitle("Unban")
33 .setDescription(keyValueList({
34 "user": `${resolved.user.username} [<@${resolved.user.id}>]`,
35 })
36 + `Are you sure you want to unban <@${resolved.user.id}>?`)
37 .setColor("Danger")
38 .send()
39 if (confirmation.success) {
40 try {
41 await interaction.guild.members.unban(resolved.user as User, "Unban");
42 let member = (resolved.user as User)
pineafan4edb7762022-06-26 19:21:04 +010043 try { await client.database.history.create("unban", interaction.guild.id, member, interaction.user) } catch {}
pineafane625d782022-05-09 18:04:32 +010044 // @ts-ignore
pineafan4edb7762022-06-26 19:21:04 +010045 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
pineafane625d782022-05-09 18:04:32 +010046 let data = {
47 meta: {
48 type: 'memberUnban',
49 displayName: 'Member Unbanned',
50 calculateType: 'guildMemberPunish',
51 color: NucleusColors.green,
52 emoji: "PUNISH.BAN.GREEN",
53 timestamp: new Date().getTime()
54 },
55 list: {
56 id: entry(member.id, `\`${member.id}\``),
57 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)),
60 accountCreated: entry(member.createdAt, renderDelta(member.createdAt)),
61 },
62 hidden: {
63 guild: interaction.guild.id
64 }
65 }
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")
70 .setTitle(`Unban`)
71 .setDescription("Something went wrong and the user was not unbanned")
72 .setStatus("Danger")
73 ], components: []})
74 }
pineafan4edb7762022-06-26 19:21:04 +010075 await interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010076 .setEmoji(`PUNISH.UNBAN.GREEN`)
77 .setTitle(`Unban`)
78 .setDescription("The member was unbanned")
79 .setStatus("Success")
80 ], components: []})
81 } 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")
84 .setTitle(`Unban`)
85 .setDescription("No changes were made")
86 .setStatus("Success")
87 ], components: []})
88 }
pineafan4f164f32022-02-26 22:07:12 +000089}
90
91const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafane625d782022-05-09 18:04:32 +010092 let member = (interaction.member as GuildMember)
PineappleFan19b002b2022-05-19 11:54:01 +010093 let me = (interaction.guild.me as GuildMember)
pineafane625d782022-05-09 18:04:32 +010094 // Check if Nucleus can unban members
pineafan4edb7762022-06-26 19:21:04 +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
pineafan663dc472022-05-10 18:13:47 +010097 if (member.id == interaction.guild.ownerId) return true
pineafane625d782022-05-09 18:04:32 +010098 // Check if the user has ban_members permission
pineafan4edb7762022-06-26 19:21:04 +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
101 return true
pineafan4f164f32022-02-26 22:07:12 +0000102}
103
pineafan8b4b17f2022-02-27 20:42:52 +0000104export { command, callback, check };