blob: a368e7a3b126af3bb6b6b492127d9ff3d4113480 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
8import addPlural from "../../utils/plurals.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("softban")
pineafan8b4b17f2022-02-27 20:42:52 +000013 .setDescription("Kicks a user and deletes their messages")
14 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
pineafan73a7c4a2022-07-24 10:38:04 +010015 .addIntegerOption(option => option.setName("delete").setDescription("The days of messages to delete | Default: 0").setMinValue(0).setMaxValue(7).setRequired(false))
16 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are softbanned | Default: Yes").setRequired(false)
pineafan8b4b17f2022-02-27 20:42:52 +000017 .addChoices([["Yes", "yes"], ["No", "no"]])
18 )
pineafan4f164f32022-02-26 22:07:12 +000019
pineafan4edb7762022-06-26 19:21:04 +010020const callback = async (interaction: CommandInteraction): Promise<any> => {
21 const { renderUser } = client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000022 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010023 let reason = null;
24 let confirmation;
25 while (true) {
26 let confirmation = await new confirmationMessage(interaction)
27 .setEmoji("PUNISH.BAN.RED")
28 .setTitle("Softban")
29 .setDescription(keyValueList({
30 "user": renderUser(interaction.options.getUser("user")),
31 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
32 })
33 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
34 + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
35 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
36 .setColor("Danger")
37 .addReasonButton(reason ?? "")
38 .send(reason !== null)
39 reason = reason ?? ""
40 if (confirmation.newReason === undefined) break
41 reason = confirmation.newReason
42 }
pineafan377794f2022-04-18 19:01:01 +010043 if (confirmation.success) {
44 let dmd = false;
pineafan4edb7762022-06-26 19:21:04 +010045 let config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000046 try {
47 if (interaction.options.getString("notify") != "no") {
48 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010049 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000050 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000051 .setTitle("Softbanned")
52 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010053 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000054 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010055 ],
56 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
57 .setStyle("LINK")
58 .setLabel(config.moderation.ban.text)
59 .setURL(config.moderation.ban.link)
60 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000061 })
62 dmd = true
63 }
64 } catch {}
pineafan4edb7762022-06-26 19:21:04 +010065 let member = (interaction.options.getMember("user") as GuildMember)
pineafan8b4b17f2022-02-27 20:42:52 +000066 try {
pineafan4edb7762022-06-26 19:21:04 +010067 await member.ban({
pineafan8b4b17f2022-02-27 20:42:52 +000068 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan73a7c4a2022-07-24 10:38:04 +010069 reason: reason
pineafan5d1908e2022-02-28 21:34:47 +000070 });
pineafan4edb7762022-06-26 19:21:04 +010071 await interaction.guild.members.unban(member, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000072 } catch {
pineafan4edb7762022-06-26 19:21:04 +010073 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000074 .setEmoji("PUNISH.BAN.RED")
75 .setTitle(`Softban`)
76 .setDescription("Something went wrong and the user was not softbanned")
77 .setStatus("Danger")
78 ], components: []})
79 }
pineafan73a7c4a2022-07-24 10:38:04 +010080 try { await client.database.history.create("softban", interaction.guild.id, member.user, reason) } catch {}
pineafan5d1908e2022-02-28 21:34:47 +000081 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +010082 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000083 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
84 .setTitle(`Softban`)
85 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
86 .setStatus(failed ? "Warning" : "Success")
87 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000088 } else {
pineafan4edb7762022-06-26 19:21:04 +010089 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000090 .setEmoji("PUNISH.BAN.GREEN")
91 .setTitle(`Softban`)
92 .setDescription("No changes were made")
93 .setStatus("Success")
94 ], components: []})
95 }
pineafan4f164f32022-02-26 22:07:12 +000096}
97
98const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000099 let member = (interaction.member as GuildMember)
100 let me = (interaction.guild.me as GuildMember)
101 let apply = (interaction.options.getMember("user") as GuildMember)
102 if (member == null || me == null || apply == null) throw "That member is not in the server"
103 let memberPos = member.roles ? member.roles.highest.position : 0
104 let mePos = me.roles ? me.roles.highest.position : 0
105 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +0000106 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000107 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000108 // Check if Nucleus has permission to ban
pineafan4edb7762022-06-26 19:21:04 +0100109 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000110 // Do not allow softbanning Nucleus
PineappleFan5fe720d2022-05-19 12:01:49 +0100111 if (member.id == me.id) throw "I cannot softban myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000112 // Allow the owner to ban anyone
pineafan167bde32022-05-19 19:33:46 +0100113 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000114 // Check if the user has ban_members permission
pineafan4edb7762022-06-26 19:21:04 +0100115 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000116 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000117 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000118 // Allow softban
119 return true
pineafan4f164f32022-02-26 22:07:12 +0000120}
121
pineafan8b4b17f2022-02-27 20:42:52 +0000122export { command, callback, check };