Migrate Logs to new Plugin structure, also dont ignore it
This commit is contained in:
parent
140ba84544
commit
aea019181c
16 changed files with 764 additions and 1 deletions
80
backend/src/plugins/Logs/util/getLogMessage.ts
Normal file
80
backend/src/plugins/Logs/util/getLogMessage.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import { PluginData } from "knub";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { verboseUserMention, verboseUserName, verboseChannelMention, messageSummary, resolveMember } from "src/utils";
|
||||
import { SavedMessage } from "src/data/entities/SavedMessage";
|
||||
import { renderTemplate, TemplateParseError } from "src/templateFormatter";
|
||||
import { logger } from "src/logger";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
export async function getLogMessage(pluginData: PluginData<LogsPluginType>, type, data): Promise<string> {
|
||||
const config = pluginData.config.get();
|
||||
const format = config.format[LogType[type]] || "";
|
||||
if (format === "") return;
|
||||
|
||||
let formatted;
|
||||
try {
|
||||
const values = {
|
||||
...data,
|
||||
userMention: async inputUserOrMember => {
|
||||
if (!inputUserOrMember) return "";
|
||||
|
||||
const usersOrMembers = Array.isArray(inputUserOrMember) ? inputUserOrMember : [inputUserOrMember];
|
||||
|
||||
const mentions = [];
|
||||
for (const userOrMember of usersOrMembers) {
|
||||
let user;
|
||||
let member;
|
||||
|
||||
if (userOrMember.user) {
|
||||
member = await resolveMember(pluginData.client, pluginData.guild, userOrMember.id);
|
||||
user = member.user;
|
||||
} else {
|
||||
user = userOrMember;
|
||||
member = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
||||
}
|
||||
|
||||
const memberConfig = pluginData.config.getMatchingConfig({ member, userId: user.id }) || ({} as any);
|
||||
|
||||
mentions.push(memberConfig.ping_user ? verboseUserMention(user) : verboseUserName(user));
|
||||
}
|
||||
|
||||
return mentions.join(", ");
|
||||
},
|
||||
channelMention: channel => {
|
||||
if (!channel) return "";
|
||||
return verboseChannelMention(channel);
|
||||
},
|
||||
messageSummary: (msg: SavedMessage) => {
|
||||
if (!msg) return "";
|
||||
return messageSummary(msg);
|
||||
},
|
||||
};
|
||||
|
||||
if (type === LogType.BOT_ALERT) {
|
||||
const valuesWithoutTmplEval = { ...values };
|
||||
values.tmplEval = str => {
|
||||
return renderTemplate(str, valuesWithoutTmplEval);
|
||||
};
|
||||
}
|
||||
|
||||
formatted = await renderTemplate(format, values);
|
||||
} catch (e) {
|
||||
if (e instanceof TemplateParseError) {
|
||||
logger.error(`Error when parsing template:\nError: ${e.message}\nTemplate: ${format}`);
|
||||
return;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
formatted = formatted.trim();
|
||||
|
||||
const timestampFormat = config.format.timestamp;
|
||||
if (timestampFormat) {
|
||||
const timestamp = moment().format(timestampFormat);
|
||||
return `\`[${timestamp}]\` ${formatted}`;
|
||||
} else {
|
||||
return formatted;
|
||||
}
|
||||
}
|
84
backend/src/plugins/Logs/util/log.ts
Normal file
84
backend/src/plugins/Logs/util/log.ts
Normal file
|
@ -0,0 +1,84 @@
|
|||
import { PluginData } from "knub";
|
||||
import { LogsPluginType, TLogChannelMap } from "../types";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { TextChannel } from "eris";
|
||||
import { createChunkedMessage, noop } from "src/utils";
|
||||
import { getLogMessage } from "./getLogMessage";
|
||||
|
||||
export async function log(pluginData: PluginData<LogsPluginType>, type, data) {
|
||||
const logChannels: TLogChannelMap = pluginData.config.get().channels;
|
||||
const typeStr = LogType[type];
|
||||
|
||||
logChannelLoop: for (const [channelId, opts] of Object.entries(logChannels)) {
|
||||
const channel = pluginData.guild.channels.get(channelId);
|
||||
if (!channel || !(channel instanceof TextChannel)) continue;
|
||||
|
||||
if ((opts.include && opts.include.includes(typeStr)) || (opts.exclude && !opts.exclude.includes(typeStr))) {
|
||||
// If this log entry is about an excluded user, skip it
|
||||
// TODO: Quick and dirty solution, look into changing at some point
|
||||
if (opts.excluded_users) {
|
||||
for (const prop of pluginData.state.excludedUserProps) {
|
||||
if (data && data[prop] && opts.excluded_users.includes(data[prop].id)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this entry is from an excluded channel, skip it
|
||||
if (opts.excluded_channels) {
|
||||
if (
|
||||
type === LogType.MESSAGE_DELETE ||
|
||||
type === LogType.MESSAGE_DELETE_BARE ||
|
||||
type === LogType.MESSAGE_EDIT ||
|
||||
type === LogType.MESSAGE_SPAM_DETECTED ||
|
||||
type === LogType.CENSOR ||
|
||||
type === LogType.CLEAN
|
||||
) {
|
||||
if (opts.excluded_channels.includes(data.channel.id)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this entry contains a message with an excluded regex, skip it
|
||||
if (type === LogType.MESSAGE_DELETE && opts.excluded_message_regexes && data.message.data.content) {
|
||||
for (const regex of opts.excluded_message_regexes) {
|
||||
if (regex.test(data.message.data.content)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type === LogType.MESSAGE_EDIT && opts.excluded_message_regexes && data.before.data.content) {
|
||||
for (const regex of opts.excluded_message_regexes) {
|
||||
if (regex.test(data.before.data.content)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const message = await getLogMessage(pluginData, type, data);
|
||||
if (message) {
|
||||
const batched = opts.batched ?? true; // Default to batched unless explicitly disabled
|
||||
const batchTime = opts.batch_time ?? 1000;
|
||||
|
||||
if (batched) {
|
||||
// If we're batching log messages, gather all log messages within the set batch_time into a single message
|
||||
if (!pluginData.state.batches.has(channel.id)) {
|
||||
pluginData.state.batches.set(channel.id, []);
|
||||
setTimeout(async () => {
|
||||
const batchedMessage = pluginData.state.batches.get(channel.id).join("\n");
|
||||
pluginData.state.batches.delete(channel.id);
|
||||
createChunkedMessage(channel, batchedMessage).catch(noop);
|
||||
}, batchTime);
|
||||
}
|
||||
|
||||
pluginData.state.batches.get(channel.id).push(message);
|
||||
} else {
|
||||
// If we're not batching log messages, just send them immediately
|
||||
await createChunkedMessage(channel, message).catch(noop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
41
backend/src/plugins/Logs/util/onMessageDelete.ts
Normal file
41
backend/src/plugins/Logs/util/onMessageDelete.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { SavedMessage } from "src/data/entities/SavedMessage";
|
||||
import { Attachment } from "eris";
|
||||
import { useMediaUrls, stripObjectToScalars, resolveUser } from "src/utils";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import moment from "moment-timezone";
|
||||
import { PluginData } from "knub";
|
||||
import { LogsPluginType } from "../types";
|
||||
|
||||
export async function onMessageDelete(pluginData: PluginData<LogsPluginType>, savedMessage: SavedMessage) {
|
||||
const user = await resolveUser(pluginData.client, savedMessage.user_id);
|
||||
const channel = pluginData.guild.channels.get(savedMessage.channel_id);
|
||||
|
||||
if (user) {
|
||||
// Replace attachment URLs with media URLs
|
||||
if (savedMessage.data.attachments) {
|
||||
for (const attachment of savedMessage.data.attachments as Attachment[]) {
|
||||
attachment.url = useMediaUrls(attachment.url);
|
||||
}
|
||||
}
|
||||
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE,
|
||||
{
|
||||
user: stripObjectToScalars(user),
|
||||
channel: stripObjectToScalars(channel),
|
||||
messageDate: moment(savedMessage.data.timestamp, "x").format(pluginData.config.get().format.timestamp),
|
||||
message: savedMessage,
|
||||
},
|
||||
savedMessage.id,
|
||||
);
|
||||
} else {
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE_BARE,
|
||||
{
|
||||
messageId: savedMessage.id,
|
||||
channel: stripObjectToScalars(channel),
|
||||
},
|
||||
savedMessage.id,
|
||||
);
|
||||
}
|
||||
}
|
21
backend/src/plugins/Logs/util/onMessageDeleteBulk.ts
Normal file
21
backend/src/plugins/Logs/util/onMessageDeleteBulk.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { PluginData } from "knub";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { SavedMessage } from "src/data/entities/SavedMessage";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { getBaseUrl } from "src/pluginUtils";
|
||||
|
||||
export async function onMessageDeleteBulk(pluginData: PluginData<LogsPluginType>, savedMessages: SavedMessage[]) {
|
||||
const channel = pluginData.guild.channels.get(savedMessages[0].channel_id);
|
||||
const archiveId = await pluginData.state.archives.createFromSavedMessages(savedMessages, pluginData.guild);
|
||||
const archiveUrl = pluginData.state.archives.getUrl(getBaseUrl, archiveId);
|
||||
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE_BULK,
|
||||
{
|
||||
count: savedMessages.length,
|
||||
channel,
|
||||
archiveUrl,
|
||||
},
|
||||
savedMessages[0].id,
|
||||
);
|
||||
}
|
58
backend/src/plugins/Logs/util/onMessageUpdate.ts
Normal file
58
backend/src/plugins/Logs/util/onMessageUpdate.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { PluginData } from "knub";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { SavedMessage } from "src/data/entities/SavedMessage";
|
||||
import { Embed } from "eris";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import { stripObjectToScalars, resolveUser } from "src/utils";
|
||||
import cloneDeep from "lodash.clonedeep";
|
||||
|
||||
export async function onMessageUpdate(
|
||||
pluginData: PluginData<LogsPluginType>,
|
||||
savedMessage: SavedMessage,
|
||||
oldSavedMessage: SavedMessage,
|
||||
) {
|
||||
// To log a message update, either the message content or a rich embed has to change
|
||||
let logUpdate = false;
|
||||
|
||||
const oldEmbedsToCompare = ((oldSavedMessage.data.embeds || []) as Embed[])
|
||||
.map(e => cloneDeep(e))
|
||||
.filter(e => (e as Embed).type === "rich");
|
||||
|
||||
const newEmbedsToCompare = ((savedMessage.data.embeds || []) as Embed[])
|
||||
.map(e => cloneDeep(e))
|
||||
.filter(e => (e as Embed).type === "rich");
|
||||
|
||||
for (const embed of [...oldEmbedsToCompare, ...newEmbedsToCompare]) {
|
||||
if (embed.thumbnail) {
|
||||
delete embed.thumbnail.width;
|
||||
delete embed.thumbnail.height;
|
||||
}
|
||||
|
||||
if (embed.image) {
|
||||
delete embed.image.width;
|
||||
delete embed.image.height;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
oldSavedMessage.data.content !== savedMessage.data.content ||
|
||||
oldEmbedsToCompare.length !== newEmbedsToCompare.length ||
|
||||
JSON.stringify(oldEmbedsToCompare) !== JSON.stringify(newEmbedsToCompare)
|
||||
) {
|
||||
logUpdate = true;
|
||||
}
|
||||
|
||||
if (!logUpdate) {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await resolveUser(pluginData.client, savedMessage.user_id);
|
||||
const channel = pluginData.guild.channels.get(savedMessage.channel_id);
|
||||
|
||||
pluginData.state.guildLogs.log(LogType.MESSAGE_EDIT, {
|
||||
user: stripObjectToScalars(user),
|
||||
channel: stripObjectToScalars(channel),
|
||||
before: oldSavedMessage,
|
||||
after: savedMessage,
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue