3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Add templateFormatter; migrate from formatTemplateString to templateFormatter

This commit is contained in:
Dragory 2019-03-16 15:42:55 +02:00
parent 5a29068bf1
commit ba3af1cb63
6 changed files with 435 additions and 32 deletions

View file

@ -3,9 +3,10 @@ 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 { trimLines } from "../utils";
import { SavedMessage } from "./entities/SavedMessage";
import { Channel, Guild, User } from "eris";
import { renderTemplate } from "../templateFormatter";
const DEFAULT_EXPIRY_DAYS = 30;
@ -40,7 +41,7 @@ export class GuildArchives extends BaseRepository {
async find(id: string): Promise<ArchiveEntry> {
return this.archives.findOne({
where: { id },
relations: this.getRelations()
relations: this.getRelations(),
});
}
@ -48,8 +49,8 @@ export class GuildArchives extends BaseRepository {
await this.archives.update(
{ id },
{
expires_at: null
}
expires_at: null,
},
);
}
@ -64,28 +65,30 @@ export class GuildArchives extends BaseRepository {
const result = await this.archives.insert({
guild_id: this.guildId,
body,
expires_at: expiresAt.format("YYYY-MM-DD HH:mm:ss")
expires_at: expiresAt.format("YYYY-MM-DD HH:mm:ss"),
});
return result.identifiers[0].id;
}
createFromSavedMessages(savedMessages: SavedMessage[], guild: Guild, expiresAt = null) {
async 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 });
const msgLines = savedMessages.map(msg => {
const headerStr = await renderTemplate(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild });
const msgLines = [];
for (const msg of savedMessages) {
const channel = guild.channels.get(msg.channel_id);
const user = { ...msg.data.author, id: msg.user_id };
return formatTemplateString(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {
const line = await renderTemplate(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {
id: msg.id,
timestamp: moment(msg.posted_at).format("YYYY-MM-DD HH:mm:ss"),
content: msg.data.content,
user,
channel
channel,
});
});
msgLines.push(line);
}
const messagesStr = msgLines.join("\n");
return this.create([headerStr, messagesStr].join("\n\n"), expiresAt);