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

Typed log functions + more

This commit is contained in:
Dragory 2021-08-18 01:51:42 +03:00
parent d2ac700143
commit bed6589d48
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
166 changed files with 4021 additions and 869 deletions

View file

@ -1,12 +1,17 @@
import { Guild, Snowflake } from "discord.js";
import { Guild, Snowflake, User } from "discord.js";
import moment from "moment-timezone";
import { isDefaultSticker } from "src/utils/isDefaultSticker";
import { getRepository, Repository } from "typeorm";
import { renderTemplate } from "../templateFormatter";
import { renderTemplate, TemplateSafeValueContainer } from "../templateFormatter";
import { trimLines } from "../utils";
import { BaseGuildRepository } from "./BaseGuildRepository";
import { ArchiveEntry } from "./entities/ArchiveEntry";
import { SavedMessage } from "./entities/SavedMessage";
import {
channelToTemplateSafeChannel,
guildToTemplateSafeGuild,
userToTemplateSafeUser,
} from "../utils/templateSafeObjects";
const DEFAULT_EXPIRY_DAYS = 30;
@ -75,9 +80,9 @@ export class GuildArchives extends BaseGuildRepository {
const msgLines: string[] = [];
for (const msg of savedMessages) {
const channel = guild.channels.cache.get(msg.channel_id as Snowflake);
const user = { ...msg.data.author, id: msg.user_id };
const partialUser = new TemplateSafeValueContainer({ ...msg.data.author, id: msg.user_id });
const line = await renderTemplate(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {
const values = new TemplateSafeValueContainer({
id: msg.id,
timestamp: moment.utc(msg.posted_at).format("YYYY-MM-DD HH:mm:ss"),
content: msg.data.content,
@ -87,9 +92,10 @@ export class GuildArchives extends BaseGuildRepository {
stickers: msg.data.stickers?.map(sti => {
return JSON.stringify({ name: sti.name, id: sti.id, isDefault: isDefaultSticker(sti.id) });
}),
user,
channel,
user: partialUser,
channel: channel ? channelToTemplateSafeChannel(channel) : null,
});
const line = await renderTemplate(MESSAGE_ARCHIVE_MESSAGE_FORMAT, {});
msgLines.push(line);
}
return msgLines;
@ -100,7 +106,9 @@ export class GuildArchives extends BaseGuildRepository {
expiresAt = moment.utc().add(DEFAULT_EXPIRY_DAYS, "days");
}
const headerStr = await renderTemplate(MESSAGE_ARCHIVE_HEADER_FORMAT, { guild });
const headerStr = await renderTemplate(MESSAGE_ARCHIVE_HEADER_FORMAT, {
guild: guildToTemplateSafeGuild(guild),
});
const msgLines = await this.renderLinesFromSavedMessages(savedMessages, guild);
const messagesStr = msgLines.join("\n");

View file

@ -45,9 +45,90 @@ export class GuildSavedMessages extends BaseGuildRepository {
timestamp: msg.createdTimestamp,
};
if (msg.attachments.size) data.attachments = [...msg.attachments.values()];
if (msg.embeds.length) data.embeds = msg.embeds;
if (msg.stickers?.size) data.stickers = [...msg.stickers.values()];
if (msg.attachments.size) {
data.attachments = Array.from(msg.attachments.values()).map(att => ({
id: att.id,
contentType: att.contentType,
name: att.name,
proxyURL: att.proxyURL,
size: att.size,
spoiler: att.spoiler,
url: att.url,
width: att.width,
}));
}
if (msg.embeds.length) {
data.embeds = msg.embeds.map(embed => ({
title: embed.title,
description: embed.description,
url: embed.url,
timestamp: embed.timestamp,
color: embed.color,
fields: embed.fields.map(field => ({
name: field.name,
value: field.value,
inline: field.inline,
})),
author: embed.author
? {
name: embed.author.name,
url: embed.author.url,
iconURL: embed.author.iconURL,
proxyIconURL: embed.author.proxyIconURL,
}
: undefined,
thumbnail: embed.thumbnail
? {
url: embed.thumbnail.url,
proxyURL: embed.thumbnail.proxyURL,
height: embed.thumbnail.height,
width: embed.thumbnail.width,
}
: undefined,
image: embed.image
? {
url: embed.image.url,
proxyURL: embed.image.proxyURL,
height: embed.image.height,
width: embed.image.width,
}
: undefined,
video: embed.video
? {
url: embed.video.url,
proxyURL: embed.video.proxyURL,
height: embed.video.height,
width: embed.video.width,
}
: undefined,
footer: embed.footer
? {
text: embed.footer.text,
iconURL: embed.footer.iconURL,
proxyIconURL: embed.footer.proxyIconURL,
}
: undefined,
}));
}
if (msg.stickers?.size) {
data.stickers = Array.from(msg.stickers.values()).map(sticker => ({
format: sticker.format,
guildId: sticker.guildId,
id: sticker.id,
name: sticker.name,
description: sticker.description,
available: sticker.available,
type: sticker.type,
}));
}
return data;
}

View file

@ -1,16 +1,79 @@
import { MessageAttachment, Sticker } from "discord.js";
import { Snowflake } from "discord.js";
import { Column, Entity, PrimaryColumn } from "typeorm";
import { createEncryptedJsonTransformer } from "../encryptedJsonTransformer";
export interface ISavedMessageAttachmentData {
id: Snowflake;
contentType: string | null;
name: string | null;
proxyURL: string;
size: number;
spoiler: boolean;
url: string;
width: number | null;
}
export interface ISavedMessageEmbedData {
title: string | null;
description: string | null;
url: string | null;
timestamp: number | null;
color: number | null;
fields: Array<{
name: string;
value: string;
inline: boolean;
}>;
author?: {
name?: string;
url?: string;
iconURL?: string;
proxyIconURL?: string;
};
thumbnail?: {
url: string;
proxyURL?: string;
height?: number;
width?: number;
};
image?: {
url: string;
proxyURL?: string;
height?: number;
width?: number;
};
video?: {
url?: string;
proxyURL?: string;
height?: number;
width?: number;
};
footer?: {
text?: string;
iconURL?: string;
proxyIconURL?: string;
};
}
export interface ISavedMessageStickerData {
format: string;
guildId: Snowflake | null;
id: Snowflake;
name: string;
description: string | null;
available: boolean | null;
type: string | null;
}
export interface ISavedMessageData {
attachments?: MessageAttachment[];
attachments?: ISavedMessageAttachmentData[];
author: {
username: string;
discriminator: string;
};
content: string;
embeds?: object[];
stickers?: Sticker[];
embeds?: ISavedMessageEmbedData[];
stickers?: ISavedMessageStickerData[];
timestamp: number;
}