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