3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-14 21:31:50 +00:00

fix(logs): fix inconsistent thread/channel/category exclusions; add excluded_threads log channel option

This commit is contained in:
Dragory 2022-08-13 23:19:06 +03:00
parent 218c31231e
commit d472fd4fa6
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
32 changed files with 132 additions and 51 deletions

View file

@ -12,6 +12,7 @@ import {
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { UnknownUser } from "../../../utils";
import { deactivateMentions, disableCodeBlocks } from "knub/dist/helpers";
import { resolveChannelIds } from "src/utils/resolveChannelIds";
interface LogCensorData {
user: User | UnknownUser;
@ -33,9 +34,8 @@ export function logCensor(pluginData: GuildPluginData<LogsPluginType>, data: Log
}),
{
userId: data.user.id,
channel: data.channel.id,
category: data.channel.parentId,
bot: data.user instanceof User ? data.user.bot : false,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { GuildChannel, NewsChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogChannelCreateData {
channel: GuildChannel | NewsChannel;
@ -18,8 +19,7 @@ export function logChannelCreate(pluginData: GuildPluginData<LogsPluginType>, da
channel: channelToTemplateSafeChannel(data.channel),
}),
{
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { GuildChannel, NewsChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogChannelDeleteData {
channel: GuildChannel | NewsChannel;
@ -18,8 +19,7 @@ export function logChannelDelete(pluginData: GuildPluginData<LogsPluginType>, da
channel: channelToTemplateSafeChannel(data.channel),
}),
{
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { GuildChannel, NewsChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogChannelUpdateData {
oldChannel: GuildChannel | NewsChannel;
@ -22,8 +23,7 @@ export function logChannelUpdate(pluginData: GuildPluginData<LogsPluginType>, da
differenceString: data.differenceString,
}),
{
channel: data.newChannel.id,
category: data.newChannel.parentId,
...resolveChannelIds(data.newChannel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, User } from "discord.js";
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogCleanData {
mod: User;
@ -24,8 +25,7 @@ export function logClean(pluginData: GuildPluginData<LogsPluginType>, data: LogC
archiveUrl: data.archiveUrl,
}),
{
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -13,6 +13,7 @@ import moment from "moment-timezone";
import { ISavedMessageAttachmentData, SavedMessage } from "../../../data/entities/SavedMessage";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UnknownUser, useMediaUrls } from "../../../utils";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageDeleteData {
user: User | UnknownUser;
@ -47,10 +48,9 @@ export function logMessageDelete(pluginData: GuildPluginData<LogsPluginType>, da
}),
{
userId: data.user.id,
channel: data.channel.id,
category: data.channel.parentId,
messageTextContent: data.message.data.content,
bot: data.user instanceof User ? data.user.bot : false,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -11,6 +11,7 @@ import {
} from "../../../utils/templateSafeObjects";
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { UnknownUser } from "../../../utils";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageDeleteAutoData {
message: SavedMessage;
@ -32,8 +33,7 @@ export function logMessageDeleteAuto(pluginData: GuildPluginData<LogsPluginType>
{
userId: data.user.id,
bot: data.user instanceof User ? data.user.bot : false,
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageDeleteBareData {
messageId: string;
@ -20,8 +21,7 @@ export function logMessageDeleteBare(pluginData: GuildPluginData<LogsPluginType>
channel: channelToTemplateSafeChannel(data.channel),
}),
{
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageDeleteBulkData {
count: number;
@ -24,8 +25,7 @@ export function logMessageDeleteBulk(pluginData: GuildPluginData<LogsPluginType>
archiveUrl: data.archiveUrl,
}),
{
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -11,6 +11,7 @@ import {
} from "../../../utils/templateSafeObjects";
import { SavedMessage } from "../../../data/entities/SavedMessage";
import { UnknownUser } from "../../../utils";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageEditData {
user: User | UnknownUser;
@ -31,9 +32,9 @@ export function logMessageEdit(pluginData: GuildPluginData<LogsPluginType>, data
}),
{
userId: data.user.id,
channel: data.channel.id,
messageTextContent: data.after.data.content,
bot: data.user instanceof User ? data.user.bot : false,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildChannel, GuildMember, ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel, memberToTemplateSafeMember } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogMessageSpamDetectedData {
member: GuildMember;
@ -30,9 +31,8 @@ export function logMessageSpamDetected(pluginData: GuildPluginData<LogsPluginTyp
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.channel.id,
category: data.channel.parentId,
bot: data.member.user.bot,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel, User } from "discord.js";
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogPostedScheduledMessageData {
author: User;
@ -27,8 +28,7 @@ export function logPostedScheduledMessage(
{
userId: data.author.id,
bot: data.author.bot,
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel, User } from "discord.js";
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogRepeatedMessageData {
author: User;
@ -32,8 +33,7 @@ export function logRepeatedMessage(pluginData: GuildPluginData<LogsPluginType>,
{
userId: data.author.id,
bot: data.author.bot,
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel, User } from "discord.js";
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogScheduledMessageData {
author: User;
@ -28,8 +29,7 @@ export function logScheduledMessage(pluginData: GuildPluginData<LogsPluginType>,
{
userId: data.author.id,
bot: data.author.bot,
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildTextChannel, GuildTextBasedChannel, ThreadChannel, User } from "discord.js";
import { channelToTemplateSafeChannel, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogScheduledRepeatedMessageData {
author: User;
@ -35,8 +36,7 @@ export function logScheduledRepeatedMessage(
{
userId: data.author.id,
bot: data.author.bot,
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { StageChannel, StageInstance } from "discord.js";
import { channelToTemplateSafeChannel, stageToTemplateSafeStage } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogStageInstanceCreateData {
stageInstance: StageInstance;
@ -20,8 +21,7 @@ export function logStageInstanceCreate(pluginData: GuildPluginData<LogsPluginTyp
stageChannel: channelToTemplateSafeChannel(data.stageChannel),
}),
{
channel: data.stageInstance.channel!.id,
category: data.stageInstance.channel!.parentId,
...resolveChannelIds(data.stageInstance.channel!),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { StageChannel, StageInstance } from "discord.js";
import { channelToTemplateSafeChannel, stageToTemplateSafeStage } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogStageInstanceDeleteData {
stageInstance: StageInstance;
@ -20,8 +21,7 @@ export function logStageInstanceDelete(pluginData: GuildPluginData<LogsPluginTyp
stageChannel: channelToTemplateSafeChannel(data.stageChannel),
}),
{
channel: data.stageInstance.channel!.id,
category: data.stageInstance.channel!.parentId,
...resolveChannelIds(data.stageInstance.channel!),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { StageChannel, StageInstance } from "discord.js";
import { channelToTemplateSafeChannel, stageToTemplateSafeStage } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogStageInstanceUpdateData {
oldStageInstance: StageInstance;
@ -24,8 +25,7 @@ export function logStageInstanceUpdate(pluginData: GuildPluginData<LogsPluginTyp
differenceString: data.differenceString,
}),
{
channel: data.newStageInstance.channel!.id,
category: data.newStageInstance.channel!.parentId,
...resolveChannelIds(data.newStageInstance.channel!),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogThreadCreateData {
thread: ThreadChannel;
@ -18,8 +19,7 @@ export function logThreadCreate(pluginData: GuildPluginData<LogsPluginType>, dat
thread: channelToTemplateSafeChannel(data.thread),
}),
{
channel: data.thread.parentId,
category: data.thread.parent?.parentId,
...resolveChannelIds(data.thread),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogThreadDeleteData {
thread: ThreadChannel;
@ -18,8 +19,7 @@ export function logThreadDelete(pluginData: GuildPluginData<LogsPluginType>, dat
thread: channelToTemplateSafeChannel(data.thread),
}),
{
channel: data.thread.parentId,
category: data.thread.parent?.parentId,
...resolveChannelIds(data.thread),
},
);
}

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { ThreadChannel } from "discord.js";
import { channelToTemplateSafeChannel } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogThreadUpdateData {
oldThread: ThreadChannel;
@ -22,8 +23,7 @@ export function logThreadUpdate(pluginData: GuildPluginData<LogsPluginType>, dat
differenceString: data.differenceString,
}),
{
channel: data.newThread.parentId,
category: data.newThread.parent?.parentId,
...resolveChannelIds(data.newThread),
},
);
}

View file

@ -9,6 +9,7 @@ import {
memberToTemplateSafeMember,
userToTemplateSafeUser,
} from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogVoiceChannelForceDisconnectData {
mod: User;
@ -31,8 +32,7 @@ export function logVoiceChannelForceDisconnect(
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.oldChannel.id,
category: data.oldChannel.parentId,
...resolveChannelIds(data.oldChannel),
bot: data.member.user.bot,
},
);

View file

@ -9,6 +9,7 @@ import {
memberToTemplateSafeMember,
userToTemplateSafeUser,
} from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogVoiceChannelForceMoveData {
mod: User;
@ -33,8 +34,7 @@ export function logVoiceChannelForceMove(
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.newChannel.id,
category: data.newChannel.parentId,
...resolveChannelIds(data.newChannel),
bot: data.member.user.bot,
},
);

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildVoiceChannel, GuildMember } from "discord.js";
import { channelToTemplateSafeChannel, memberToTemplateSafeMember } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogVoiceChannelJoinData {
member: GuildMember;
@ -22,8 +23,7 @@ export function logVoiceChannelJoin(pluginData: GuildPluginData<LogsPluginType>,
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
bot: data.member.user.bot,
},
);

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildVoiceChannel, GuildMember } from "discord.js";
import { channelToTemplateSafeChannel, memberToTemplateSafeMember } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogVoiceChannelLeaveData {
member: GuildMember;
@ -22,8 +23,7 @@ export function logVoiceChannelLeave(pluginData: GuildPluginData<LogsPluginType>
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.channel.id,
category: data.channel.parentId,
...resolveChannelIds(data.channel),
bot: data.member.user.bot,
},
);

View file

@ -5,6 +5,7 @@ import { log } from "../util/log";
import { createTypedTemplateSafeValueContainer } from "../../../templateFormatter";
import { BaseGuildVoiceChannel, GuildMember } from "discord.js";
import { channelToTemplateSafeChannel, memberToTemplateSafeMember } from "../../../utils/templateSafeObjects";
import { resolveChannelIds } from "../../../utils/resolveChannelIds";
interface LogVoiceChannelMoveData {
member: GuildMember;
@ -24,8 +25,7 @@ export function logVoiceChannelMove(pluginData: GuildPluginData<LogsPluginType>,
{
userId: data.member.id,
roles: Array.from(data.member.roles.cache.keys()),
channel: data.newChannel.id,
category: data.newChannel.parentId,
...resolveChannelIds(data.newChannel),
bot: data.member.user.bot,
},
);

View file

@ -42,6 +42,7 @@ const LogChannel = t.partial({
excluded_message_regexes: t.array(TRegex),
excluded_channels: t.array(t.string),
excluded_categories: t.array(t.string),
excluded_threads: t.array(t.string),
exclude_bots: t.boolean,
excluded_roles: t.array(t.string),
format: tNullable(tLogFormats),

View file

@ -22,6 +22,7 @@ interface ExclusionData {
roles?: Snowflake[] | null;
channel?: Snowflake | null;
category?: Snowflake | null;
thread?: Snowflake | null;
messageTextContent?: string | null;
}
@ -58,6 +59,10 @@ async function shouldExclude(
return true;
}
if (opts.excluded_threads && exclusionData.thread && opts.excluded_threads.includes(exclusionData.thread)) {
return true;
}
if (opts.excluded_message_regexes && exclusionData.messageTextContent) {
for (const regex of opts.excluded_message_regexes) {
const matches = await pluginData.state.regexRunner

View file

@ -0,0 +1,6 @@
import { Channel, DMChannel } from "discord.js";
import { ChannelTypeStrings } from "src/types";
export function isDmChannel(channel: Channel): channel is DMChannel {
return channel.type === ChannelTypeStrings.DM || channel.type === ChannelTypeStrings.GROUP;
}

View file

@ -0,0 +1,5 @@
import { Channel, GuildChannel } from "discord.js";
export function isGuildChannel(channel: Channel): channel is GuildChannel {
return channel.type.startsWith("GUILD_");
}

View file

@ -0,0 +1,10 @@
import { Channel, ThreadChannel } from "discord.js";
import { ChannelTypeStrings } from "src/types";
export function isThreadChannel(channel: Channel): channel is ThreadChannel {
return (
channel.type === ChannelTypeStrings.NEWS_THREAD ||
channel.type === ChannelTypeStrings.PUBLIC_THREAD ||
channel.type === ChannelTypeStrings.PRIVATE_THREAD
);
}

View file

@ -0,0 +1,53 @@
import { ChannelType } from "discord-api-types/v9";
import { CategoryChannel, Channel } from "discord.js";
import { ChannelTypes } from "discord.js/typings/enums";
import { ChannelTypeStrings } from "src/types";
import { isDmChannel } from "./isDmChannel";
import { isGuildChannel } from "./isGuildChannel";
import { isThreadChannel } from "./isThreadChannel";
type ResolvedChannelIds = {
category: string | null;
channel: string | null;
thread: string | null;
};
export function resolveChannelIds(channel: Channel): ResolvedChannelIds {
if (isDmChannel(channel)) {
return {
category: null,
channel: channel.id,
thread: null,
};
}
if (isThreadChannel(channel)) {
return {
category: channel.parent?.parentId || null,
channel: channel.parentId,
thread: channel.id,
};
}
if (channel instanceof CategoryChannel) {
return {
category: channel.id,
channel: null,
thread: null,
};
}
if (isGuildChannel(channel)) {
return {
category: channel.parentId,
channel: channel.id,
thread: null,
};
}
return {
category: null,
channel: channel.id,
thread: null,
};
}