3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Message archive formatting tweaks and fixes; only emit deleteBulk event from SavedMessages if any messages were actually marked as deleted

This commit is contained in:
Dragory 2018-12-22 13:06:40 +02:00
parent dee4637a7f
commit ce0b7ded08
5 changed files with 36 additions and 26 deletions

View file

@ -11,10 +11,9 @@ 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_MESSAGE_FORMAT =
"[#{channel.name}] [{user.id}] [{timestamp}] {user.username}#{user.discriminator}: {content}{attachments}";
const MESSAGE_ARCHIVE_FOOTER_FORMAT = trimLines(`
Log file generated on {timestamp}
Expires at {expires}
@ -65,22 +64,20 @@ export class GuildArchives extends BaseRepository {
return result.identifiers[0].id;
}
createFromSavedMessages(
savedMessages: SavedMessage[],
guild: Guild,
channel: Channel = null,
user: User = null,
expiresAt = null
) {
createFromSavedMessages(savedMessages: SavedMessage[], guild: Guild, expiresAt = null) {
if (expiresAt == null) expiresAt = moment().add(DEFAULT_EXPIRY_DAYS, "days");
const headerStr = formatTemplateString(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild, channel, user });
const headerStr = formatTemplateString(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild });
const msgLines = savedMessages.map(msg => {
const channel = guild.channels.get(msg.channel_id);
const user = { ...msg.data.author, id: msg.user_id };
return formatTemplateString(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {
id: msg.id,
timestamp: moment(msg.posted_at).format("HH:mm:ss"),
timestamp: moment(msg.posted_at).format("YYYY-MM-DD HH:mm:ss"),
content: msg.data.content,
user
user,
channel
});
});
const messagesStr = msgLines.join("\n");

View file

@ -180,23 +180,31 @@ export class GuildSavedMessages extends BaseRepository {
this.events.emit("delete", [deleted]);
}
/**
* Marks the specified messages as deleted in the database (if they weren't already marked before).
* If any messages were marked as deleted, also emits the deleteBulk event.
*/
async markBulkAsDeleted(ids) {
const deletedAt = moment().format("YYYY-MM-DD HH:mm:ss.SSS");
await this.messages
.createQueryBuilder()
.update()
.set({
deleted_at: () => "NOW(3)"
})
.set({ deleted_at: deletedAt })
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("id IN (:ids)", { ids })
.andWhere("deleted_at IS NULL")
.execute();
const deleted = await this.messages
.createQueryBuilder()
.where("id IN (:ids)", { ids })
.where("deleted_at = :deletedAt", { deletedAt })
.getMany();
this.events.emit("deleteBulk", [deleted]);
if (deleted.length) {
this.events.emit("deleteBulk", [deleted]);
}
}
async saveEdit(id, newData: ISavedMessageData) {

View file

@ -326,8 +326,7 @@ export class LogsPlugin extends Plugin {
// Uses events from savesMessages
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 archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild);
const baseUrl = this.knub.getGlobalConfig().url;
this.guildLogs.log(

View file

@ -157,9 +157,9 @@ export class SpamPlugin extends Plugin {
this.recentActions = this.recentActions.filter(action => action.timestamp >= expiryTimestamp);
}
async saveSpamArchives(savedMessages: SavedMessage[], channel: Channel, user: User) {
async saveSpamArchives(savedMessages: SavedMessage[], channel: Channel) {
const expiresAt = moment().add(SPAM_ARCHIVE_EXPIRY_DAYS, "days");
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, channel, user, expiresAt);
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, expiresAt);
const url = this.knub.getGlobalConfig().url;
return url ? `${url}/archives/${archiveId}` : `Archive ID: ${archiveId}`;
@ -249,8 +249,7 @@ 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 archiveUrl = await this.saveSpamArchives(uniqueMessages, channel, user);
const archiveUrl = await this.saveSpamArchives(uniqueMessages, channel);
// Create a case and log the actions taken above
const caseType = spamConfig.mute ? CaseTypes.Mute : CaseTypes.Note;

View file

@ -140,13 +140,20 @@ export class UtilityPlugin extends ZeppelinPlugin {
this.logs.ignoreLog(LogType.MESSAGE_DELETE, savedMessages[0].id);
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, savedMessages[0].id);
// Delete & archive in ID order
savedMessages = Array.from(savedMessages).sort((a, b) => (a.id > b.id ? 1 : -1));
const idsToDelete = savedMessages.map(m => m.id);
// Make sure the deletions aren't double logged
idsToDelete.forEach(id => this.logs.ignoreLog(LogType.MESSAGE_DELETE, id));
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, idsToDelete[0]);
// Actually delete the messages
await this.bot.deleteMessages(channel.id, idsToDelete);
await this.savedMessages.markBulkAsDeleted(idsToDelete);
savedMessages.reverse();
const user = this.bot.users.get(savedMessages[0].user_id);
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild, channel, user);
// Create an archive
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild);
const archiveUrl = `${this.knub.getGlobalConfig().url}/archives/${archiveId}`;
this.logs.log(LogType.CLEAN, {