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

Add tempbans (#139)

This commit is contained in:
Nils 2021-01-28 00:20:55 +01:00 committed by GitHub
parent d8e721c9de
commit a7e01ae4e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 372 additions and 37 deletions

View file

@ -16,6 +16,7 @@ import { ignoreEvent } from "./ignoreEvent";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseTypes } from "../../../data/CaseTypes";
import { logger } from "../../../logger";
import humanizeDuration from "humanize-duration";
/**
* Ban the specified user id, whether or not they're actually on the server at the time. Generates a case.
@ -25,6 +26,7 @@ export async function banUserId(
userId: string,
reason?: string,
banOptions: BanOptions = {},
banTime?: number,
): Promise<BanResult> {
const config = pluginData.config.get();
const user = await resolveUser(pluginData.client, userId);
@ -43,7 +45,7 @@ export async function banUserId(
: getDefaultContactMethods(pluginData, "ban");
if (contactMethods.length) {
if (config.ban_message) {
if (!banTime && config.ban_message) {
const banMessage = await renderTemplate(config.ban_message, {
guildName: pluginData.guild.name,
reason,
@ -52,9 +54,20 @@ export async function banUserId(
: {},
});
notifyResult = await notifyUser(user, banMessage, contactMethods);
} else if (banTime && config.tempban_message) {
const banMessage = await renderTemplate(config.tempban_message, {
guildName: pluginData.guild.name,
reason,
moderator: banOptions.caseArgs?.modId
? stripObjectToScalars(await resolveUser(pluginData.client, banOptions.caseArgs.modId))
: {},
banTime: humanizeDuration(banTime),
});
notifyResult = await notifyUser(user, banMessage, contactMethods);
} else {
notifyResult = createUserNotificationError("No ban message specified in config");
notifyResult = createUserNotificationError("No ban/tempban message specified in config");
}
}
}
@ -87,22 +100,31 @@ export async function banUserId(
// Create a case for this action
const modId = banOptions.caseArgs?.modId || pluginData.client.user.id;
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const noteDetails: string[] = [];
const timeUntilUnban = banTime ? humanizeDuration(banTime) : "indefinite";
const timeDetails = `Banned ${banTime ? `for ${timeUntilUnban}` : "indefinitely"}`;
if (notifyResult.text) noteDetails.push(ucfirst(notifyResult.text));
noteDetails.push(timeDetails);
const createdCase = await casesPlugin.createCase({
...(banOptions.caseArgs || {}),
userId,
modId,
type: CaseTypes.Ban,
reason,
noteDetails: notifyResult.text ? [ucfirst(notifyResult.text)] : [],
noteDetails,
});
// Log the action
const mod = await resolveUser(pluginData.client, modId);
pluginData.state.serverLogs.log(LogType.MEMBER_BAN, {
const logtype = banTime ? LogType.MEMBER_TIMED_BAN : LogType.MEMBER_BAN;
pluginData.state.serverLogs.log(logtype, {
mod: stripObjectToScalars(mod),
user: stripObjectToScalars(user),
caseNumber: createdCase.case_number,
reason,
banTime: banTime ? humanizeDuration(banTime) : null,
});
return {

View file

@ -0,0 +1,67 @@
import { resolveUser, SECONDS, stripObjectToScalars } from "../../../utils";
import { GuildPluginData } from "knub";
import { IgnoredEventType, ModActionsPluginType } from "../types";
import { LogType } from "src/data/LogType";
import { formatReasonWithAttachments } from "./formatReasonWithAttachments";
import { ignoreEvent } from "./ignoreEvent";
import { isBanned } from "./isBanned";
import { logger } from "src/logger";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseTypes } from "../../../data/CaseTypes";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
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.unbanMember(tempban.user_id, reason != null ? encodeURIComponent(reason) : undefined);
} catch (e) {
pluginData.state.serverLogs.log(LogType.BOT_ALERT, {
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.state.serverLogs.log(LogType.MEMBER_TIMED_UNBAN, {
mod: stripObjectToScalars(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);
}
}