blob: 8ea3ec1f12536401c99ffbda7ffec6284b69e6ac [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";
pineafane625d782022-05-09 18:04:32 +01004import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
5import keyValueList from "../../utils/generateKeyValueList.js";
6import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
10 .setName("unban")
11 .setDescription("Unbans a user")
pineafane625d782022-05-09 18:04:32 +010012 .addStringOption(option => option.setName("user").setDescription("The user to unban (Username or ID)").setRequired(true))
pineafan4f164f32022-02-26 22:07:12 +000013
pineafane625d782022-05-09 18:04:32 +010014const callback = async (interaction: CommandInteraction) => { // TODO: User search
15 let bans = await interaction.guild.bans.fetch()
16 let 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())
20 if (!resolved) {
21 return interaction.reply({embeds: [new generateEmojiEmbed()
22 .setTitle("Unban")
23 .setDescription(`Could not find any user called \`${user}\``)
24 .setEmoji("PUNISH.UNBAN.RED")
25 .setStatus("Danger")
26 ], ephemeral: true})
27 }
28 // TODO:[Modals] Replace this with a modal
29 let confirmation = await new confirmationMessage(interaction)
30 .setEmoji("PUNISH.UNBAN.RED")
31 .setTitle("Unban")
32 .setDescription(keyValueList({
33 "user": `${resolved.user.username} [<@${resolved.user.id}>]`,
34 })
35 + `Are you sure you want to unban <@${resolved.user.id}>?`)
36 .setColor("Danger")
37 .send()
38 if (confirmation.success) {
39 try {
40 await interaction.guild.members.unban(resolved.user as User, "Unban");
41 let member = (resolved.user as User)
42 // @ts-ignore
43 const { log, NucleusColors, entry, renderUser, renderDelta } = interaction.user.client.logger
44 let data = {
45 meta: {
46 type: 'memberUnban',
47 displayName: 'Member Unbanned',
48 calculateType: 'guildMemberPunish',
49 color: NucleusColors.green,
50 emoji: "PUNISH.BAN.GREEN",
51 timestamp: new Date().getTime()
52 },
53 list: {
54 id: entry(member.id, `\`${member.id}\``),
55 name: entry(member.id, renderUser(member)),
56 unbanned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
57 unbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
58 accountCreated: entry(member.createdAt, renderDelta(member.createdAt)),
59 },
60 hidden: {
61 guild: interaction.guild.id
62 }
63 }
64 log(data, member.client);
65 } catch {
66 await interaction.editReply({embeds: [new generateEmojiEmbed()
67 .setEmoji("PUNISH.UNBAN.RED")
68 .setTitle(`Unban`)
69 .setDescription("Something went wrong and the user was not unbanned")
70 .setStatus("Danger")
71 ], components: []})
72 }
73 await interaction.editReply({embeds: [new generateEmojiEmbed()
74 .setEmoji(`PUNISH.UNBAN.GREEN`)
75 .setTitle(`Unban`)
76 .setDescription("The member was unbanned")
77 .setStatus("Success")
78 ], components: []})
79 } else {
80 await interaction.editReply({embeds: [new generateEmojiEmbed()
81 .setEmoji("PUNISH.UNBAN.GREEN")
82 .setTitle(`Unban`)
83 .setDescription("No changes were made")
84 .setStatus("Success")
85 ], components: []})
86 }
pineafan4f164f32022-02-26 22:07:12 +000087}
88
89const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafane625d782022-05-09 18:04:32 +010090 let member = (interaction.member as GuildMember)
PineappleFan19b002b2022-05-19 11:54:01 +010091 let me = (interaction.guild.me as GuildMember)
pineafane625d782022-05-09 18:04:32 +010092 // Check if Nucleus can unban members
93 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
94 // Allow the owner to unban anyone
pineafan663dc472022-05-10 18:13:47 +010095 if (member.id == interaction.guild.ownerId) return true
pineafane625d782022-05-09 18:04:32 +010096 // Check if the user has ban_members permission
pineafan663dc472022-05-10 18:13:47 +010097 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
pineafane625d782022-05-09 18:04:32 +010098 // Allow unban
99 return true
pineafan4f164f32022-02-26 22:07:12 +0000100}
101
pineafan8b4b17f2022-02-27 20:42:52 +0000102export { command, callback, check };