3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35: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

@ -26,8 +26,8 @@
"MESSAGE_EDIT": "✏ **{member.user.username}#{member.user.discriminator}** (`{member.id}`) message edited in **#{channel.name}**:\nBefore:```{before}```After:```{after}```",
"MESSAGE_DELETE": "🗑 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) message deleted in **#{channel.name}**:\n```{messageText}```",
"MESSAGE_DELETE_BULK": "🗑 **{count}** messages deleted in **#{channel.name}**",
"MESSAGE_DELETE_BARE": "🗑 Message (`{messageId}`) deleted in **#{channel.name}** (no more info available due to bot restart)",
"MESSAGE_DELETE_BULK": "🗑 **{count}** messages deleted in **#{channel.name}** ({archiveUrl})",
"MESSAGE_DELETE_BARE": "🗑 Message (`{messageId}`) deleted in **#{channel.name}** (no more info available)",
"VOICE_CHANNEL_JOIN": "🎙 🔵 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) joined **{channel.name}**",
"VOICE_CHANNEL_MOVE": "🎙 ↔ **{member.user.username}#{member.user.discriminator}** (`{member.id}`) moved from **{oldChannel.name}** to **{newChannel.name}**",
@ -35,9 +35,9 @@
"COMMAND": "🤖 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) used command in **#{channel.name}**:\n`{command}`",
"SPAM_DETECTED": "🛑 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) spam detected in **#{channel.name}**: {description} (more than {limit} in {interval}s)\n{logUrl}",
"SPAM_DETECTED": "🛑 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) spam detected in **#{channel.name}**: {description} (more than {limit} in {interval}s)\n{archiveUrl}",
"CENSOR": "🛑 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) censored message in **#{channel.name}** (`{channel.id}`) {reason}:\n```{messageText}```",
"CLEAN": "🚿 **{mod.username}#{mod.discriminator}** (`{mod.id}`) cleaned **{count}** message(s) in **#{channel.name}**",
"CLEAN": "🚿 **{mod.username}#{mod.discriminator}** (`{mod.id}`) cleaned **{count}** message(s) in **#{channel.name}**\n{archiveUrl}",
"CASE_CREATE": "✏ **{member.user.username}#{member.user.discriminator}** (`{member.id}`) manually created new **{caseType}** case (#{caseNum})",

View file

@ -3,9 +3,23 @@ import moment from "moment-timezone";
import { ArchiveEntry } from "./entities/ArchiveEntry";
import { getRepository, Repository } from "typeorm";
import { BaseRepository } from "./BaseRepository";
import { formatTemplateString, trimLines } from "../utils";
import { SavedMessage } from "./entities/SavedMessage";
import { Channel, Guild, User } from "eris";
const DEFAULT_EXPIRY_DAYS = 30;
const MESSAGE_ARCHIVE_HEADER_FORMAT = trimLines(`
Server: {guild.name} ({guild.id})
Channel: #{channel.name} ({channel.id})
User: {user.username}#{user.discriminator} ({user.id})
`);
const MESSAGE_ARCHIVE_MESSAGE_FORMAT = "[MSG ID {id}] [{timestamp}] {user.username}: {content}{attachments}";
const MESSAGE_ARCHIVE_FOOTER_FORMAT = trimLines(`
Log file generated on {timestamp}
Expires at {expires}
`);
export class GuildArchives extends BaseRepository {
protected archives: Repository<ArchiveEntry>;
@ -50,4 +64,31 @@ export class GuildArchives extends BaseRepository {
return result.identifiers[0].id;
}
createFromSavedMessages(
savedMessages: SavedMessage[],
guild: Guild,
channel: Channel = null,
user: User = null,
expiresAt = null
) {
if (expiresAt == null) expiresAt = moment().add(DEFAULT_EXPIRY_DAYS, "days");
const headerStr = formatTemplateString(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild, channel, user });
const msgLines = savedMessages.map(msg => {
return formatTemplateString(MESSAGE_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(MESSAGE_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)")
});
return this.create([headerStr, messagesStr, footerStr].join("\n\n"), expiresAt);
}
}

View file

@ -77,16 +77,54 @@ export class GuildSavedMessages extends BaseRepository {
.getOne();
}
getUserMessagesByChannelAfterId(userId, channelId, afterId) {
getLatestBotMessagesByChannel(channelId, limit) {
return this.messages
.createQueryBuilder()
.where("guild_id = :guild_id", { guild_id: this.guildId })
.where("user_id = :user_id", { user_id: userId })
.where("channel_id = :channel_id", { channel_id: channelId })
.where("id > :afterId", { afterId })
.andWhere("channel_id = :channel_id", { channel_id: channelId })
.andWhere("is_bot = 1")
.orderBy("id", "DESC")
.limit(limit)
.getMany();
}
getLatestByChannelBeforeId(channelId, beforeId, limit) {
return this.messages
.createQueryBuilder()
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("channel_id = :channel_id", { channel_id: channelId })
.andWhere("id < :beforeId", { beforeId })
.orderBy("id", "DESC")
.limit(limit)
.getMany();
}
getLatestByChannelAndUser(channelId, userId, limit) {
return this.messages
.createQueryBuilder()
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("channel_id = :channel_id", { channel_id: channelId })
.andWhere("user_id = :user_id", { user_id: userId })
.orderBy("id", "DESC")
.limit(limit)
.getMany();
}
getUserMessagesByChannelAfterId(userId, channelId, afterId, limit = null) {
let query = this.messages
.createQueryBuilder()
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("user_id = :user_id", { user_id: userId })
.andWhere("channel_id = :channel_id", { channel_id: channelId })
.andWhere("id > :afterId", { afterId });
if (limit != null) {
query = query.limit(limit);
}
return query.getMany();
}
async create(data) {
const isPermanent = this.toBePermanent.has(data.id);
if (isPermanent) {
@ -137,6 +175,23 @@ export class GuildSavedMessages extends BaseRepository {
this.events.emit("delete", [deleted]);
}
async markBulkAsDeleted(ids) {
await this.messages
.createQueryBuilder()
.update()
.set({
deleted_at: () => "NOW(3)"
})
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("id IN (:ids)", { ids })
.execute();
return this.messages
.createQueryBuilder()
.where("id IN (:ids)", { ids })
.getMany();
}
async saveEdit(id, newData: ISavedMessageData) {
const oldMessage = await this.messages.findOne(id);
const newMessage = { ...oldMessage, data: newData };