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,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;
}