2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-07-27 01:53:14 +02:00
|
|
|
import { LogsPluginType, TLogChannelMap } from "../types";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { LogType } from "../../../data/LogType";
|
2021-06-01 02:05:55 +02:00
|
|
|
|
2021-05-22 14:05:09 +03:00
|
|
|
import { createChunkedMessage, get, noop } from "../../../utils";
|
2020-07-27 01:53:14 +02:00
|
|
|
import { getLogMessage } from "./getLogMessage";
|
2020-08-05 01:15:36 +03:00
|
|
|
import { allowTimeout } from "../../../RegExpRunner";
|
2021-05-24 20:43:46 +03:00
|
|
|
import { SavedMessage } from "../../../data/entities/SavedMessage";
|
2021-06-01 04:33:02 +02:00
|
|
|
import { MessageMentionTypes, TextChannel } from "discord.js";
|
2020-07-27 01:53:14 +02:00
|
|
|
|
2020-07-30 13:40:34 +03:00
|
|
|
const excludedUserProps = ["user", "member", "mod"];
|
2021-05-22 14:05:09 +03:00
|
|
|
const excludedRoleProps = ["message.member.roles", "member.roles"];
|
|
|
|
|
|
|
|
function isRoleArray(value: any): value is string[] {
|
|
|
|
return Array.isArray(value);
|
|
|
|
}
|
2020-07-30 13:40:34 +03:00
|
|
|
|
2020-10-01 01:43:38 +03:00
|
|
|
export async function log(pluginData: GuildPluginData<LogsPluginType>, type: LogType, data: any) {
|
2020-07-27 01:53:14 +02:00
|
|
|
const logChannels: TLogChannelMap = pluginData.config.get().channels;
|
|
|
|
const typeStr = LogType[type];
|
|
|
|
|
|
|
|
logChannelLoop: for (const [channelId, opts] of Object.entries(logChannels)) {
|
2021-06-01 02:05:55 +02:00
|
|
|
const channel = pluginData.guild.channels.cache.get(channelId);
|
2020-07-27 01:53:14 +02:00
|
|
|
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) {
|
2020-07-30 13:40:34 +03:00
|
|
|
for (const prop of excludedUserProps) {
|
2020-07-27 01:53:14 +02:00
|
|
|
if (data && data[prop] && opts.excluded_users.includes(data[prop].id)) {
|
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:55:33 +02:00
|
|
|
// 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) {
|
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 14:05:09 +03:00
|
|
|
if (opts.excluded_roles) {
|
2021-05-24 20:55:03 +03:00
|
|
|
for (const value of Object.values(data || {})) {
|
|
|
|
if (value instanceof SavedMessage) {
|
2021-06-01 02:05:55 +02:00
|
|
|
const member = pluginData.guild.members.cache.get(value.user_id);
|
2021-06-01 04:33:02 +02:00
|
|
|
for (const role of member?.roles.cache || []) {
|
|
|
|
if (opts.excluded_roles.includes(role[0])) {
|
2021-05-24 20:55:03 +03:00
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
2021-05-24 20:43:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 14:05:09 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 01:53:14 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:52:56 +02:00
|
|
|
// 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
|
|
|
|
) {
|
2020-12-24 00:48:04 +02:00
|
|
|
if (data.channel.parentID && opts.excluded_categories.includes(data.channel.parentID)) {
|
2020-11-08 17:52:56 +02:00
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 01:53:14 +02:00
|
|
|
// 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) {
|
2020-08-05 01:15:36 +03:00
|
|
|
const matches = await pluginData.state.regexRunner.exec(regex, data.message.data.content).catch(allowTimeout);
|
|
|
|
if (matches) {
|
2020-07-27 01:53:14 +02:00
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === LogType.MESSAGE_EDIT && opts.excluded_message_regexes && data.before.data.content) {
|
|
|
|
for (const regex of opts.excluded_message_regexes) {
|
2020-08-05 01:19:09 +03:00
|
|
|
const matches = await pluginData.state.regexRunner.exec(regex, data.before.data.content).catch(allowTimeout);
|
2020-08-05 01:15:36 +03:00
|
|
|
if (matches) {
|
2020-07-27 01:53:14 +02:00
|
|
|
continue logChannelLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 02:32:55 +03:00
|
|
|
const message = await getLogMessage(pluginData, type, data, {
|
|
|
|
format: opts.format,
|
|
|
|
include_embed_timestamp: opts.include_embed_timestamp,
|
|
|
|
timestamp_format: opts.timestamp_format,
|
|
|
|
});
|
|
|
|
|
2020-07-27 01:53:14 +02:00
|
|
|
if (message) {
|
2020-07-30 02:21:31 +03:00
|
|
|
// For non-string log messages (i.e. embeds) batching or chunking is not possible, so send them immediately
|
|
|
|
if (typeof message !== "string") {
|
2021-06-01 04:33:02 +02:00
|
|
|
await channel.send(message).catch(noop);
|
2020-07-30 02:21:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to batched unless explicitly disabled
|
|
|
|
const batched = opts.batched ?? true;
|
2020-07-27 01:53:14 +02:00
|
|
|
const batchTime = opts.batch_time ?? 1000;
|
2021-01-28 00:26:24 +01:00
|
|
|
const cfg = pluginData.config.get();
|
2021-06-01 04:33:02 +02:00
|
|
|
const parse: MessageMentionTypes[] | undefined = cfg.allow_user_mentions ? ["users"] : undefined;
|
2020-07-27 01:53:14 +02:00
|
|
|
|
|
|
|
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 () => {
|
2020-11-09 20:03:57 +02:00
|
|
|
const batchedMessage = pluginData.state.batches.get(channel.id)!.join("\n");
|
2020-07-27 01:53:14 +02:00
|
|
|
pluginData.state.batches.delete(channel.id);
|
2021-06-01 04:33:02 +02:00
|
|
|
createChunkedMessage(channel, batchedMessage, { parse }).catch(noop);
|
2020-07-27 01:53:14 +02:00
|
|
|
}, batchTime);
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
pluginData.state.batches.get(channel.id)!.push(message);
|
2020-07-27 01:53:14 +02:00
|
|
|
} else {
|
|
|
|
// If we're not batching log messages, just send them immediately
|
2021-06-01 04:33:02 +02:00
|
|
|
await createChunkedMessage(channel, message, { parse }).catch(noop);
|
2020-07-27 01:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|