mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Centralize periodic checks for mutes, tempbans, vcalerts, reminders, and scheduled posts
This should result in a significant performance improvement. The new method is also more precise than the old one, allowing the aforementioned checks to be performed with second-precision.
This commit is contained in:
parent
c84d1a0be1
commit
c7751a9da1
55 changed files with 883 additions and 366 deletions
|
@ -34,7 +34,6 @@ import { UnmuteCmd } from "./commands/UnmuteCmd";
|
|||
import { UpdateCmd } from "./commands/UpdateCmd";
|
||||
import { WarnCmd } from "./commands/WarnCmd";
|
||||
import { CreateBanCaseOnManualBanEvt } from "./events/CreateBanCaseOnManualBanEvt";
|
||||
import { CreateKickCaseOnManualKickEvt } from "./events/CreateKickCaseOnManualKickEvt";
|
||||
import { CreateUnbanCaseOnManualUnbanEvt } from "./events/CreateUnbanCaseOnManualUnbanEvt";
|
||||
import { PostAlertOnMemberJoinEvt } from "./events/PostAlertOnMemberJoinEvt";
|
||||
import { banUserId } from "./functions/banUserId";
|
||||
|
@ -42,11 +41,12 @@ import { hasMutePermission } from "./functions/hasMutePerm";
|
|||
import { kickMember } from "./functions/kickMember";
|
||||
import { offModActionsEvent } from "./functions/offModActionsEvent";
|
||||
import { onModActionsEvent } from "./functions/onModActionsEvent";
|
||||
import { outdatedTempbansLoop } from "./functions/outdatedTempbansLoop";
|
||||
import { updateCase } from "./functions/updateCase";
|
||||
import { warnMember } from "./functions/warnMember";
|
||||
import { BanOptions, ConfigSchema, KickOptions, ModActionsPluginType, WarnOptions } from "./types";
|
||||
import { LogsPlugin } from "../Logs/LogsPlugin";
|
||||
import { onGuildEvent } from "../../data/GuildEvents";
|
||||
import { clearTempban } from "./functions/clearTempban";
|
||||
|
||||
const defaultOptions = {
|
||||
config: {
|
||||
|
@ -200,7 +200,6 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()({
|
|||
state.serverLogs = new GuildLogs(guild.id);
|
||||
|
||||
state.unloaded = false;
|
||||
state.outdatedTempbansTimeout = null;
|
||||
state.ignoredEvents = [];
|
||||
// Massbans can take a while depending on rate limits,
|
||||
// so we're giving each massban 15 minutes to complete before launching the next massban
|
||||
|
@ -210,11 +209,14 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()({
|
|||
},
|
||||
|
||||
afterLoad(pluginData) {
|
||||
outdatedTempbansLoop(pluginData);
|
||||
pluginData.state.unregisterGuildEventListener = onGuildEvent(pluginData.guild.id, "expiredTempban", (tempban) =>
|
||||
clearTempban(pluginData, tempban),
|
||||
);
|
||||
},
|
||||
|
||||
beforeUnload(pluginData) {
|
||||
pluginData.state.unloaded = true;
|
||||
pluginData.state.unregisterGuildEventListener();
|
||||
pluginData.state.events.removeAllListeners();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -10,6 +10,7 @@ import { formatReasonWithAttachments } from "../functions/formatReasonWithAttach
|
|||
import { ignoreEvent } from "../functions/ignoreEvent";
|
||||
import { IgnoredEventType, modActionsCmd } from "../types";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { clearExpiringTempban } from "../../../data/loops/expiringTempbansLoop";
|
||||
|
||||
const opts = {
|
||||
mod: ct.member({ option: true }),
|
||||
|
@ -68,7 +69,11 @@ export const UnbanCmd = modActionsCmd({
|
|||
ppId: mod.id !== msg.author.id ? msg.author.id : undefined,
|
||||
});
|
||||
// Delete the tempban, if one exists
|
||||
pluginData.state.tempbans.clear(user.id);
|
||||
const tempban = await pluginData.state.tempbans.findExistingTempbanForUserId(user.id);
|
||||
if (tempban) {
|
||||
clearExpiringTempban(tempban);
|
||||
await pluginData.state.tempbans.clear(user.id);
|
||||
}
|
||||
|
||||
// Confirm the action
|
||||
sendSuccessMessage(pluginData, msg.channel, `Member unbanned (Case #${createdCase.case_number})`);
|
||||
|
|
|
@ -19,6 +19,7 @@ import { BanOptions, BanResult, IgnoredEventType, ModActionsPluginType } from ".
|
|||
import { getDefaultContactMethods } from "./getDefaultContactMethods";
|
||||
import { ignoreEvent } from "./ignoreEvent";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { registerExpiringTempban } from "../../../data/loops/expiringTempbansLoop";
|
||||
|
||||
/**
|
||||
* Ban the specified user id, whether or not they're actually on the server at the time. Generates a case.
|
||||
|
@ -112,6 +113,8 @@ export async function banUserId(
|
|||
} else {
|
||||
pluginData.state.tempbans.addTempban(user.id, banTime, banOptions.modId ?? selfId);
|
||||
}
|
||||
const tempban = (await pluginData.state.tempbans.findExistingTempbanForUserId(user.id))!;
|
||||
registerExpiringTempban(tempban);
|
||||
}
|
||||
|
||||
// Create a case for this action
|
||||
|
|
60
backend/src/plugins/ModActions/functions/clearTempban.ts
Normal file
60
backend/src/plugins/ModActions/functions/clearTempban.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { GuildPluginData } from "knub";
|
||||
import moment from "moment-timezone";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { logger } from "src/logger";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
import { resolveUser, SECONDS } from "../../../utils";
|
||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||
import { IgnoredEventType, ModActionsPluginType } from "../types";
|
||||
import { formatReasonWithAttachments } from "./formatReasonWithAttachments";
|
||||
import { ignoreEvent } from "./ignoreEvent";
|
||||
import { isBanned } from "./isBanned";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { Tempban } from "../../../data/entities/Tempban";
|
||||
|
||||
export async function clearTempban(pluginData: GuildPluginData<ModActionsPluginType>, tempban: Tempban) {
|
||||
if (!(await isBanned(pluginData, tempban.user_id))) {
|
||||
pluginData.state.tempbans.clear(tempban.user_id);
|
||||
return;
|
||||
}
|
||||
|
||||
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, tempban.user_id);
|
||||
const reason = formatReasonWithAttachments(
|
||||
`Tempban timed out.
|
||||
Tempbanned at: \`${tempban.created_at} UTC\``,
|
||||
[],
|
||||
);
|
||||
try {
|
||||
ignoreEvent(pluginData, IgnoredEventType.Unban, tempban.user_id);
|
||||
await pluginData.guild.bans.remove(tempban.user_id as Snowflake, reason ?? undefined);
|
||||
} catch (e) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Encountered an error trying to automatically unban ${tempban.user_id} after tempban timeout`,
|
||||
});
|
||||
logger.warn(`Error automatically unbanning ${tempban.user_id} (tempban timeout): ${e}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create case and delete tempban
|
||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||
const createdCase = await casesPlugin.createCase({
|
||||
userId: tempban.user_id,
|
||||
modId: tempban.mod_id,
|
||||
type: CaseTypes.Unban,
|
||||
reason,
|
||||
ppId: undefined,
|
||||
});
|
||||
pluginData.state.tempbans.clear(tempban.user_id);
|
||||
|
||||
// Log the unban
|
||||
const banTime = moment(tempban.created_at).diff(moment(tempban.expires_at));
|
||||
pluginData.getPlugin(LogsPlugin).logMemberTimedUnban({
|
||||
mod: await resolveUser(pluginData.client, tempban.mod_id),
|
||||
userId: tempban.user_id,
|
||||
caseNumber: createdCase.case_number,
|
||||
reason,
|
||||
banTime: humanizeDuration(banTime),
|
||||
});
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { GuildPluginData } from "knub";
|
||||
import moment from "moment-timezone";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { logger } from "src/logger";
|
||||
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
|
||||
import { CaseTypes } from "../../../data/CaseTypes";
|
||||
import { resolveUser, SECONDS } from "../../../utils";
|
||||
import { CasesPlugin } from "../../Cases/CasesPlugin";
|
||||
import { IgnoredEventType, ModActionsPluginType } from "../types";
|
||||
import { formatReasonWithAttachments } from "./formatReasonWithAttachments";
|
||||
import { ignoreEvent } from "./ignoreEvent";
|
||||
import { isBanned } from "./isBanned";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
|
||||
const TEMPBAN_LOOP_TIME = 60 * SECONDS;
|
||||
|
||||
export async function outdatedTempbansLoop(pluginData: GuildPluginData<ModActionsPluginType>) {
|
||||
const outdatedTempbans = await pluginData.state.tempbans.getExpiredTempbans();
|
||||
|
||||
for (const tempban of outdatedTempbans) {
|
||||
if (!(await isBanned(pluginData, tempban.user_id))) {
|
||||
pluginData.state.tempbans.clear(tempban.user_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, tempban.user_id);
|
||||
const reason = formatReasonWithAttachments(
|
||||
`Tempban timed out.
|
||||
Tempbanned at: \`${tempban.created_at} UTC\``,
|
||||
[],
|
||||
);
|
||||
try {
|
||||
ignoreEvent(pluginData, IgnoredEventType.Unban, tempban.user_id);
|
||||
await pluginData.guild.bans.remove(tempban.user_id as Snowflake, reason ?? undefined);
|
||||
} catch (e) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Encountered an error trying to automatically unban ${tempban.user_id} after tempban timeout`,
|
||||
});
|
||||
logger.warn(`Error automatically unbanning ${tempban.user_id} (tempban timeout): ${e}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create case and delete tempban
|
||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||
const createdCase = await casesPlugin.createCase({
|
||||
userId: tempban.user_id,
|
||||
modId: tempban.mod_id,
|
||||
type: CaseTypes.Unban,
|
||||
reason,
|
||||
ppId: undefined,
|
||||
});
|
||||
pluginData.state.tempbans.clear(tempban.user_id);
|
||||
|
||||
// Log the unban
|
||||
const banTime = moment(tempban.created_at).diff(moment(tempban.expires_at));
|
||||
pluginData.getPlugin(LogsPlugin).logMemberTimedUnban({
|
||||
mod: await resolveUser(pluginData.client, tempban.mod_id),
|
||||
userId: tempban.user_id,
|
||||
caseNumber: createdCase.case_number,
|
||||
reason,
|
||||
banTime: humanizeDuration(banTime),
|
||||
});
|
||||
}
|
||||
|
||||
if (!pluginData.state.unloaded) {
|
||||
pluginData.state.outdatedTempbansTimeout = setTimeout(() => outdatedTempbansLoop(pluginData), TEMPBAN_LOOP_TIME);
|
||||
}
|
||||
}
|
|
@ -72,7 +72,7 @@ export interface ModActionsPluginType extends BasePluginType {
|
|||
serverLogs: GuildLogs;
|
||||
|
||||
unloaded: boolean;
|
||||
outdatedTempbansTimeout: Timeout | null;
|
||||
unregisterGuildEventListener: () => void;
|
||||
ignoredEvents: IIgnoredEvent[];
|
||||
massbanQueue: Queue;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue