pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, User } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 5 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 6 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
| 11 | .setName("unban") |
| 12 | .setDescription("Unbans a user") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | .addStringOption(option => option.setName("user").setDescription("The user to unban (Username or ID)").setRequired(true)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 15 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 16 | 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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 22 | return interaction.reply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 23 | .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) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 43 | try { await client.database.history.create("unban", interaction.guild.id, member, interaction.user) } catch {} |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 44 | // @ts-ignore |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 45 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 46 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 56 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 66 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 67 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 68 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 69 | .setEmoji("PUNISH.UNBAN.RED") |
| 70 | .setTitle(`Unban`) |
| 71 | .setDescription("Something went wrong and the user was not unbanned") |
| 72 | .setStatus("Danger") |
| 73 | ], components: []}) |
| 74 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 75 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 76 | .setEmoji(`PUNISH.UNBAN.GREEN`) |
| 77 | .setTitle(`Unban`) |
| 78 | .setDescription("The member was unbanned") |
| 79 | .setStatus("Success") |
| 80 | ], components: []}) |
| 81 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 82 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 83 | .setEmoji("PUNISH.UNBAN.GREEN") |
| 84 | .setTitle(`Unban`) |
| 85 | .setDescription("No changes were made") |
| 86 | .setStatus("Success") |
| 87 | ], components: []}) |
| 88 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 92 | let member = (interaction.member as GuildMember) |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 93 | let me = (interaction.guild.me as GuildMember) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 94 | // Check if Nucleus can unban members |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 96 | // Allow the owner to unban anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 97 | if (member.id == interaction.guild.ownerId) return true |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 98 | // Check if the user has ban_members permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 99 | if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 100 | // Allow unban |
| 101 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 104 | export { command, callback, check }; |