3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-07 11:07:19 +00:00

feat: first batch of emojis 🎉

This commit is contained in:
Lily Bergonzat 2024-02-14 09:17:11 +01:00
parent dfb0e2c19d
commit a4c4b17a14
91 changed files with 3659 additions and 2032 deletions

View file

@ -0,0 +1,163 @@
import { ChatInputCommandInteraction, GuildMember, Snowflake, TextBasedChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { waitForReply } from "knub/helpers";
import { CaseTypes } from "../../../../data/CaseTypes";
import { LogType } from "../../../../data/LogType";
import { humanizeDurationShort } from "../../../../humanizeDurationShort";
import {
canActOn,
isContextInteraction,
sendContextResponse,
sendErrorMessage,
sendSuccessMessage,
} from "../../../../pluginUtils";
import { DAYS, MINUTES, SECONDS, noop } from "../../../../utils";
import { CasesPlugin } from "../../../Cases/CasesPlugin";
import { LogsPlugin } from "../../../Logs/LogsPlugin";
import { IgnoredEventType, ModActionsPluginType } from "../../types";
import { formatReasonWithAttachments } from "../formatReasonWithAttachments";
import { ignoreEvent } from "../ignoreEvent";
export async function actualMassBanCmd(
pluginData: GuildPluginData<ModActionsPluginType>,
context: TextBasedChannel | ChatInputCommandInteraction,
userIds: string[],
author: GuildMember,
) {
// Limit to 100 users at once (arbitrary?)
if (userIds.length > 100) {
sendErrorMessage(pluginData, context, `Can only massban max 100 users at once`);
return;
}
// Ask for ban reason (cleaner this way instead of trying to cram it into the args)
sendContextResponse(context, "Ban reason? `cancel` to cancel");
const banReasonReply = await waitForReply(
pluginData.client,
isContextInteraction(context) ? context.channel! : context,
author.id,
);
if (!banReasonReply || !banReasonReply.content || banReasonReply.content.toLowerCase().trim() === "cancel") {
sendErrorMessage(pluginData, context, "Cancelled");
return;
}
const banReason = formatReasonWithAttachments(banReasonReply.content, [...banReasonReply.attachments.values()]);
// Verify we can act on each of the users specified
for (const userId of userIds) {
const member = pluginData.guild.members.cache.get(userId as Snowflake); // TODO: Get members on demand?
if (member && !canActOn(pluginData, author, member)) {
sendErrorMessage(pluginData, context, "Cannot massban one or more users: insufficient permissions");
return;
}
}
// Show a loading indicator since this can take a while
const maxWaitTime = pluginData.state.massbanQueue.timeout * pluginData.state.massbanQueue.length;
const maxWaitTimeFormatted = humanizeDurationShort(maxWaitTime, { round: true });
const initialLoadingText =
pluginData.state.massbanQueue.length === 0
? "Banning..."
: `Massban queued. Waiting for previous massban to finish (max wait ${maxWaitTimeFormatted}).`;
const loadingMsg = await sendContextResponse(context, initialLoadingText);
const waitTimeStart = performance.now();
const waitingInterval = setInterval(() => {
const waitTime = humanizeDurationShort(performance.now() - waitTimeStart, { round: true });
loadingMsg
.edit(`Massban queued. Still waiting for previous massban to finish (waited ${waitTime}).`)
.catch(() => clearInterval(waitingInterval));
}, 1 * MINUTES);
pluginData.state.massbanQueue.add(async () => {
clearInterval(waitingInterval);
if (pluginData.state.unloaded) {
void loadingMsg.delete().catch(noop);
return;
}
void loadingMsg.edit("Banning...").catch(noop);
// Ban each user and count failed bans (if any)
const startTime = performance.now();
const failedBans: string[] = [];
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const messageConfig = isContextInteraction(context)
? await pluginData.config.getForInteraction(context)
: await pluginData.config.getForChannel(context);
const deleteDays = messageConfig.ban_delete_message_days;
for (const [i, userId] of userIds.entries()) {
if (pluginData.state.unloaded) {
break;
}
try {
// Ignore automatic ban cases and logs
// We create our own cases below and post a single "mass banned" log instead
ignoreEvent(pluginData, IgnoredEventType.Ban, userId, 30 * MINUTES);
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_BAN, userId, 30 * MINUTES);
await pluginData.guild.bans.create(userId as Snowflake, {
deleteMessageSeconds: (deleteDays * DAYS) / SECONDS,
reason: banReason,
});
await casesPlugin.createCase({
userId,
modId: author.id,
type: CaseTypes.Ban,
reason: `Mass ban: ${banReason}`,
postInCaseLogOverride: false,
});
pluginData.state.events.emit("ban", userId, banReason);
} catch {
failedBans.push(userId);
}
// Send a status update every 10 bans
if ((i + 1) % 10 === 0) {
loadingMsg.edit(`Banning... ${i + 1}/${userIds.length}`).catch(noop);
}
}
const totalTime = performance.now() - startTime;
const formattedTimeTaken = humanizeDurationShort(totalTime, { round: true });
// Clear loading indicator
loadingMsg.delete().catch(noop);
const successfulBanCount = userIds.length - failedBans.length;
if (successfulBanCount === 0) {
// All bans failed - don't create a log entry and notify the user
sendErrorMessage(pluginData, context, "All bans failed. Make sure the IDs are valid.");
} else {
// Some or all bans were successful. Create a log entry for the mass ban and notify the user.
pluginData.getPlugin(LogsPlugin).logMassBan({
mod: author.user,
count: successfulBanCount,
reason: banReason,
});
if (failedBans.length) {
sendSuccessMessage(
pluginData,
context,
`Banned ${successfulBanCount} users in ${formattedTimeTaken}, ${failedBans.length} failed: ${failedBans.join(
" ",
)}`,
);
} else {
sendSuccessMessage(
pluginData,
context,
`Banned ${successfulBanCount} users successfully in ${formattedTimeTaken}`,
);
}
}
});
}