3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-11 04:45:02 +00:00

Create archives from bulk deletes and cleans. Use GuildSavedMessages for cleans.

This commit is contained in:
Dragory 2018-11-24 18:39:17 +02:00
parent f7b62429c6
commit 2bce771c59
9 changed files with 176 additions and 145 deletions

View file

@ -11,9 +11,6 @@ function notFound(res: ServerResponse) {
res.end("Not Found");
}
/**
* A global plugin that allows bot owners to control the bot
*/
export class LogServerPlugin extends GlobalPlugin {
protected archives: GuildArchives;
protected server: http.Server;

View file

@ -16,6 +16,7 @@ import isEqual from "lodash.isequal";
import diff from "lodash.difference";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildArchives } from "../data/GuildArchives";
interface ILogChannel {
include?: string[];
@ -33,8 +34,9 @@ const unknownUser = {
};
export class LogsPlugin extends Plugin {
protected serverLogs: GuildLogs;
protected guildLogs: GuildLogs;
protected savedMessages: GuildSavedMessages;
protected archives: GuildArchives;
protected logListener;
@ -55,11 +57,12 @@ export class LogsPlugin extends Plugin {
}
onLoad() {
this.serverLogs = new GuildLogs(this.guildId);
this.guildLogs = new GuildLogs(this.guildId);
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
this.archives = GuildArchives.getInstance(this.guildId);
this.logListener = ({ type, data }) => this.log(type, data);
this.serverLogs.on("log", this.logListener);
this.guildLogs.on("log", this.logListener);
this.savedMessages.events.on("delete", this.onMessageDelete.bind(this));
this.savedMessages.events.on("deleteBulk", this.onMessageDeleteBulk.bind(this));
@ -67,7 +70,7 @@ export class LogsPlugin extends Plugin {
}
onUnload() {
this.serverLogs.removeListener("log", this.logListener);
this.guildLogs.removeListener("log", this.logListener);
this.savedMessages.events.off("delete", this.onMessageDelete.bind(this));
this.savedMessages.events.off("deleteBulk", this.onMessageDeleteBulk.bind(this));
@ -113,7 +116,7 @@ export class LogsPlugin extends Plugin {
round: true
});
this.serverLogs.log(LogType.MEMBER_JOIN, {
this.guildLogs.log(LogType.MEMBER_JOIN, {
member: stripObjectToScalars(member, ["user"]),
new: member.createdAt >= newThreshold ? " :new:" : "",
account_age: accountAge
@ -122,7 +125,7 @@ export class LogsPlugin extends Plugin {
@d.event("guildMemberRemove")
onMemberLeave(_, member) {
this.serverLogs.log(LogType.MEMBER_LEAVE, {
this.guildLogs.log(LogType.MEMBER_LEAVE, {
member: stripObjectToScalars(member, ["user"])
});
}
@ -136,7 +139,7 @@ export class LogsPlugin extends Plugin {
);
const mod = relevantAuditLogEntry ? relevantAuditLogEntry.user : unknownUser;
this.serverLogs.log(
this.guildLogs.log(
LogType.MEMBER_BAN,
{
user: stripObjectToScalars(user),
@ -155,7 +158,7 @@ export class LogsPlugin extends Plugin {
);
const mod = relevantAuditLogEntry ? relevantAuditLogEntry.user : unknownUser;
this.serverLogs.log(
this.guildLogs.log(
LogType.MEMBER_UNBAN,
{
mod: stripObjectToScalars(mod),
@ -170,7 +173,7 @@ export class LogsPlugin extends Plugin {
if (!oldMember) return;
if (member.nick !== oldMember.nick) {
this.serverLogs.log(LogType.MEMBER_NICK_CHANGE, {
this.guildLogs.log(LogType.MEMBER_NICK_CHANGE, {
member,
oldNick: oldMember.nick || "<none>",
newNick: member.nick
@ -189,7 +192,7 @@ export class LogsPlugin extends Plugin {
const mod = relevantAuditLogEntry ? relevantAuditLogEntry.user : unknownUser;
if (addedRoles.length) {
this.serverLogs.log(
this.guildLogs.log(
LogType.MEMBER_ROLE_ADD,
{
member,
@ -199,7 +202,7 @@ export class LogsPlugin extends Plugin {
member.id
);
} else if (removedRoles.length) {
this.serverLogs.log(
this.guildLogs.log(
LogType.MEMBER_ROLE_REMOVE,
{
member,
@ -218,7 +221,7 @@ export class LogsPlugin extends Plugin {
if (user.username !== oldUser.username || user.discriminator !== oldUser.discriminator) {
const member = this.guild.members.get(user.id) || { id: user.id, user };
this.serverLogs.log(LogType.MEMBER_USERNAME_CHANGE, {
this.guildLogs.log(LogType.MEMBER_USERNAME_CHANGE, {
member: stripObjectToScalars(member, ["user"]),
oldName: `${oldUser.username}#${oldUser.discriminator}`,
newName: `${user.username}#${user.discriminator}`
@ -228,28 +231,28 @@ export class LogsPlugin extends Plugin {
@d.event("channelCreate")
onChannelCreate(channel) {
this.serverLogs.log(LogType.CHANNEL_CREATE, {
this.guildLogs.log(LogType.CHANNEL_CREATE, {
channel: stripObjectToScalars(channel)
});
}
@d.event("channelDelete")
onChannelDelete(channel) {
this.serverLogs.log(LogType.CHANNEL_DELETE, {
this.guildLogs.log(LogType.CHANNEL_DELETE, {
channel: stripObjectToScalars(channel)
});
}
@d.event("guildRoleCreate")
onRoleCreate(_, role) {
this.serverLogs.log(LogType.ROLE_CREATE, {
this.guildLogs.log(LogType.ROLE_CREATE, {
role: stripObjectToScalars(role)
});
}
@d.event("guildRoleDelete")
onRoleDelete(_, role) {
this.serverLogs.log(LogType.ROLE_DELETE, {
this.guildLogs.log(LogType.ROLE_DELETE, {
role: stripObjectToScalars(role)
});
}
@ -266,7 +269,7 @@ export class LogsPlugin extends Plugin {
: "Unknown pre-edit content";
const after = disableCodeBlocks(deactivateMentions(savedMessage.data.content || ""));
this.serverLogs.log(LogType.MESSAGE_EDIT, {
this.guildLogs.log(LogType.MESSAGE_EDIT, {
member: stripObjectToScalars(member, ["user"]),
channel: stripObjectToScalars(channel),
before,
@ -280,7 +283,7 @@ export class LogsPlugin extends Plugin {
const channel = this.guild.channels.get(savedMessage.channel_id);
if (member) {
this.serverLogs.log(
this.guildLogs.log(
LogType.MESSAGE_DELETE,
{
member: stripObjectToScalars(member, ["user"]),
@ -290,7 +293,7 @@ export class LogsPlugin extends Plugin {
savedMessage.id
);
} else {
this.serverLogs.log(
this.guildLogs.log(
LogType.MESSAGE_DELETE_BARE,
{
messageId: savedMessage.id,
@ -302,14 +305,18 @@ export class LogsPlugin extends Plugin {
}
// Uses events from savesMessages
onMessageDeleteBulk(savedMessages: SavedMessage[]) {
async onMessageDeleteBulk(savedMessages: SavedMessage[]) {
const channel = this.guild.channels.get(savedMessages[0].channel_id);
const user = this.bot.users.get(savedMessages[0].user_id);
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, channel, user);
const baseUrl = this.knub.getGlobalConfig().url;
this.serverLogs.log(
this.guildLogs.log(
LogType.MESSAGE_DELETE_BULK,
{
count: savedMessages.length,
channel
channel,
archiveUrl: `${baseUrl}/archives/${archiveId}`
},
savedMessages[0].id
);
@ -317,7 +324,7 @@ export class LogsPlugin extends Plugin {
@d.event("voiceChannelJoin")
onVoiceChannelJoin(member: Member, channel: Channel) {
this.serverLogs.log(LogType.VOICE_CHANNEL_JOIN, {
this.guildLogs.log(LogType.VOICE_CHANNEL_JOIN, {
member: stripObjectToScalars(member, ["user"]),
channel: stripObjectToScalars(channel)
});
@ -325,7 +332,7 @@ export class LogsPlugin extends Plugin {
@d.event("voiceChannelLeave")
onVoiceChannelLeave(member: Member, channel: Channel) {
this.serverLogs.log(LogType.VOICE_CHANNEL_LEAVE, {
this.guildLogs.log(LogType.VOICE_CHANNEL_LEAVE, {
member: stripObjectToScalars(member, ["user"]),
channel: stripObjectToScalars(channel)
});
@ -333,7 +340,7 @@ export class LogsPlugin extends Plugin {
@d.event("voiceChannelSwitch")
onVoiceChannelSwitch(member: Member, newChannel: Channel, oldChannel: Channel) {
this.serverLogs.log(LogType.VOICE_CHANNEL_MOVE, {
this.guildLogs.log(LogType.VOICE_CHANNEL_MOVE, {
member: stripObjectToScalars(member, ["user"]),
oldChannel: stripObjectToScalars(oldChannel),
newChannel: stripObjectToScalars(newChannel)

View file

@ -36,4 +36,10 @@ export class MessageSaverPlugin extends Plugin {
await this.savedMessages.saveEditFromMsg(msg);
}
@d.event("messageDeleteBulk", "guild", false)
async onMessageBulkDelete(messages: Message[]) {
const ids = messages.map(m => m.id);
await this.savedMessages.markBulkAsDeleted(ids);
}
}

View file

@ -39,17 +39,7 @@ interface IRecentAction {
const MAX_INTERVAL = 300;
const ARCHIVE_EXPIRY_DAYS = 90;
const ARCHIVE_HEADER_FORMAT = trimLines(`
Server: {guild.name} ({guild.id})
Channel: #{channel.name} ({channel.id})
User: {user.username}#{user.discriminator} ({user.id})
`);
const ARCHIVE_MESSAGE_FORMAT = "[MSG ID {id}] [{timestamp}] {user.username}: {content}{attachments}";
const ARCHIVE_FOOTER_FORMAT = trimLines(`
Log file generated on {timestamp}
Expires at {expires}
`);
const SPAM_ARCHIVE_EXPIRY_DAYS = 90;
export class SpamPlugin extends Plugin {
protected logs: GuildLogs;
@ -163,31 +153,11 @@ export class SpamPlugin extends Plugin {
}
async saveSpamArchives(savedMessages: SavedMessage[], channel: Channel, user: User) {
const expiresAt = moment().add(ARCHIVE_EXPIRY_DAYS, "days");
const headerStr = formatTemplateString(ARCHIVE_HEADER_FORMAT, {
guild: this.guild,
channel,
user
});
const msgLines = savedMessages.map(msg => {
return formatTemplateString(ARCHIVE_MESSAGE_FORMAT, {
id: msg.id,
timestamp: moment(msg.posted_at).format("HH:mm:ss"),
content: msg.data.content,
user
});
});
const messagesStr = msgLines.join("\n");
const footerStr = formatTemplateString(ARCHIVE_FOOTER_FORMAT, {
timestamp: moment().format("YYYY-MM-DD [at] HH:mm:ss (Z)"),
expires: expiresAt.format("YYYY-MM-DD [at] HH:mm:ss (Z)")
});
const logId = await this.archives.create([headerStr, messagesStr, footerStr].join("\n\n"), expiresAt);
const expiresAt = moment().add(SPAM_ARCHIVE_EXPIRY_DAYS, "days");
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, channel, user, expiresAt);
const url = this.knub.getGlobalConfig().url;
return url ? `${url}/archives/${logId}` : `Archive ID: ${logId}`;
return url ? `${url}/archives/${archiveId}` : `Archive ID: ${archiveId}`;
}
async logAndDetectSpam(
@ -285,13 +255,13 @@ export class SpamPlugin extends Plugin {
// Generate a log from the detected messages
const channel = this.guild.channels.get(savedMessage.channel_id);
const user = this.bot.users.get(savedMessage.user_id);
const logUrl = await this.saveSpamArchives(uniqueMessages, channel, user);
const archiveUrl = await this.saveSpamArchives(uniqueMessages, channel, user);
// Create a case and log the actions taken above
const caseType = spamConfig.mute ? CaseTypes.Mute : CaseTypes.Note;
const caseText = trimLines(`
Automatic spam detection: ${description} (over ${spamConfig.count} in ${spamConfig.interval}s)
${logUrl}
${archiveUrl}
`);
this.logs.log(LogType.SPAM_DETECTED, {
@ -300,7 +270,7 @@ export class SpamPlugin extends Plugin {
description,
limit: spamConfig.count,
interval: spamConfig.interval,
logUrl
archiveUrl
});
const caseId = await modActionsPlugin.createCase(

View file

@ -1,12 +1,15 @@
import { Plugin, decorators as d, reply } from "knub";
import { Channel, EmbedOptions, Message, TextChannel, User, VoiceChannel } from "eris";
import { embedPadding, errorMessage, getMessages, stripObjectToScalars, successMessage, trimLines } from "../utils";
import { embedPadding, errorMessage, stripObjectToScalars, successMessage, trimLines } from "../utils";
import { GuildLogs } from "../data/GuildLogs";
import { LogType } from "../data/LogType";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { GuildCases } from "../data/GuildCases";
import { CaseTypes } from "../data/CaseTypes";
import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { GuildArchives } from "../data/GuildArchives";
const MAX_SEARCH_RESULTS = 15;
const MAX_CLEAN_COUNT = 50;
@ -16,6 +19,8 @@ const activeReloads: Map<string, TextChannel> = new Map();
export class UtilityPlugin extends Plugin {
protected logs: GuildLogs;
protected cases: GuildCases;
protected savedMessages: GuildSavedMessages;
protected archives: GuildArchives;
getDefaultOptions() {
return {
@ -48,6 +53,8 @@ export class UtilityPlugin extends Plugin {
onLoad() {
this.logs = new GuildLogs(this.guildId);
this.cases = GuildCases.getInstance(this.guildId);
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
this.archives = GuildArchives.getInstance(this.guildId);
if (activeReloads && activeReloads.has(this.guildId)) {
activeReloads.get(this.guildId).createMessage(successMessage("Reloaded!"));
@ -125,14 +132,22 @@ export class UtilityPlugin extends Plugin {
}
}
async cleanMessages(channel: Channel, messageIds: string[], mod: User) {
this.logs.ignoreLog(LogType.MESSAGE_DELETE, messageIds[0]);
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, messageIds[0]);
await this.bot.deleteMessages(channel.id, messageIds);
async cleanMessages(channel: Channel, savedMessages: SavedMessage[], mod: User) {
this.logs.ignoreLog(LogType.MESSAGE_DELETE, savedMessages[0].id);
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, savedMessages[0].id);
await this.bot.deleteMessages(channel.id, savedMessages.map(m => m.id));
savedMessages.reverse();
const user = this.bot.users.get(savedMessages[0].user_id);
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, channel, user);
const archiveUrl = `${this.knub.getGlobalConfig().url}/archives/${archiveId}`;
this.logs.log(LogType.CLEAN, {
mod: stripObjectToScalars(mod),
channel: stripObjectToScalars(channel),
count: messageIds.length
count: savedMessages.length,
archiveUrl
});
}
@ -145,9 +160,9 @@ export class UtilityPlugin extends Plugin {
return;
}
const messagesToClean = await getMessages(msg.channel as TextChannel, m => m.id !== msg.id, args.count);
const messagesToClean = await this.savedMessages.getLatestByChannelBeforeId(msg.channel.id, msg.id, args.count);
if (messagesToClean.length > 0) {
await this.cleanMessages(msg.channel, messagesToClean.map(m => m.id), msg.author);
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
}
msg.channel.createMessage(
@ -163,13 +178,9 @@ export class UtilityPlugin extends Plugin {
return;
}
const messagesToClean = await getMessages(
msg.channel as TextChannel,
m => m.id !== msg.id && m.author.id === args.userId,
args.count
);
const messagesToClean = await this.savedMessages.getLatestByChannelAndUser(msg.channel.id, args.userId, args.count);
if (messagesToClean.length > 0) {
await this.cleanMessages(msg.channel, messagesToClean.map(m => m.id), msg.author);
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
}
msg.channel.createMessage(
@ -185,13 +196,9 @@ export class UtilityPlugin extends Plugin {
return;
}
const messagesToClean = await getMessages(
msg.channel as TextChannel,
m => m.id !== msg.id && m.author.bot,
args.count
);
const messagesToClean = await this.savedMessages.getLatestBotMessagesByChannel(msg.channel.id, args.count);
if (messagesToClean.length > 0) {
await this.cleanMessages(msg.channel, messagesToClean.map(m => m.id), msg.author);
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
}
msg.channel.createMessage(