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