Typed log functions + more
This commit is contained in:
parent
d2ac700143
commit
bed6589d48
166 changed files with 4021 additions and 869 deletions
|
@ -3,7 +3,12 @@ import { GuildPluginData } from "knub";
|
|||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { logger } from "../../../logger";
|
||||
import { renderTemplate, TemplateParseError } from "../../../templateFormatter";
|
||||
import {
|
||||
renderTemplate,
|
||||
TemplateParseError,
|
||||
TemplateSafeValueContainer,
|
||||
TypedTemplateSafeValueContainer,
|
||||
} from "../../../templateFormatter";
|
||||
import {
|
||||
messageSummary,
|
||||
renderRecursively,
|
||||
|
@ -13,17 +18,18 @@ import {
|
|||
verboseUserName,
|
||||
} from "../../../utils";
|
||||
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
||||
import { FORMAT_NO_TIMESTAMP, LogsPluginType, TLogChannel } from "../types";
|
||||
import { FORMAT_NO_TIMESTAMP, ILogTypeData, LogsPluginType, TLogChannel } from "../types";
|
||||
import {
|
||||
getConfigAccessibleMemberLevel,
|
||||
IConfigAccessibleMember,
|
||||
memberToConfigAccessibleMember,
|
||||
} from "../../../utils/configAccessibleObjects";
|
||||
getTemplateSafeMemberLevel,
|
||||
TemplateSafeMember,
|
||||
memberToTemplateSafeMember,
|
||||
TemplateSafeUser,
|
||||
} from "../../../utils/templateSafeObjects";
|
||||
|
||||
export async function getLogMessage(
|
||||
export async function getLogMessage<TLogType extends keyof ILogTypeData>(
|
||||
pluginData: GuildPluginData<LogsPluginType>,
|
||||
type: LogType,
|
||||
data: any,
|
||||
type: TLogType,
|
||||
data: TypedTemplateSafeValueContainer<ILogTypeData[TLogType]>,
|
||||
opts?: Pick<TLogChannel, "format" | "timestamp_format" | "include_embed_timestamp">,
|
||||
): Promise<MessageOptions | null> {
|
||||
const config = pluginData.config.get();
|
||||
|
@ -42,10 +48,12 @@ export async function getLogMessage(
|
|||
const isoTimestamp = time.toISOString();
|
||||
const timestamp = timestampFormat ? time.format(timestampFormat) : "";
|
||||
|
||||
const values = {
|
||||
const values = new TemplateSafeValueContainer({
|
||||
...data,
|
||||
timestamp,
|
||||
userMention: async inputUserOrMember => {
|
||||
userMention: async (
|
||||
inputUserOrMember: TemplateSafeUser | TemplateSafeMember | TemplateSafeUser[] | TemplateSafeMember[],
|
||||
) => {
|
||||
if (!inputUserOrMember) return "";
|
||||
|
||||
const usersOrMembers = Array.isArray(inputUserOrMember) ? inputUserOrMember : [inputUserOrMember];
|
||||
|
@ -53,20 +61,20 @@ export async function getLogMessage(
|
|||
const mentions: string[] = [];
|
||||
for (const userOrMember of usersOrMembers) {
|
||||
let user;
|
||||
let member: IConfigAccessibleMember | null = null;
|
||||
let member: TemplateSafeMember | null = null;
|
||||
|
||||
if (userOrMember.user) {
|
||||
member = userOrMember as IConfigAccessibleMember;
|
||||
member = userOrMember as TemplateSafeMember;
|
||||
user = member.user;
|
||||
} else {
|
||||
user = userOrMember;
|
||||
const apiMember = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
||||
if (apiMember) {
|
||||
member = memberToConfigAccessibleMember(apiMember);
|
||||
member = memberToTemplateSafeMember(apiMember);
|
||||
}
|
||||
}
|
||||
|
||||
const level = member ? getConfigAccessibleMemberLevel(pluginData, member) : 0;
|
||||
const level = member ? getTemplateSafeMemberLevel(pluginData, member) : 0;
|
||||
const memberConfig =
|
||||
(await pluginData.config.getMatchingConfig({
|
||||
level,
|
||||
|
@ -92,7 +100,7 @@ export async function getLogMessage(
|
|||
if (!msg) return "";
|
||||
return messageSummary(msg);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
if (type === LogType.BOT_ALERT) {
|
||||
const valuesWithoutTmplEval = { ...values };
|
||||
|
|
7
backend/src/plugins/Logs/util/isLogIgnored.ts
Normal file
7
backend/src/plugins/Logs/util/isLogIgnored.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
|
||||
export function isLogIgnored(pluginData: GuildPluginData<LogsPluginType>, type: LogType, ignoreId: string) {
|
||||
return pluginData.state.guildLogs.isLogIgnored(type, ignoreId);
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
import { MessageMentionTypes, Snowflake, TextChannel } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { allowTimeout } from "../../../RegExpRunner";
|
||||
import { createChunkedMessage, get, noop } from "../../../utils";
|
||||
import { LogsPluginType, TLogChannelMap } from "../types";
|
||||
import { ILogTypeData, LogsPluginType, LogTypeData, TLogChannelMap } from "../types";
|
||||
import { getLogMessage } from "./getLogMessage";
|
||||
import { TemplateSafeValueContainer, TypedTemplateSafeValueContainer } from "../../../templateFormatter";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
|
||||
const excludedUserProps = ["user", "member", "mod"];
|
||||
const excludedRoleProps = ["message.member.roles", "member.roles"];
|
||||
|
@ -14,7 +15,21 @@ function isRoleArray(value: any): value is string[] {
|
|||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
export async function log(pluginData: GuildPluginData<LogsPluginType>, type: LogType, data: any) {
|
||||
interface ExclusionData {
|
||||
userId?: Snowflake | null;
|
||||
bot?: boolean | null;
|
||||
roles?: Snowflake[] | null;
|
||||
channel?: Snowflake | null;
|
||||
category?: Snowflake | null;
|
||||
messageTextContent?: string | null;
|
||||
}
|
||||
|
||||
export async function log<TLogType extends keyof ILogTypeData>(
|
||||
pluginData: GuildPluginData<LogsPluginType>,
|
||||
type: TLogType,
|
||||
data: TypedTemplateSafeValueContainer<ILogTypeData[TLogType]>,
|
||||
exclusionData: ExclusionData = {},
|
||||
) {
|
||||
const logChannels: TLogChannelMap = pluginData.config.get().channels;
|
||||
const typeStr = LogType[type];
|
||||
|
||||
|
@ -25,94 +40,40 @@ export async function log(pluginData: GuildPluginData<LogsPluginType>, type: Log
|
|||
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 excludedUserProps) {
|
||||
if (data && data[prop] && opts.excluded_users.includes(data[prop].id)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
if (opts.excluded_users && exclusionData.userId && opts.excluded_users.includes(exclusionData.userId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we're excluding bots and the logged user is a bot, skip it
|
||||
if (opts.exclude_bots) {
|
||||
for (const prop of excludedUserProps) {
|
||||
if (data && data[prop] && data[prop].bot) {
|
||||
if (opts.exclude_bots && exclusionData.bot) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (opts.excluded_roles && exclusionData.roles) {
|
||||
for (const role of exclusionData.roles) {
|
||||
if (opts.excluded_roles.includes(role)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.excluded_roles) {
|
||||
for (const value of Object.values(data || {})) {
|
||||
if (value instanceof SavedMessage) {
|
||||
const member = pluginData.guild.members.cache.get(value.user_id as Snowflake);
|
||||
for (const role of member?.roles.cache || []) {
|
||||
if (opts.excluded_roles.includes(role[0])) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const prop of excludedRoleProps) {
|
||||
const roles = get(data, prop);
|
||||
if (!isRoleArray(roles)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const role of roles) {
|
||||
if (opts.excluded_roles.includes(role)) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (opts.excluded_channels && exclusionData.channel && opts.excluded_channels.includes(exclusionData.channel)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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 (
|
||||
opts.excluded_categories &&
|
||||
exclusionData.category &&
|
||||
opts.excluded_categories.includes(exclusionData.category)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this entry is from an excluded category, skip it
|
||||
if (opts.excluded_categories) {
|
||||
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 (data.channel.parentId && opts.excluded_categories.includes(data.channel.parentId)) {
|
||||
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) {
|
||||
if (opts.excluded_message_regexes && exclusionData.messageTextContent) {
|
||||
for (const regex of opts.excluded_message_regexes) {
|
||||
const matches = await pluginData.state.regexRunner.exec(regex, data.message.data.content).catch(allowTimeout);
|
||||
if (matches) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type === LogType.MESSAGE_EDIT && opts.excluded_message_regexes && data.before.data.content) {
|
||||
for (const regex of opts.excluded_message_regexes) {
|
||||
const matches = await pluginData.state.regexRunner.exec(regex, data.before.data.content).catch(allowTimeout);
|
||||
const matches = await pluginData.state.regexRunner
|
||||
.exec(regex, exclusionData.messageTextContent)
|
||||
.catch(allowTimeout);
|
||||
if (matches) {
|
||||
continue logChannelLoop;
|
||||
}
|
||||
|
|
|
@ -1,51 +1,33 @@
|
|||
import { MessageAttachment, Snowflake } from "discord.js";
|
||||
import { BaseGuildTextChannel, Snowflake, ThreadChannel } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import moment from "moment-timezone";
|
||||
import { channelToConfigAccessibleChannel, userToConfigAccessibleUser } from "../../../utils/configAccessibleObjects";
|
||||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { resolveUser, useMediaUrls } from "../../../utils";
|
||||
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
||||
import { FORMAT_NO_TIMESTAMP, LogsPluginType } from "../types";
|
||||
import { resolveUser } from "../../../utils";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { logMessageDelete } from "../logFunctions/logMessageDelete";
|
||||
import { isLogIgnored } from "./isLogIgnored";
|
||||
import { logMessageDeleteBare } from "../logFunctions/logMessageDeleteBare";
|
||||
|
||||
export async function onMessageDelete(pluginData: GuildPluginData<LogsPluginType>, savedMessage: SavedMessage) {
|
||||
const user = await resolveUser(pluginData.client, savedMessage.user_id);
|
||||
const channel = pluginData.guild.channels.resolve(savedMessage.channel_id as Snowflake)!;
|
||||
const channel = pluginData.guild.channels.resolve(savedMessage.channel_id as Snowflake)! as
|
||||
| BaseGuildTextChannel
|
||||
| ThreadChannel;
|
||||
|
||||
if (isLogIgnored(pluginData, LogType.MESSAGE_DELETE, savedMessage.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (user) {
|
||||
// Replace attachment URLs with media URLs
|
||||
if (savedMessage.data.attachments) {
|
||||
for (const attachment of savedMessage.data.attachments as MessageAttachment[]) {
|
||||
attachment.url = useMediaUrls(attachment.url);
|
||||
}
|
||||
}
|
||||
|
||||
// See comment on FORMAT_NO_TIMESTAMP in types.ts
|
||||
const config = pluginData.config.get();
|
||||
const timestampFormat =
|
||||
(config.format.timestamp !== FORMAT_NO_TIMESTAMP ? config.format.timestamp : null) ?? config.timestamp_format;
|
||||
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE,
|
||||
{
|
||||
user: userToConfigAccessibleUser(user),
|
||||
channel: channelToConfigAccessibleChannel(channel),
|
||||
messageDate: pluginData
|
||||
.getPlugin(TimeAndDatePlugin)
|
||||
.inGuildTz(moment.utc(savedMessage.data.timestamp, "x"))
|
||||
.format(timestampFormat),
|
||||
message: savedMessage,
|
||||
},
|
||||
savedMessage.id,
|
||||
);
|
||||
logMessageDelete(pluginData, {
|
||||
user,
|
||||
channel,
|
||||
message: savedMessage,
|
||||
});
|
||||
} else {
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE_BARE,
|
||||
{
|
||||
messageId: savedMessage.id,
|
||||
channel: channelToConfigAccessibleChannel(channel),
|
||||
},
|
||||
savedMessage.id,
|
||||
);
|
||||
logMessageDeleteBare(pluginData, {
|
||||
messageId: savedMessage.id,
|
||||
channel,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,28 @@
|
|||
import { Snowflake } from "discord.js";
|
||||
import { BaseGuildTextChannel, Snowflake, ThreadChannel } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { getBaseUrl } from "../../../pluginUtils";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { logMessageDeleteBulk } from "../logFunctions/logMessageDeleteBulk";
|
||||
import { isLogIgnored } from "./isLogIgnored";
|
||||
|
||||
export async function onMessageDeleteBulk(pluginData: GuildPluginData<LogsPluginType>, savedMessages: SavedMessage[]) {
|
||||
const channel = pluginData.guild.channels.cache.get(savedMessages[0].channel_id as Snowflake);
|
||||
if (isLogIgnored(pluginData, LogType.MESSAGE_DELETE, savedMessages[0].id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const channel = pluginData.guild.channels.cache.get(savedMessages[0].channel_id as Snowflake) as
|
||||
| BaseGuildTextChannel
|
||||
| ThreadChannel;
|
||||
const archiveId = await pluginData.state.archives.createFromSavedMessages(savedMessages, pluginData.guild);
|
||||
const archiveUrl = pluginData.state.archives.getUrl(getBaseUrl(pluginData), archiveId);
|
||||
const authorIds = Array.from(new Set(savedMessages.map(item => `\`${item.user_id}\``))).join(", ");
|
||||
const authorIds = Array.from(new Set(savedMessages.map(item => `\`${item.user_id}\``)));
|
||||
|
||||
pluginData.state.guildLogs.log(
|
||||
LogType.MESSAGE_DELETE_BULK,
|
||||
{
|
||||
count: savedMessages.length,
|
||||
authorIds,
|
||||
channel,
|
||||
archiveUrl,
|
||||
},
|
||||
savedMessages[0].id,
|
||||
);
|
||||
logMessageDeleteBulk(pluginData, {
|
||||
count: savedMessages.length,
|
||||
authorIds,
|
||||
channel,
|
||||
archiveUrl,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { MessageEmbed, Snowflake } from "discord.js";
|
||||
import { BaseGuildTextChannel, MessageEmbed, Snowflake, ThreadChannel } from "discord.js";
|
||||
import { GuildPluginData } from "knub";
|
||||
import cloneDeep from "lodash.clonedeep";
|
||||
import { channelToConfigAccessibleChannel, userToConfigAccessibleUser } from "../../../utils/configAccessibleObjects";
|
||||
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
|
||||
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
||||
import { LogType } from "../../../data/LogType";
|
||||
import { resolveUser } from "../../../utils";
|
||||
import { LogsPluginType } from "../types";
|
||||
import { logMessageEdit } from "../logFunctions/logMessageEdit";
|
||||
|
||||
export async function onMessageUpdate(
|
||||
pluginData: GuildPluginData<LogsPluginType>,
|
||||
|
@ -48,11 +49,13 @@ export async function onMessageUpdate(
|
|||
}
|
||||
|
||||
const user = await resolveUser(pluginData.client, savedMessage.user_id);
|
||||
const channel = pluginData.guild.channels.resolve(savedMessage.channel_id as Snowflake)!;
|
||||
const channel = pluginData.guild.channels.resolve(savedMessage.channel_id as Snowflake)! as
|
||||
| BaseGuildTextChannel
|
||||
| ThreadChannel;
|
||||
|
||||
pluginData.state.guildLogs.log(LogType.MESSAGE_EDIT, {
|
||||
user: userToConfigAccessibleUser(user),
|
||||
channel: channelToConfigAccessibleChannel(channel),
|
||||
logMessageEdit(pluginData, {
|
||||
user,
|
||||
channel,
|
||||
before: oldSavedMessage,
|
||||
after: savedMessage,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue