From d472fd4fa6a2111fe71f88715d269386f3f6fadc Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 13 Aug 2022 23:19:06 +0300 Subject: [PATCH] fix(logs): fix inconsistent thread/channel/category exclusions; add excluded_threads log channel option --- .../plugins/Logs/logFunctions/logCensor.ts | 4 +- .../Logs/logFunctions/logChannelCreate.ts | 4 +- .../Logs/logFunctions/logChannelDelete.ts | 4 +- .../Logs/logFunctions/logChannelUpdate.ts | 4 +- .../src/plugins/Logs/logFunctions/logClean.ts | 4 +- .../Logs/logFunctions/logMessageDelete.ts | 4 +- .../Logs/logFunctions/logMessageDeleteAuto.ts | 4 +- .../Logs/logFunctions/logMessageDeleteBare.ts | 4 +- .../Logs/logFunctions/logMessageDeleteBulk.ts | 4 +- .../Logs/logFunctions/logMessageEdit.ts | 3 +- .../logFunctions/logMessageSpamDetected.ts | 4 +- .../logFunctions/logPostedScheduledMessage.ts | 4 +- .../Logs/logFunctions/logRepeatedMessage.ts | 4 +- .../Logs/logFunctions/logScheduledMessage.ts | 4 +- .../logScheduledRepeatedMessage.ts | 4 +- .../logFunctions/logStageInstanceCreate.ts | 4 +- .../logFunctions/logStageInstanceDelete.ts | 4 +- .../logFunctions/logStageInstanceUpdate.ts | 4 +- .../Logs/logFunctions/logThreadCreate.ts | 4 +- .../Logs/logFunctions/logThreadDelete.ts | 4 +- .../Logs/logFunctions/logThreadUpdate.ts | 4 +- .../logVoiceChannelForceDisconnect.ts | 4 +- .../logFunctions/logVoiceChannelForceMove.ts | 4 +- .../Logs/logFunctions/logVoiceChannelJoin.ts | 4 +- .../Logs/logFunctions/logVoiceChannelLeave.ts | 4 +- .../Logs/logFunctions/logVoiceChannelMove.ts | 4 +- backend/src/plugins/Logs/types.ts | 1 + backend/src/plugins/Logs/util/log.ts | 5 ++ backend/src/utils/isDmChannel.ts | 6 +++ backend/src/utils/isGuildChannel.ts | 5 ++ backend/src/utils/isThreadChannel.ts | 10 ++++ backend/src/utils/resolveChannelIds.ts | 53 +++++++++++++++++++ 32 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 backend/src/utils/isDmChannel.ts create mode 100644 backend/src/utils/isGuildChannel.ts create mode 100644 backend/src/utils/isThreadChannel.ts create mode 100644 backend/src/utils/resolveChannelIds.ts diff --git a/backend/src/plugins/Logs/logFunctions/logCensor.ts b/backend/src/plugins/Logs/logFunctions/logCensor.ts index b0131657..c5048730 100644 --- a/backend/src/plugins/Logs/logFunctions/logCensor.ts +++ b/backend/src/plugins/Logs/logFunctions/logCensor.ts @@ -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, 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), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logChannelCreate.ts b/backend/src/plugins/Logs/logFunctions/logChannelCreate.ts index 02ccba3d..a95ffb48 100644 --- a/backend/src/plugins/Logs/logFunctions/logChannelCreate.ts +++ b/backend/src/plugins/Logs/logFunctions/logChannelCreate.ts @@ -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, da channel: channelToTemplateSafeChannel(data.channel), }), { - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logChannelDelete.ts b/backend/src/plugins/Logs/logFunctions/logChannelDelete.ts index 547ed963..25bd4374 100644 --- a/backend/src/plugins/Logs/logFunctions/logChannelDelete.ts +++ b/backend/src/plugins/Logs/logFunctions/logChannelDelete.ts @@ -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, da channel: channelToTemplateSafeChannel(data.channel), }), { - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logChannelUpdate.ts b/backend/src/plugins/Logs/logFunctions/logChannelUpdate.ts index ebe2ceb5..7942501e 100644 --- a/backend/src/plugins/Logs/logFunctions/logChannelUpdate.ts +++ b/backend/src/plugins/Logs/logFunctions/logChannelUpdate.ts @@ -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, da differenceString: data.differenceString, }), { - channel: data.newChannel.id, - category: data.newChannel.parentId, + ...resolveChannelIds(data.newChannel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logClean.ts b/backend/src/plugins/Logs/logFunctions/logClean.ts index 4ef2077e..6486873f 100644 --- a/backend/src/plugins/Logs/logFunctions/logClean.ts +++ b/backend/src/plugins/Logs/logFunctions/logClean.ts @@ -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, data: LogC archiveUrl: data.archiveUrl, }), { - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageDelete.ts b/backend/src/plugins/Logs/logFunctions/logMessageDelete.ts index 6a2d642c..e103b13b 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageDelete.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageDelete.ts @@ -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, 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), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageDeleteAuto.ts b/backend/src/plugins/Logs/logFunctions/logMessageDeleteAuto.ts index 4ce4694c..d4e66f28 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageDeleteAuto.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageDeleteAuto.ts @@ -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 { userId: data.user.id, bot: data.user instanceof User ? data.user.bot : false, - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageDeleteBare.ts b/backend/src/plugins/Logs/logFunctions/logMessageDeleteBare.ts index ed49d3e4..ad9320e3 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageDeleteBare.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageDeleteBare.ts @@ -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 channel: channelToTemplateSafeChannel(data.channel), }), { - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageDeleteBulk.ts b/backend/src/plugins/Logs/logFunctions/logMessageDeleteBulk.ts index b57b526b..2ffe36bf 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageDeleteBulk.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageDeleteBulk.ts @@ -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 archiveUrl: data.archiveUrl, }), { - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageEdit.ts b/backend/src/plugins/Logs/logFunctions/logMessageEdit.ts index 3aee8dbe..64f12e8f 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageEdit.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageEdit.ts @@ -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, 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), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logMessageSpamDetected.ts b/backend/src/plugins/Logs/logFunctions/logMessageSpamDetected.ts index 94e41eda..a18f197c 100644 --- a/backend/src/plugins/Logs/logFunctions/logMessageSpamDetected.ts +++ b/backend/src/plugins/Logs/logFunctions/logMessageSpamDetected.ts @@ -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, { userId: data.author.id, bot: data.author.bot, - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logScheduledMessage.ts b/backend/src/plugins/Logs/logFunctions/logScheduledMessage.ts index 54315e6f..8067dd6a 100644 --- a/backend/src/plugins/Logs/logFunctions/logScheduledMessage.ts +++ b/backend/src/plugins/Logs/logFunctions/logScheduledMessage.ts @@ -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, { userId: data.author.id, bot: data.author.bot, - channel: data.channel.id, - category: data.channel.parentId, + ...resolveChannelIds(data.channel), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logScheduledRepeatedMessage.ts b/backend/src/plugins/Logs/logFunctions/logScheduledRepeatedMessage.ts index 03272f5f..849b1a4a 100644 --- a/backend/src/plugins/Logs/logFunctions/logScheduledRepeatedMessage.ts +++ b/backend/src/plugins/Logs/logFunctions/logScheduledRepeatedMessage.ts @@ -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), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logStageInstanceCreate.ts b/backend/src/plugins/Logs/logFunctions/logStageInstanceCreate.ts index f1602c3b..6172a2bc 100644 --- a/backend/src/plugins/Logs/logFunctions/logStageInstanceCreate.ts +++ b/backend/src/plugins/Logs/logFunctions/logStageInstanceCreate.ts @@ -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, dat thread: channelToTemplateSafeChannel(data.thread), }), { - channel: data.thread.parentId, - category: data.thread.parent?.parentId, + ...resolveChannelIds(data.thread), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logThreadDelete.ts b/backend/src/plugins/Logs/logFunctions/logThreadDelete.ts index 2467682f..04827083 100644 --- a/backend/src/plugins/Logs/logFunctions/logThreadDelete.ts +++ b/backend/src/plugins/Logs/logFunctions/logThreadDelete.ts @@ -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, dat thread: channelToTemplateSafeChannel(data.thread), }), { - channel: data.thread.parentId, - category: data.thread.parent?.parentId, + ...resolveChannelIds(data.thread), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logThreadUpdate.ts b/backend/src/plugins/Logs/logFunctions/logThreadUpdate.ts index 80e20acc..c359a683 100644 --- a/backend/src/plugins/Logs/logFunctions/logThreadUpdate.ts +++ b/backend/src/plugins/Logs/logFunctions/logThreadUpdate.ts @@ -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, dat differenceString: data.differenceString, }), { - channel: data.newThread.parentId, - category: data.newThread.parent?.parentId, + ...resolveChannelIds(data.newThread), }, ); } diff --git a/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceDisconnect.ts b/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceDisconnect.ts index 4bdca809..9940a1a3 100644 --- a/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceDisconnect.ts +++ b/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceDisconnect.ts @@ -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, }, ); diff --git a/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceMove.ts b/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceMove.ts index 5f5ec1e7..90b5478c 100644 --- a/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceMove.ts +++ b/backend/src/plugins/Logs/logFunctions/logVoiceChannelForceMove.ts @@ -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, }, ); diff --git a/backend/src/plugins/Logs/logFunctions/logVoiceChannelJoin.ts b/backend/src/plugins/Logs/logFunctions/logVoiceChannelJoin.ts index 07c44309..71d5ea0c 100644 --- a/backend/src/plugins/Logs/logFunctions/logVoiceChannelJoin.ts +++ b/backend/src/plugins/Logs/logFunctions/logVoiceChannelJoin.ts @@ -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, { 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, }, ); diff --git a/backend/src/plugins/Logs/logFunctions/logVoiceChannelLeave.ts b/backend/src/plugins/Logs/logFunctions/logVoiceChannelLeave.ts index 3fc67abb..509494fc 100644 --- a/backend/src/plugins/Logs/logFunctions/logVoiceChannelLeave.ts +++ b/backend/src/plugins/Logs/logFunctions/logVoiceChannelLeave.ts @@ -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 { 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, }, ); diff --git a/backend/src/plugins/Logs/logFunctions/logVoiceChannelMove.ts b/backend/src/plugins/Logs/logFunctions/logVoiceChannelMove.ts index 14e47ccd..56090907 100644 --- a/backend/src/plugins/Logs/logFunctions/logVoiceChannelMove.ts +++ b/backend/src/plugins/Logs/logFunctions/logVoiceChannelMove.ts @@ -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, { 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, }, ); diff --git a/backend/src/plugins/Logs/types.ts b/backend/src/plugins/Logs/types.ts index d1905b8e..511636a6 100644 --- a/backend/src/plugins/Logs/types.ts +++ b/backend/src/plugins/Logs/types.ts @@ -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), diff --git a/backend/src/plugins/Logs/util/log.ts b/backend/src/plugins/Logs/util/log.ts index f95e1f07..26be9585 100644 --- a/backend/src/plugins/Logs/util/log.ts +++ b/backend/src/plugins/Logs/util/log.ts @@ -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 diff --git a/backend/src/utils/isDmChannel.ts b/backend/src/utils/isDmChannel.ts new file mode 100644 index 00000000..ad1a5781 --- /dev/null +++ b/backend/src/utils/isDmChannel.ts @@ -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; +} diff --git a/backend/src/utils/isGuildChannel.ts b/backend/src/utils/isGuildChannel.ts new file mode 100644 index 00000000..fa5a5199 --- /dev/null +++ b/backend/src/utils/isGuildChannel.ts @@ -0,0 +1,5 @@ +import { Channel, GuildChannel } from "discord.js"; + +export function isGuildChannel(channel: Channel): channel is GuildChannel { + return channel.type.startsWith("GUILD_"); +} diff --git a/backend/src/utils/isThreadChannel.ts b/backend/src/utils/isThreadChannel.ts new file mode 100644 index 00000000..6a1060df --- /dev/null +++ b/backend/src/utils/isThreadChannel.ts @@ -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 + ); +} diff --git a/backend/src/utils/resolveChannelIds.ts b/backend/src/utils/resolveChannelIds.ts new file mode 100644 index 00000000..bc7493e9 --- /dev/null +++ b/backend/src/utils/resolveChannelIds.ts @@ -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, + }; +}