PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import type { CommandInteraction, GuildMember, User } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 2 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
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; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 51 | await client.database.history.create( |
| 52 | "unban", |
| 53 | interaction.guild.id, |
| 54 | member, |
| 55 | interaction.user, |
| 56 | "No reason provided" |
| 57 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 58 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 60 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | type: "memberUnban", |
| 62 | displayName: "Member Unbanned", |
| 63 | calculateType: "guildMemberPunish", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 64 | color: NucleusColors.green, |
| 65 | emoji: "PUNISH.BAN.GREEN", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 66 | timestamp: Date.now() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 67 | }, |
| 68 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 69 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 70 | name: entry(member.id, renderUser(member)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 71 | unbanned: entry(Date.now(), renderDelta(Date.now())), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 72 | unbannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 73 | accountCreated: entry(member.createdTimestamp, renderDelta(member.createdTimestamp)) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 74 | }, |
| 75 | hidden: { |
| 76 | guild: interaction.guild.id |
| 77 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 79 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 80 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | await interaction.editReply({ |
| 82 | embeds: [ |
| 83 | new EmojiEmbed() |
| 84 | .setEmoji("PUNISH.UNBAN.RED") |
| 85 | .setTitle("Unban") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 86 | .setDescription("Something went wrong and the user was not unbanned") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | .setStatus("Danger") |
| 88 | ], |
| 89 | components: [] |
| 90 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 91 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | await interaction.editReply({ |
| 93 | embeds: [ |
| 94 | new EmojiEmbed() |
| 95 | .setEmoji("PUNISH.UNBAN.GREEN") |
| 96 | .setTitle("Unban") |
| 97 | .setDescription("The member was unbanned") |
| 98 | .setStatus("Success") |
| 99 | ], |
| 100 | components: [] |
| 101 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 102 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | await interaction.editReply({ |
| 104 | embeds: [ |
| 105 | new EmojiEmbed() |
| 106 | .setEmoji("PUNISH.UNBAN.GREEN") |
| 107 | .setTitle("Unban") |
| 108 | .setDescription("No changes were made") |
| 109 | .setStatus("Success") |
| 110 | ], |
| 111 | components: [] |
| 112 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 113 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 114 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 115 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 116 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 117 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 119 | // Check if the user has ban_members permission |
| 120 | if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission"; |
| 121 | if (partial) return true; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 122 | const me = interaction.guild.members.me!; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 123 | // Check if Nucleus can unban members |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 124 | 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] | 125 | // Allow the owner to unban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 127 | // Allow unban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | return true; |
| 129 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 130 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | export { command, callback, check }; |