3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +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

@ -19,6 +19,8 @@
"MEMBER_NICK_CHANGE": "✏ {userMention(member)}: nickname changed from **{oldNick}** to **{newNick}**",
"MEMBER_USERNAME_CHANGE": "✏ {userMention(user)}: username changed from **{oldName}** to **{newName}**",
"MEMBER_RESTORE": "💿 Restored {restoredData} for {userMention(member)} on rejoin",
"MEMBER_TIMED_BAN": "🔨 {userMention(user)} was tempbanned by {userMention(mod)} for {banTime}",
"MEMBER_TIMED_UNBAN": "🔓 User (`{userId}`) was automatically unbanned by {userMention(mod)} after a tempban for {banTime}",
"CHANNEL_CREATE": "🖊 Channel {channelMention(channel)} was created",
"CHANNEL_DELETE": "🗑 Channel {channelMention(channel)} was deleted",

View file

@ -0,0 +1,75 @@
import moment from "moment-timezone";
import { Mute } from "./entities/Mute";
import { BaseGuildRepository } from "./BaseGuildRepository";
import { Brackets, getRepository, Repository } from "typeorm";
import { Tempban } from "./entities/Tempban";
export class GuildTempbans extends BaseGuildRepository {
private tempbans: Repository<Tempban>;
constructor(guildId) {
super(guildId);
this.tempbans = getRepository(Tempban);
}
async getExpiredTempbans(): Promise<Tempban[]> {
return this.tempbans
.createQueryBuilder("mutes")
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("expires_at IS NOT NULL")
.andWhere("expires_at <= NOW()")
.getMany();
}
async findExistingTempbanForUserId(userId: string): Promise<Tempban | undefined> {
return this.tempbans.findOne({
where: {
guild_id: this.guildId,
user_id: userId,
},
});
}
async addTempban(userId, expiryTime, modId): Promise<Tempban> {
const expiresAt = moment
.utc()
.add(expiryTime, "ms")
.format("YYYY-MM-DD HH:mm:ss");
const result = await this.tempbans.insert({
guild_id: this.guildId,
user_id: userId,
mod_id: modId,
expires_at: expiresAt,
created_at: moment.utc().format("YYYY-MM-DD HH:mm:ss"),
});
return (await this.tempbans.findOne({ where: result.identifiers[0] }))!;
}
async updateExpiryTime(userId, newExpiryTime, modId) {
const expiresAt = moment
.utc()
.add(newExpiryTime, "ms")
.format("YYYY-MM-DD HH:mm:ss");
return this.tempbans.update(
{
guild_id: this.guildId,
user_id: userId,
},
{
created_at: moment.utc().format("YYYY-MM-DD HH:mm:ss"),
expires_at: expiresAt,
mod_id: modId,
},
);
}
async clear(userId) {
await this.tempbans.delete({
guild_id: this.guildId,
user_id: userId,
});
}
}

View file

@ -44,6 +44,8 @@ export enum LogType {
MEMBER_TIMED_MUTE,
MEMBER_TIMED_UNMUTE,
MEMBER_TIMED_BAN,
MEMBER_TIMED_UNBAN,
MEMBER_JOIN_WITH_PRIOR_RECORDS,
OTHER_SPAM_DETECTED,

View file

@ -0,0 +1,18 @@
import { Column, Entity, PrimaryColumn } from "typeorm";
@Entity("tempbans")
export class Tempban {
@Column()
@PrimaryColumn()
guild_id: string;
@Column()
@PrimaryColumn()
user_id: string;
@Column() mod_id: string;
@Column() created_at: string;
@Column() expires_at: string;
}

View file

@ -0,0 +1,45 @@
import { MigrationInterface, QueryRunner, Table, TableIndex } from "typeorm";
export class CreateTempBansTable1608753440716 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
const table = await queryRunner.createTable(
new Table({
name: "tempbans",
columns: [
{
name: "guild_id",
type: "bigint",
isPrimary: true,
},
{
name: "user_id",
type: "bigint",
isPrimary: true,
},
{
name: "mod_id",
type: "bigint",
},
{
name: "created_at",
type: "datetime",
},
{
name: "expires_at",
type: "datetime",
},
],
}),
);
queryRunner.createIndex(
"tempbans",
new TableIndex({
columnNames: ["expires_at"],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropTable("tempbans");
}
}

View file

@ -36,6 +36,8 @@ import { MassmuteCmd } from "./commands/MassmuteCmd";
import { trimPluginDescription } from "../../utils";
import { DeleteCaseCmd } from "./commands/DeleteCaseCmd";
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
import { GuildTempbans } from "../../data/GuildTempbans";
import { outdatedTempbansLoop } from "./functions/outdatedTempbansLoop";
const defaultOptions = {
config: {
@ -49,6 +51,7 @@ const defaultOptions = {
warn_message: "You have received a warning on the {guildName} server: {reason}",
kick_message: "You have been kicked from the {guildName} server. Reason given: {reason}",
ban_message: "You have been banned from the {guildName} server. Reason given: {reason}",
tempban_message: "You have been banned from the {guildName} server for {banTime}. Reason given: {reason}",
alert_on_rejoin: false,
alert_channel: null,
warn_notify_enabled: false,
@ -165,8 +168,17 @@ export const ModActionsPlugin = zeppelinGuildPlugin<ModActionsPluginType>()("mod
state.mutes = GuildMutes.getGuildInstance(guild.id);
state.cases = GuildCases.getGuildInstance(guild.id);
state.tempbans = GuildTempbans.getGuildInstance(guild.id);
state.serverLogs = new GuildLogs(guild.id);
state.unloaded = false;
state.outdatedTempbansTimeout = null;
state.ignoredEvents = [];
outdatedTempbansLoop(pluginData);
},
onUnload(pluginData) {
pluginData.state.unloaded = true;
},
});

View file

@ -1,14 +1,16 @@
import { modActionsCmd, IgnoredEventType } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { canActOn, sendErrorMessage, hasPermission, sendSuccessMessage } from "../../../pluginUtils";
import { resolveUser, resolveMember } from "../../../utils";
import { resolveUser, resolveMember, stripObjectToScalars, noop } from "../../../utils";
import { isBanned } from "../functions/isBanned";
import { readContactMethodsFromArgs } from "../functions/readContactMethodsFromArgs";
import { formatReasonWithAttachments } from "../functions/formatReasonWithAttachments";
import { banUserId } from "../functions/banUserId";
import { ignoreEvent } from "../functions/ignoreEvent";
import { LogType } from "../../../data/LogType";
import { getMemberLevel, waitForReaction } from "knub/dist/helpers";
import humanizeDuration from "humanize-duration";
import { CasesPlugin } from "src/plugins/Cases/CasesPlugin";
import { CaseTypes } from "src/data/CaseTypes";
import { LogType } from "src/data/LogType";
const opts = {
mod: ct.member({ option: true }),
@ -20,9 +22,16 @@ const opts = {
export const BanCmd = modActionsCmd({
trigger: "ban",
permission: "can_ban",
description: "Ban the specified member",
description: "Ban or Tempban the specified member",
signature: [
{
user: ct.string(),
time: ct.delay(),
reason: ct.string({ required: false, catchAll: true }),
...opts,
},
{
user: ct.string(),
reason: ct.string({ required: false, catchAll: true }),
@ -37,24 +46,90 @@ export const BanCmd = modActionsCmd({
sendErrorMessage(pluginData, msg.channel, `User not found`);
return;
}
const time = args["time"] ? args["time"] : null;
const reason = formatReasonWithAttachments(args.reason, msg.attachments);
const memberToBan = await resolveMember(pluginData.client, pluginData.guild, user.id);
let forceban = false;
if (!memberToBan) {
const banned = await isBanned(pluginData, user.id);
if (banned) {
sendErrorMessage(pluginData, msg.channel, `User is already banned`);
// The moderator who did the action is the message author or, if used, the specified -mod
let mod = msg.member;
if (args.mod) {
if (!hasPermission(pluginData, "can_act_as_other", { message: msg, channelId: msg.channel.id })) {
sendErrorMessage(pluginData, msg.channel, "No permission for -mod");
return;
}
mod = args.mod;
}
// acquire a lock because of the needed user-inputs below (if banned/not on server)
const lock = await pluginData.locks.acquire(`ban-${user.id}`);
let forceban = false;
const existingTempban = await pluginData.state.tempbans.findExistingTempbanForUserId(user.id);
const banned = await isBanned(pluginData, user.id);
if (!memberToBan) {
if (banned) {
// Abort if trying to ban user indefinitely if they are already banned indefinitely
if (!existingTempban && !time) {
sendErrorMessage(pluginData, msg.channel, `User is already banned indefinitely.`);
return;
}
// Ask the mod if we should update the existing ban
const alreadyBannedMsg = await msg.channel.createMessage("User is already banned, update ban?");
const reply = await waitForReaction(pluginData.client, alreadyBannedMsg, ["✅", "❌"], msg.author.id);
alreadyBannedMsg.delete().catch(noop);
if (!reply || reply.name === "❌") {
sendErrorMessage(pluginData, msg.channel, "User already banned, update cancelled by moderator");
lock.unlock();
return;
} else {
// Update or add new tempban / remove old tempban
if (time && time > 0) {
if (existingTempban) {
pluginData.state.tempbans.updateExpiryTime(user.id, time, mod.id);
} else {
pluginData.state.tempbans.addTempban(user.id, time, mod.id);
}
} else if (existingTempban) {
pluginData.state.tempbans.clear(user.id);
}
// Create a new case for the updated ban since we never stored the old case id and log the action
const casesPlugin = pluginData.getPlugin(CasesPlugin);
const createdCase = await casesPlugin.createCase({
modId: mod.id,
type: CaseTypes.Ban,
userId: user.id,
reason,
noteDetails: [`Ban updated to ${time ? humanizeDuration(time) : "indefinite"}`],
});
const logtype = time ? LogType.MEMBER_TIMED_BAN : LogType.MEMBER_BAN;
pluginData.state.serverLogs.log(logtype, {
mod: stripObjectToScalars(mod.user),
user: stripObjectToScalars(user),
caseNumber: createdCase.case_number,
reason,
banTime: time ? humanizeDuration(time) : null,
});
sendSuccessMessage(
pluginData,
msg.channel,
`Ban updated to ${time ? "expire in " + humanizeDuration(time) + " from now" : "indefinite"}`,
);
lock.unlock();
return;
}
} else {
// Ask the mod if we should upgrade to a forceban as the user is not on the server
const notOnServerMsg = await msg.channel.createMessage("User not found on the server, forceban instead?");
const reply = await waitForReaction(pluginData.client, notOnServerMsg, ["✅", "❌"], msg.author.id);
notOnServerMsg.delete();
notOnServerMsg.delete().catch(noop);
if (!reply || reply.name === "❌") {
sendErrorMessage(pluginData, msg.channel, "User not on server, ban cancelled by moderator");
lock.unlock();
return;
} else {
forceban = true;
@ -71,53 +146,62 @@ export const BanCmd = modActionsCmd({
msg.channel,
`Cannot ban: target permission level is equal or higher to yours, ${targetLevel} >= ${ourLevel}`,
);
lock.unlock();
return;
}
// The moderator who did the action is the message author or, if used, the specified -mod
let mod = msg.member;
if (args.mod) {
if (!hasPermission(pluginData, "can_act_as_other", { message: msg, channelId: msg.channel.id })) {
sendErrorMessage(pluginData, msg.channel, "No permission for -mod");
return;
}
mod = args.mod;
}
let contactMethods;
try {
contactMethods = readContactMethodsFromArgs(args);
} catch (e) {
sendErrorMessage(pluginData, msg.channel, e.message);
lock.unlock();
return;
}
const deleteMessageDays = args["delete-days"] ?? pluginData.config.getForMessage(msg).ban_delete_message_days;
const reason = formatReasonWithAttachments(args.reason, msg.attachments);
const banResult = await banUserId(pluginData, user.id, reason, {
contactMethods,
caseArgs: {
modId: mod.id,
ppId: mod.id !== msg.author.id ? msg.author.id : undefined,
const banResult = await banUserId(
pluginData,
user.id,
reason,
{
contactMethods,
caseArgs: {
modId: mod.id,
ppId: mod.id !== msg.author.id ? msg.author.id : undefined,
},
deleteMessageDays,
},
deleteMessageDays,
});
time,
);
if (banResult.status === "failed") {
sendErrorMessage(pluginData, msg.channel, `Failed to ban member: ${banResult.error}`);
lock.unlock();
return;
}
let forTime = "";
if (time && time > 0) {
if (existingTempban) {
pluginData.state.tempbans.updateExpiryTime(user.id, time, mod.id);
} else {
pluginData.state.tempbans.addTempban(user.id, time, mod.id);
}
forTime = `for ${humanizeDuration(time)} `;
}
// Confirm the action to the moderator
let response = "";
if (!forceban) {
response = `Banned **${user.username}#${user.discriminator}** (Case #${banResult.case.case_number})`;
response = `Banned **${user.username}#${user.discriminator}** ${forTime}(Case #${banResult.case.case_number})`;
if (banResult.notifyResult.text) response += ` (${banResult.notifyResult.text})`;
} else {
response = `Member forcebanned (Case #${banResult.case.case_number})`;
response = `Member forcebanned ${forTime}(Case #${banResult.case.case_number})`;
}
lock.unlock();
sendSuccessMessage(pluginData, msg.channel, response);
},
});

View file

@ -64,6 +64,8 @@ export const UnbanCmd = modActionsCmd({
reason,
ppId: mod.id !== msg.author.id ? msg.author.id : undefined,
});
// Delete the tempban, if one exists
pluginData.state.tempbans.clear(user.id);
// Confirm the action
sendSuccessMessage(pluginData, msg.channel, `Member unbanned (Case #${createdCase.case_number})`);

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);
}
}

View file

@ -7,6 +7,8 @@ import { GuildLogs } from "../../data/GuildLogs";
import { Case } from "../../data/entities/Case";
import { CaseArgs } from "../Cases/types";
import { TextChannel } from "eris";
import { GuildTempbans } from "../../data/GuildTempbans";
import Timeout = NodeJS.Timeout;
export const ConfigSchema = t.type({
dm_on_warn: t.boolean,
@ -19,6 +21,7 @@ export const ConfigSchema = t.type({
warn_message: tNullable(t.string),
kick_message: tNullable(t.string),
ban_message: tNullable(t.string),
tempban_message: tNullable(t.string),
alert_on_rejoin: t.boolean,
alert_channel: tNullable(t.string),
warn_notify_enabled: t.boolean,
@ -46,8 +49,11 @@ export interface ModActionsPluginType extends BasePluginType {
state: {
mutes: GuildMutes;
cases: GuildCases;
tempbans: GuildTempbans;
serverLogs: GuildLogs;
unloaded: boolean;
outdatedTempbansTimeout: Timeout | null;
ignoredEvents: IIgnoredEvent[];
};
}