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