From a9351d07cd8344f634a67b19d1825a586e1104b8 Mon Sep 17 00:00:00 2001 From: almeidx Date: Tue, 26 Oct 2021 23:48:18 +0100 Subject: [PATCH] remove config in favour of overrides --- backend/src/data/GuildSavedMessages.ts | 17 -------------- .../events/runAutomodOnThreadEvents.ts | 23 +++++++------------ .../plugins/Automod/functions/runAutomod.ts | 8 ++++--- .../plugins/Automod/triggers/threadCreate.ts | 15 +++--------- .../plugins/Automod/triggers/threadDelete.ts | 18 ++++----------- backend/src/plugins/Automod/types.ts | 3 ++- 6 files changed, 23 insertions(+), 61 deletions(-) diff --git a/backend/src/data/GuildSavedMessages.ts b/backend/src/data/GuildSavedMessages.ts index 66ff2e42..e6c51e91 100644 --- a/backend/src/data/GuildSavedMessages.ts +++ b/backend/src/data/GuildSavedMessages.ts @@ -25,23 +25,6 @@ export class GuildSavedMessages extends BaseGuildRepository { this.toBePermanent = new Set(); } - public msgToSavedMessage(message: Message): SavedMessage { - const postedAt = moment.utc(message.createdTimestamp, "x").format("YYYY-MM-DD HH:mm:ss"); - - return { - data: this.msgToSavedMessageData(message), - id: message.id, - guild_id: (message.channel as GuildChannel).guildId, - channel_id: message.channelId, - user_id: message.author.id, - is_bot: message.author.bot, - posted_at: postedAt, - // @ts-expect-error - deleted_at: null, - is_permanent: false, - }; - } - protected msgToSavedMessageData(msg: Message): ISavedMessageData { const data: ISavedMessageData = { author: { diff --git a/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts b/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts index 2539017c..9e1f4507 100644 --- a/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts +++ b/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts @@ -5,28 +5,18 @@ import { AutomodContext, AutomodPluginType } from "../types"; export const RunAutomodOnThreadCreate = typedGuildEventListener()({ event: "threadCreate", async listener({ pluginData, args: { thread } }) { - const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0; + const user = thread.ownerId + ? await pluginData.client.users.fetch(thread.ownerId).catch(() => undefined) + : undefined; const context: AutomodContext = { timestamp: Date.now(), threadChange: { created: thread, }, user, + channel: thread, }; - // This is a hack to make this trigger compatible with the reply action - const sourceChannel = thread.parent ?? pluginData.client.channels.cache.find((c) => c.id === thread.parentId); - if (sourceChannel?.isText()) { - const sourceMessage = sourceChannel.messages.cache.find( - (m) => m.thread?.id === thread.id || m.reference?.channelId === thread.id, - ); - if (sourceMessage) { - const savedMessage = pluginData.state.savedMessages.msgToSavedMessage(sourceMessage); - savedMessage.channel_id = thread.id; - context.message = savedMessage; - } - } - pluginData.state.queue.add(() => { runAutomod(pluginData, context); }); @@ -36,7 +26,9 @@ export const RunAutomodOnThreadCreate = typedGuildEventListener()({ event: "threadDelete", async listener({ pluginData, args: { thread } }) { - const user = thread.ownerId ? await pluginData.client.users.fetch(thread.ownerId).catch(() => void 0) : void 0; + const user = thread.ownerId + ? await pluginData.client.users.fetch(thread.ownerId).catch(() => undefined) + : undefined; const context: AutomodContext = { timestamp: Date.now(), @@ -44,6 +36,7 @@ export const RunAutomodOnThreadDelete = typedGuildEventListener { diff --git a/backend/src/plugins/Automod/functions/runAutomod.ts b/backend/src/plugins/Automod/functions/runAutomod.ts index 6658e0a2..88d9f9bb 100644 --- a/backend/src/plugins/Automod/functions/runAutomod.ts +++ b/backend/src/plugins/Automod/functions/runAutomod.ts @@ -15,9 +15,11 @@ export async function runAutomod(pluginData: GuildPluginData, const member = context.member || (userId && pluginData.guild.members.cache.get(userId as Snowflake)) || null; const channelIdOrThreadId = context.message?.channel_id; - const channelOrThread = channelIdOrThreadId - ? (pluginData.guild.channels.cache.get(channelIdOrThreadId as Snowflake) as TextChannel | ThreadChannel) - : null; + const channelOrThread = + context.channel ?? + (channelIdOrThreadId + ? (pluginData.guild.channels.cache.get(channelIdOrThreadId as Snowflake) as TextChannel | ThreadChannel) + : null); const channelId = channelOrThread?.isThread() ? channelOrThread.parent?.id : channelIdOrThreadId; const threadId = channelOrThread?.isThread() ? channelOrThread.id : null; const channel = channelOrThread?.isThread() ? channelOrThread.parent : channelOrThread; diff --git a/backend/src/plugins/Automod/triggers/threadCreate.ts b/backend/src/plugins/Automod/triggers/threadCreate.ts index e95d5bd2..f4bc7e7a 100644 --- a/backend/src/plugins/Automod/triggers/threadCreate.ts +++ b/backend/src/plugins/Automod/triggers/threadCreate.ts @@ -1,7 +1,6 @@ import { Snowflake } from "discord-api-types"; -import { ThreadChannel, User, Util } from "discord.js"; +import { User, Util } from "discord.js"; import * as t from "io-ts"; -import { tNullable } from "../../../utils"; import { automodTrigger } from "../helpers"; interface ThreadCreateResult { @@ -13,24 +12,16 @@ interface ThreadCreateResult { } export const ThreadCreateTrigger = automodTrigger()({ - configType: t.type({ - parent: tNullable(t.union([t.string, t.array(t.string)])), - }), - + configType: t.type({}), defaultConfig: {}, - async match({ context, triggerConfig }) { + async match({ context }) { if (!context.threadChange?.created) { return; } const thread = context.threadChange.created; - if (triggerConfig.parent) { - const parentIds = Array.isArray(triggerConfig.parent) ? triggerConfig.parent : [triggerConfig.parent]; - if (thread.parentId && !parentIds.includes(thread.parentId)) return; - } - return { extra: { matchedThreadId: thread.id, diff --git a/backend/src/plugins/Automod/triggers/threadDelete.ts b/backend/src/plugins/Automod/triggers/threadDelete.ts index 1a22fdd5..988ee752 100644 --- a/backend/src/plugins/Automod/triggers/threadDelete.ts +++ b/backend/src/plugins/Automod/triggers/threadDelete.ts @@ -1,7 +1,6 @@ import { Snowflake } from "discord-api-types"; import { User, Util } from "discord.js"; import * as t from "io-ts"; -import { tNullable } from "../../../utils"; import { automodTrigger } from "../helpers"; interface ThreadDeleteResult { @@ -13,24 +12,16 @@ interface ThreadDeleteResult { } export const ThreadDeleteTrigger = automodTrigger()({ - configType: t.type({ - parent: tNullable(t.union([t.string, t.array(t.string)])), - }), - + configType: t.type({}), defaultConfig: {}, - async match({ context, triggerConfig }) { + async match({ context }) { if (!context.threadChange?.deleted) { return; } const thread = context.threadChange.deleted; - if (triggerConfig.parent) { - const parentIds = Array.isArray(triggerConfig.parent) ? triggerConfig.parent : [triggerConfig.parent]; - if (thread.parentId && !parentIds.includes(thread.parentId)) return; - } - return { extra: { matchedThreadId: thread.id, @@ -53,7 +44,8 @@ export const ThreadDeleteTrigger = automodTrigger()({ threadOwner.tag, )}** (\`${threadOwner.id}\`) in the **#${parentName}** (\`${parentId}\`) channel has been deleted`; } - return `Thread **#${threadName ?? - "Unknown"}** (\`${threadId}\`) from the **#${parentName}** (\`${parentId}\`) channel has been deleted`; + return `Thread **#${ + threadName ?? "Unknown" + }** (\`${threadId}\`) from the **#${parentName}** (\`${parentId}\`) channel has been deleted`; }, }); diff --git a/backend/src/plugins/Automod/types.ts b/backend/src/plugins/Automod/types.ts index d6bd165d..7ae40f30 100644 --- a/backend/src/plugins/Automod/types.ts +++ b/backend/src/plugins/Automod/types.ts @@ -1,4 +1,4 @@ -import { GuildMember, PartialGuildMember, ThreadChannel, User } from "discord.js"; +import { GuildMember, PartialGuildMember, TextChannel, ThreadChannel, User } from "discord.js"; import * as t from "io-ts"; import { BasePluginType, CooldownManager } from "knub"; import { SavedMessage } from "../../data/entities/SavedMessage"; @@ -135,6 +135,7 @@ export interface AutomodContext { created?: ThreadChannel; deleted?: ThreadChannel; }; + channel?: TextChannel | ThreadChannel; } export interface RecentAction {