blob: 06fe647b23fce943c110ddef6a4af4226b97c40c [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import { CommandInteraction, GuildMember, User } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafane625d782022-05-09 18:04:32 +01004import keyValueList from "../../utils/generateKeyValueList.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01006import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("unban")
11 .setDescription("Unbans a user")
Skyler Grey75ea9172022-08-06 10:22:23 +010012 .addStringOption((option) =>
13 option
14 .setName("user")
15 .setDescription("The user to unban (Username or ID)")
16 .setRequired(true)
17 );
pineafan4f164f32022-02-26 22:07:12 +000018
Skyler Grey75ea9172022-08-06 10:22:23 +010019const callback = async (
20 interaction: CommandInteraction
21): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010022 const bans = await interaction.guild.bans.fetch();
23 const user = interaction.options.getString("user");
Skyler Grey75ea9172022-08-06 10:22:23 +010024 let resolved = bans.find((ban) => ban.user.id === user);
25 if (!resolved)
26 resolved = bans.find(
27 (ban) => ban.user.username.toLowerCase() === user.toLowerCase()
28 );
29 if (!resolved)
30 resolved = bans.find(
31 (ban) => ban.user.tag.toLowerCase() === user.toLowerCase()
32 );
pineafane625d782022-05-09 18:04:32 +010033 if (!resolved) {
Skyler Grey75ea9172022-08-06 10:22:23 +010034 return interaction.reply({
35 embeds: [
36 new EmojiEmbed()
37 .setTitle("Unban")
38 .setDescription(
39 `Could not find any user called \`${user}\``
40 )
41 .setEmoji("PUNISH.UNBAN.RED")
42 .setStatus("Danger")
43 ],
44 ephemeral: true
45 });
pineafane625d782022-05-09 18:04:32 +010046 }
47 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010048 const confirmation = await new confirmationMessage(interaction)
pineafane625d782022-05-09 18:04:32 +010049 .setEmoji("PUNISH.UNBAN.RED")
50 .setTitle("Unban")
Skyler Grey75ea9172022-08-06 10:22:23 +010051 .setDescription(
52 keyValueList({
53 user: `${resolved.user.username} [<@${resolved.user.id}>]`
54 }) + `Are you sure you want to unban <@${resolved.user.id}>?`
55 )
pineafane625d782022-05-09 18:04:32 +010056 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010057 .send();
58 if (confirmation.cancelled) return;
pineafane625d782022-05-09 18:04:32 +010059 if (confirmation.success) {
60 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010061 await interaction.guild.members.unban(
62 resolved.user as User,
63 "Unban"
64 );
65 const member = resolved.user as User;
66 await client.database.history.create(
67 "unban",
68 interaction.guild.id,
69 member,
70 interaction.user
71 );
72 const { log, NucleusColors, entry, renderUser, renderDelta } =
73 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010074 const data = {
pineafane625d782022-05-09 18:04:32 +010075 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010076 type: "memberUnban",
77 displayName: "Member Unbanned",
78 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010079 color: NucleusColors.green,
80 emoji: "PUNISH.BAN.GREEN",
81 timestamp: new Date().getTime()
82 },
83 list: {
pineafanda6e5342022-07-03 10:03:16 +010084 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010085 name: entry(member.id, renderUser(member)),
Skyler Grey75ea9172022-08-06 10:22:23 +010086 unbanned: entry(
87 new Date().getTime(),
88 renderDelta(new Date().getTime())
89 ),
90 unbannedBy: entry(
91 interaction.user.id,
92 renderUser(interaction.user)
93 ),
94 accountCreated: entry(
95 member.createdAt,
96 renderDelta(member.createdAt)
97 )
pineafane625d782022-05-09 18:04:32 +010098 },
99 hidden: {
100 guild: interaction.guild.id
101 }
pineafan63fc5e22022-08-04 22:04:10 +0100102 };
pineafan4edb7762022-06-26 19:21:04 +0100103 log(data);
pineafane625d782022-05-09 18:04:32 +0100104 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 await interaction.editReply({
106 embeds: [
107 new EmojiEmbed()
108 .setEmoji("PUNISH.UNBAN.RED")
109 .setTitle("Unban")
110 .setDescription(
111 "Something went wrong and the user was not unbanned"
112 )
113 .setStatus("Danger")
114 ],
115 components: []
116 });
pineafane625d782022-05-09 18:04:32 +0100117 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 await interaction.editReply({
119 embeds: [
120 new EmojiEmbed()
121 .setEmoji("PUNISH.UNBAN.GREEN")
122 .setTitle("Unban")
123 .setDescription("The member was unbanned")
124 .setStatus("Success")
125 ],
126 components: []
127 });
pineafane625d782022-05-09 18:04:32 +0100128 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 await interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setEmoji("PUNISH.UNBAN.GREEN")
133 .setTitle("Unban")
134 .setDescription("No changes were made")
135 .setStatus("Success")
136 ],
137 components: []
138 });
pineafane625d782022-05-09 18:04:32 +0100139 }
pineafan63fc5e22022-08-04 22:04:10 +0100140};
pineafan4f164f32022-02-26 22:07:12 +0000141
pineafanbd02b4a2022-08-05 22:01:38 +0100142const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 const member = interaction.member as GuildMember;
144 const me = interaction.guild.me!;
pineafane625d782022-05-09 18:04:32 +0100145 // Check if Nucleus can unban members
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 if (!me.permissions.has("BAN_MEMBERS"))
147 throw "I do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100148 // Allow the owner to unban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100149 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100150 // Check if the user has ban_members permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 if (!member.permissions.has("BAN_MEMBERS"))
152 throw "You do not have the *Ban Members* permission";
pineafane625d782022-05-09 18:04:32 +0100153 // Allow unban
pineafan63fc5e22022-08-04 22:04:10 +0100154 return true;
155};
pineafan4f164f32022-02-26 22:07:12 +0000156
Skyler Grey75ea9172022-08-06 10:22:23 +0100157export { command, callback, check };