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