3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-14 22:05:01 +00:00

Fixes, refactoring and PR feedback

This commit is contained in:
Lily Bergonzat 2024-04-15 15:51:45 +02:00
parent 0be54912c4
commit 893a77d562
202 changed files with 1037 additions and 1069 deletions

View file

@ -1,8 +1,7 @@
import { waitForReply } from "knub/helpers";
import { commandTypeHelpers as ct } from "../../../../commandTypes";
import { getContextChannel, sendContextResponse } from "../../../../pluginUtils";
import { CommonPlugin } from "../../../Common/CommonPlugin";
import { actualMassMuteCmd } from "../../functions/actualCommands/actualMassMuteCmd";
import { actualMassMuteCmd } from "./actualMassMuteCmd";
import { modActionsMsgCmd } from "../../types";
export const MassMuteMsgCmd = modActionsMsgCmd({
@ -25,7 +24,7 @@ export const MassMuteMsgCmd = modActionsMsgCmd({
!muteReasonReceived.content ||
muteReasonReceived.content.toLowerCase().trim() === "cancel"
) {
pluginData.getPlugin(CommonPlugin).sendErrorMessage(msg, "Cancelled");
pluginData.state.common.sendErrorMessage(msg, "Cancelled");
return;
}

View file

@ -1,8 +1,7 @@
import { GuildMember } from "discord.js";
import { slashOptions } from "knub";
import { generateAttachmentSlashOptions, retrieveMultipleOptions } from "../../../../utils/multipleSlashOptions";
import { CommonPlugin } from "../../../Common/CommonPlugin";
import { actualMassMuteCmd } from "../../functions/actualCommands/actualMassMuteCmd";
import { actualMassMuteCmd } from "./actualMassMuteCmd";
import { modActionsSlashCmd } from "../../types";
import { NUMBER_ATTACHMENTS_CASE_CREATION } from "../constants";
@ -31,9 +30,13 @@ export const MassMuteSlashSlashCmd = modActionsSlashCmd({
const attachments = retrieveMultipleOptions(NUMBER_ATTACHMENTS_CASE_CREATION, options, "attachment");
if ((!options.reason || options.reason.trim() === "") && attachments.length < 1) {
pluginData
.getPlugin(CommonPlugin)
.sendErrorMessage(interaction, "Text or attachment required", undefined, undefined, true);
pluginData.state.common.sendErrorMessage(
interaction,
"Text or attachment required",
undefined,
undefined,
true
);
return;
}

View file

@ -0,0 +1,97 @@
import { Attachment, ChatInputCommandInteraction, GuildMember, Message, Snowflake } from "discord.js";
import { GuildPluginData } from "knub";
import { LogType } from "../../../../data/LogType";
import { logger } from "../../../../logger";
import { canActOn, isContextInteraction, sendContextResponse } from "../../../../pluginUtils";
import { LogsPlugin } from "../../../Logs/LogsPlugin";
import { MutesPlugin } from "../../../Mutes/MutesPlugin";
import { ModActionsPluginType } from "../../types";
import { handleAttachmentLinkDetectionAndGetRestriction } from "../../functions/attachmentLinkReaction";
import { formatReasonWithAttachments, formatReasonWithMessageLinkForAttachments } from "../../functions/formatReasonForAttachments";
export async function actualMassMuteCmd(
pluginData: GuildPluginData<ModActionsPluginType>,
context: Message | ChatInputCommandInteraction,
userIds: string[],
author: GuildMember,
reason: string,
attachments: Attachment[],
) {
// Limit to 100 users at once (arbitrary?)
if (userIds.length > 100) {
pluginData.state.common.sendErrorMessage(context, `Can only massmute max 100 users at once`);
return;
}
if (await handleAttachmentLinkDetectionAndGetRestriction(pluginData, context, reason)) {
return;
}
const muteReason = await formatReasonWithMessageLinkForAttachments(pluginData, reason, context, attachments);
const muteReasonWithAttachments = formatReasonWithAttachments(reason, attachments);
// Verify we can act upon all users
for (const userId of userIds) {
const member = pluginData.guild.members.cache.get(userId as Snowflake);
if (member && !canActOn(pluginData, author, member)) {
pluginData.state.common.sendErrorMessage(
context,
"Cannot massmute one or more users: insufficient permissions"
);
return;
}
}
// Ignore automatic mute cases and logs for these users
// We'll create our own cases below and post a single "mass muted" log instead
userIds.forEach((userId) => {
// Use longer timeouts since this can take a while
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_MUTE, userId, 120 * 1000);
});
// Show loading indicator
const loadingMsg = await sendContextResponse(context, { content: "Muting...", ephemeral: true });
// Mute everyone and count fails
const modId = author.id;
const failedMutes: string[] = [];
const mutesPlugin = pluginData.getPlugin(MutesPlugin);
for (const userId of userIds) {
try {
await mutesPlugin.muteUser(userId, 0, `Mass mute: ${muteReason}`, `Mass mute: ${muteReasonWithAttachments}`, {
caseArgs: {
modId,
},
});
} catch (e) {
logger.info(e);
failedMutes.push(userId);
}
}
if (!isContextInteraction(context)) {
// Clear loading indicator
loadingMsg.delete();
}
const successfulMuteCount = userIds.length - failedMutes.length;
if (successfulMuteCount === 0) {
// All mutes failed
pluginData.state.common.sendErrorMessage(context, "All mutes failed. Make sure the IDs are valid.");
} else {
// Success on all or some mutes
pluginData.getPlugin(LogsPlugin).logMassMute({
mod: author.user,
count: successfulMuteCount,
});
if (failedMutes.length) {
pluginData.state.common.sendSuccessMessage(
context,
`Muted ${successfulMuteCount} users, ${failedMutes.length} failed: ${failedMutes.join(" ")}`,
);
} else {
pluginData.state.common.sendSuccessMessage(context, `Muted ${successfulMuteCount} users successfully`);
}
}
}