blob: 2266a1a8a1e9375fa9abcb350f4ff0b60f289d74 [file] [log] [blame]
import { LinkWarningFooter, LoadingEmbed } from "../../utils/defaults.js";
import Discord, {
CommandInteraction,
GuildMember,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ButtonInteraction
} from "discord.js";
import type { SlashCommandSubcommandBuilder } from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
import confirmationMessage from "../../utils/confirmationMessage.js";
import keyValueList from "../../utils/generateKeyValueList.js";
// @ts-expect-error
import humanizeDuration from "humanize-duration";
import client from "../../utils/client.js";
import { areTicketsEnabled, create } from "../../actions/createModActionTicket.js";
const command = (builder: SlashCommandSubcommandBuilder) =>
builder
.setName("mute")
// .setNameLocalizations({"ru": "silence"})
.setDescription("Mutes a member, stopping them from talking in the server")
.addUserOption((option) => option.setName("user").setDescription("The user to mute").setRequired(true))
.addIntegerOption((option) =>
option
.setName("days")
.setDescription("The number of days to mute the user for | Default: 0")
.setMinValue(0)
.setMaxValue(27)
.setRequired(false)
)
.addIntegerOption((option) =>
option
.setName("hours")
.setDescription("The number of hours to mute the user for | Default: 0")
.setMinValue(0)
.setMaxValue(23)
.setRequired(false)
)
.addIntegerOption((option) =>
option
.setName("minutes")
.setDescription("The number of minutes to mute the user for | Default: 0")
.setMinValue(0)
.setMaxValue(59)
.setRequired(false)
)
.addIntegerOption((option) =>
option
.setName("seconds")
.setDescription("The number of seconds to mute the user for | Default: 0")
.setMinValue(0)
.setMaxValue(59)
.setRequired(false)
);
const callback = async (
interaction: CommandInteraction | ButtonInteraction,
member?: GuildMember
): Promise<unknown> => {
if (!interaction.guild) return;
const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger;
let time: { days: number; hours: number; minutes: number; seconds: number } | null = null;
if (!interaction.isButton()) {
member = interaction.options.getMember("user") as GuildMember;
time = {
days: (interaction.options.get("days")?.value as number | null) ?? 0,
hours: (interaction.options.get("hours")?.value as number | null) ?? 0,
minutes: (interaction.options.get("minutes")?.value as number | null) ?? 0,
seconds: (interaction.options.get("seconds")?.value as number | null) ?? 0
};
} else {
time = { days: 0, hours: 0, minutes: 0, seconds: 0 };
}
if (!member) return;
const config = await client.database.guilds.read(interaction.guild.id);
let serverSettingsDescription = config.moderation.mute.timeout ? "given a timeout" : "";
if (config.moderation.mute.role)
serverSettingsDescription +=
(serverSettingsDescription ? " and " : "") + `given the <@&${config.moderation.mute.role}> role`;
let muteTime = time.days * 24 * 60 * 60 + time.hours * 60 * 60 + time.minutes * 60 + time.seconds;
if (muteTime === 0) {
const m = await interaction.reply({
embeds: [
new EmojiEmbed()
.setEmoji("PUNISH.MUTE.GREEN")
.setTitle("Mute")
.setDescription("How long should the user be muted for?")
.setStatus("Success")
],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
new Discord.ButtonBuilder().setCustomId("1m").setLabel("1 Minute").setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder()
.setCustomId("10m")
.setLabel("10 Minutes")
.setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder()
.setCustomId("30m")
.setLabel("30 Minutes")
.setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder().setCustomId("1h").setLabel("1 Hour").setStyle(ButtonStyle.Secondary)
]),
new ActionRowBuilder<ButtonBuilder>().addComponents([
new Discord.ButtonBuilder().setCustomId("6h").setLabel("6 Hours").setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder().setCustomId("12h").setLabel("12 Hours").setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder().setCustomId("1d").setLabel("1 Day").setStyle(ButtonStyle.Secondary),
new Discord.ButtonBuilder().setCustomId("1w").setLabel("1 Week").setStyle(ButtonStyle.Secondary)
]),
new ActionRowBuilder<ButtonBuilder>().addComponents([
new Discord.ButtonBuilder()
.setCustomId("cancel")
.setLabel("Cancel")
.setStyle(ButtonStyle.Danger)
.setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
])
],
ephemeral: true,
fetchReply: true
});
let component;
try {
component = await m.awaitMessageComponent({
filter: (i) => {
return i.user.id === interaction.user.id && i.channelId === interaction.channelId;
},
time: 300000
});
} catch {
return;
}
component.deferUpdate();
if (component.customId === "cancel")
return interaction.editReply({
embeds: [
new EmojiEmbed()
.setEmoji("PUNISH.MUTE.RED")
.setTitle("Mute")
.setDescription("Mute cancelled")
.setStatus("Danger")
]
});
switch (component.customId) {
case "1m": {
muteTime = 60;
break;
}
case "10m": {
muteTime = 60 * 10;
break;
}
case "30m": {
muteTime = 60 * 30;
break;
}
case "1h": {
muteTime = 60 * 60;
break;
}
case "6h": {
muteTime = 60 * 60 * 6;
break;
}
case "12h": {
muteTime = 60 * 60 * 12;
break;
}
case "1d": {
muteTime = 60 * 60 * 24;
break;
}
case "1w": {
muteTime = 60 * 60 * 24 * 7;
break;
}
}
} else {
await interaction.reply({
embeds: LoadingEmbed,
ephemeral: true,
fetchReply: true
});
}
// TODO:[Modals] Replace this with a modal
let reason: string | null = null;
let notify = true;
let createAppealTicket = false;
let confirmation;
let timedOut = false;
let success = false;
do {
confirmation = await new confirmationMessage(interaction)
.setEmoji("PUNISH.MUTE.RED")
.setTitle("Mute")
.setDescription(
keyValueList({
user: renderUser(member.user),
time: `${humanizeDuration(muteTime * 1000, {
round: true
})}`,
reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
}) +
"The user will be " +
serverSettingsDescription +
"\n\n" +
`Are you sure you want to mute <@!${member.id}>?`
)
.setColor("Danger")
.addCustomBoolean(
"appeal",
"Create appeal ticket",
!(await areTicketsEnabled(interaction.guild.id)),
async () => await create(interaction.guild!, member!.user, interaction.user, reason),
"An appeal ticket will be created when Confirm is clicked",
null,
"CONTROL.TICKET",
createAppealTicket
)
.addCustomBoolean(
"notify",
"Notify user",
false,
null,
"The user will be sent a DM",
null,
"ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
notify
)
.addReasonButton(reason ?? "")
.setFailedMessage("No changes were made", "Success", "PUNISH.MUTE.GREEN")
.send(true);
reason = reason ?? "";
if (confirmation.cancelled) timedOut = true;
else if (confirmation.success) success = true;
else if (confirmation.newReason) reason = confirmation.newReason;
else if (confirmation.components) {
notify = confirmation.components["notify"]!.active;
createAppealTicket = confirmation.components["appeal"]!.active;
}
} while (!timedOut && !success);
if (timedOut || !confirmation.success) return;
const status: { timeout: boolean | null; role: boolean | null; dm: boolean | null } = {
timeout: null,
role: null,
dm: null
};
let dmMessage;
try {
if (notify) {
if (reason) {
reason = reason
.split("\n")
.map((line) => "> " + line)
.join("\n");
}
const messageData: {
embeds: EmojiEmbed[];
components: ActionRowBuilder<ButtonBuilder>[];
} = {
embeds: [
new EmojiEmbed()
.setEmoji("PUNISH.MUTE.RED")
.setTitle("Muted")
.setDescription(
`You have been muted in ${interaction.guild.name}` +
(reason ? ` for:\n${reason}` : ".\n*No reason was provided*") +
"\n\n" +
`You will be unmuted at: <t:${Math.round(Date.now() / 1000) + muteTime}:D> at ` +
`<t:${Math.round(Date.now() / 1000) + muteTime}:T> (<t:${
Math.round(Date.now() / 1000) + muteTime
}:R>)` +
"\n\n" +
(createAppealTicket
? `You can appeal this in the ticket created in <#${
confirmation.components!["appeal"]!.response
}>`
: "")
)
.setStatus("Danger")
],
components: []
};
if (config.moderation.mute.text && config.moderation.mute.link) {
messageData.embeds[0]!.setFooter(LinkWarningFooter);
messageData.components.push(
new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setLabel(config.moderation.mute.text)
.setURL(config.moderation.mute.link.replaceAll("{id}", member.id))
)
);
}
dmMessage = await member.send(messageData);
status.dm = true;
}
} catch {
status.dm = false;
}
try {
if (config.moderation.mute.timeout) {
await member.timeout(muteTime * 1000, reason || "*No reason provided*");
if (config.moderation.mute.role !== null) {
await member.roles.add(config.moderation.mute.role);
await client.database.eventScheduler.schedule(
"naturalUnmute",
(Date.now() + muteTime * 1000).toString(),
{
guild: interaction.guild.id,
user: member.id,
expires: Date.now() + muteTime * 1000
}
);
}
} else {
status.timeout = true;
}
} catch {
status.timeout = false;
}
try {
if (config.moderation.mute.role !== null) {
await member.roles.add(config.moderation.mute.role);
await client.database.eventScheduler.schedule("unmuteRole", (Date.now() + muteTime * 1000).toString(), {
guild: interaction.guild.id,
user: member.id,
role: config.moderation.mute.role
});
} else {
status.role = true;
}
} catch {
status.role = false;
}
const countTrue = (items: (boolean | null)[]) => items.filter((item) => item === true).length;
const requiredPunishments = countTrue([config.moderation.mute.timeout, config.moderation.mute.role !== null]);
const actualPunishments = countTrue([status.timeout, status.role]);
await client.database.history.create("mute", interaction.guild.id, member.user, interaction.user, reason);
if (requiredPunishments !== actualPunishments) {
const messages = [];
if (config.moderation.mute.timeout) messages.push(`The member was ${status.timeout ? "" : "not "}timed out`);
if (config.moderation.mute.role !== null)
messages.push(`The member was ${status.role ? "" : "not "}given the mute role`);
messages.push(`The member was not sent a DM`);
if (dmMessage && actualPunishments === 0) await dmMessage.delete();
await interaction.editReply({
embeds: [
new EmojiEmbed()
.setEmoji("PUNISH.MUTE." + (actualPunishments > 0 ? "YELLOW" : "RED"))
.setTitle("Mute")
.setDescription(
"Mute " +
(actualPunishments > 0 ? "partially" : "failed") +
":\n" +
messages.map((message) => `> ${message}`).join("\n")
)
.setStatus(actualPunishments > 0 ? "Warning" : "Danger")
],
components: []
});
}
const data = {
meta: {
type: "memberMute",
displayName: "Member Muted",
calculateType: "guildMemberPunish",
color: NucleusColors.yellow,
emoji: "PUNISH.WARN.YELLOW",
timestamp: Date.now()
},
list: {
memberId: entry(member.user.id, `\`${member.user.id}\``),
name: entry(member.user.id, renderUser(member.user)),
mutedUntil: entry((Date.now() + muteTime * 1000).toString(), renderDelta(Date.now() + muteTime * 1000)),
muted: entry(new Date().getTime.toString(), renderDelta(Date.now() - 1000)),
mutedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)),
reason: entry(reason, reason ? reason : "*No reason provided*")
},
separate: {
end:
getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
` The user was ${notify ? "" : "not "}notified`
},
hidden: {
guild: interaction.guild.id
}
};
log(data);
const failed = !status.dm && notify;
await interaction.editReply({
embeds: [
new EmojiEmbed()
.setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
.setTitle("Mute")
.setDescription(
"The member was muted" +
(failed ? ", but could not be notified" : "") +
(createAppealTicket
? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
: "")
)
.setStatus(failed ? "Warning" : "Success")
],
components: []
});
};
const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
if (!interaction.guild) return;
const member = interaction.member as GuildMember;
// Check if the user has moderate_members permission
if (!member.permissions.has("ModerateMembers")) return "You do not have the *Moderate Members* permission";
if (partial) return true;
const me = interaction.guild.members.me!;
let apply;
if (interaction.isButton()) {
apply = target!;
} else {
apply = interaction.options.getMember("user") as GuildMember;
}
const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
// Do not allow muting the owner
if (member.id === interaction.guild.ownerId) return "You cannot mute the owner of the server";
// Check if Nucleus can mute the member
if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
// Check if Nucleus has permission to mute
if (!me.permissions.has("ModerateMembers")) return "I do not have the *Moderate Members* permission";
// Do not allow muting Nucleus
if (member.id === me.id) return "I cannot mute myself";
// Allow the owner to mute anyone
if (member.id === interaction.guild.ownerId) return true;
// Check if the user is below on the role list
if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
// Allow mute
return true;
};
export { command, callback, check };
export const metadata = {
longDescription:
"Stops a member from being able to send messages or join voice channels for a specified amount of time.",
premiumOnly: true
};