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"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 4 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 5 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 7 | |
| 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | .setName("unban") |
| 11 | .setDescription("Unbans a user") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | .addStringOption((option) => |
| 13 | option |
| 14 | .setName("user") |
| 15 | .setDescription("The user to unban (Username or ID)") |
| 16 | .setRequired(true) |
| 17 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | const callback = async ( |
| 20 | interaction: CommandInteraction |
| 21 | ): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | const bans = await interaction.guild.bans.fetch(); |
| 23 | const user = interaction.options.getString("user"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | let resolved = bans.find((ban) => ban.user.id === user); |
| 25 | if (!resolved) |
| 26 | resolved = bans.find( |
| 27 | (ban) => ban.user.username.toLowerCase() === user.toLowerCase() |
| 28 | ); |
| 29 | if (!resolved) |
| 30 | resolved = bans.find( |
| 31 | (ban) => ban.user.tag.toLowerCase() === user.toLowerCase() |
| 32 | ); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 33 | if (!resolved) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | return interaction.reply({ |
| 35 | embeds: [ |
| 36 | new EmojiEmbed() |
| 37 | .setTitle("Unban") |
| 38 | .setDescription( |
| 39 | `Could not find any user called \`${user}\`` |
| 40 | ) |
| 41 | .setEmoji("PUNISH.UNBAN.RED") |
| 42 | .setStatus("Danger") |
| 43 | ], |
| 44 | ephemeral: true |
| 45 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 46 | } |
| 47 | // TODO:[Modals] Replace this with a modal |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | const confirmation = await new confirmationMessage(interaction) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 49 | .setEmoji("PUNISH.UNBAN.RED") |
| 50 | .setTitle("Unban") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 51 | .setDescription( |
| 52 | keyValueList({ |
| 53 | user: `${resolved.user.username} [<@${resolved.user.id}>]` |
| 54 | }) + `Are you sure you want to unban <@${resolved.user.id}>?` |
| 55 | ) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 56 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | .send(); |
| 58 | if (confirmation.cancelled) return; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 59 | if (confirmation.success) { |
| 60 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | await interaction.guild.members.unban( |
| 62 | resolved.user as User, |
| 63 | "Unban" |
| 64 | ); |
| 65 | const member = resolved.user as User; |
| 66 | await client.database.history.create( |
| 67 | "unban", |
| 68 | interaction.guild.id, |
| 69 | member, |
| 70 | interaction.user |
| 71 | ); |
| 72 | const { log, NucleusColors, entry, renderUser, renderDelta } = |
| 73 | client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 75 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | type: "memberUnban", |
| 77 | displayName: "Member Unbanned", |
| 78 | calculateType: "guildMemberPunish", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 79 | color: NucleusColors.green, |
| 80 | emoji: "PUNISH.BAN.GREEN", |
| 81 | timestamp: new Date().getTime() |
| 82 | }, |
| 83 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 84 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 85 | name: entry(member.id, renderUser(member)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | unbanned: entry( |
| 87 | new Date().getTime(), |
| 88 | renderDelta(new Date().getTime()) |
| 89 | ), |
| 90 | unbannedBy: entry( |
| 91 | interaction.user.id, |
| 92 | renderUser(interaction.user) |
| 93 | ), |
| 94 | accountCreated: entry( |
| 95 | member.createdAt, |
| 96 | renderDelta(member.createdAt) |
| 97 | ) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 98 | }, |
| 99 | hidden: { |
| 100 | guild: interaction.guild.id |
| 101 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 103 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 104 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | await interaction.editReply({ |
| 106 | embeds: [ |
| 107 | new EmojiEmbed() |
| 108 | .setEmoji("PUNISH.UNBAN.RED") |
| 109 | .setTitle("Unban") |
| 110 | .setDescription( |
| 111 | "Something went wrong and the user was not unbanned" |
| 112 | ) |
| 113 | .setStatus("Danger") |
| 114 | ], |
| 115 | components: [] |
| 116 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 117 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | await interaction.editReply({ |
| 119 | embeds: [ |
| 120 | new EmojiEmbed() |
| 121 | .setEmoji("PUNISH.UNBAN.GREEN") |
| 122 | .setTitle("Unban") |
| 123 | .setDescription("The member was unbanned") |
| 124 | .setStatus("Success") |
| 125 | ], |
| 126 | components: [] |
| 127 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 128 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | await interaction.editReply({ |
| 130 | embeds: [ |
| 131 | new EmojiEmbed() |
| 132 | .setEmoji("PUNISH.UNBAN.GREEN") |
| 133 | .setTitle("Unban") |
| 134 | .setDescription("No changes were made") |
| 135 | .setStatus("Success") |
| 136 | ], |
| 137 | components: [] |
| 138 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 139 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 140 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 141 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 142 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 143 | const member = interaction.member as GuildMember; |
| 144 | const me = interaction.guild.me!; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 145 | // Check if Nucleus can unban members |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | if (!me.permissions.has("BAN_MEMBERS")) |
| 147 | throw "I do not have the *Ban Members* permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 148 | // Allow the owner to unban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 150 | // Check if the user has ban_members permission |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 151 | if (!member.permissions.has("BAN_MEMBERS")) |
| 152 | throw "You do not have the *Ban Members* permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 153 | // Allow unban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 154 | return true; |
| 155 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 156 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | export { command, callback, check }; |