From f84461a20d51ed49641374ab27a7fa922fa88233 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 14:05:13 +0000 Subject: [PATCH] cleanup & add template rendering --- .../plugins/Automod/actions/changePerms.ts | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index fd27094f..34f16105 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -3,6 +3,12 @@ import * as t from "io-ts"; import { automodAction } from "../helpers"; import { tNullable, isValidSnowflake, tPartialDictionary } from "../../../utils"; import { noop } from "knub/dist/utils"; +import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter"; +import { + guildToTemplateSafeGuild, + savedMessageToTemplateSafeSavedMessage, + userToTemplateSafeUser, +} from "../../../utils/templateSafeObjects"; export const ChangePermsAction = automodAction({ configType: t.type({ @@ -18,20 +24,39 @@ export const ChangePermsAction = automodAction({ }, async apply({ pluginData, contexts, actionConfig, ruleName }) { - const role = await pluginData.guild.roles.fetch(actionConfig.target); + const user = contexts.find((c) => c.user)?.user; + const message = contexts.find((c) => c.message)?.message; + + const renderTarget = async (str: string) => + renderTemplate( + str, + new TemplateSafeValueContainer({ + user: user ? userToTemplateSafeUser(user) : null, + guild: guildToTemplateSafeGuild(pluginData.guild), + }), + ); + const renderChannel = async (str: string) => + renderTemplate( + str, + new TemplateSafeValueContainer({ + msg: message ? savedMessageToTemplateSafeSavedMessage(message) : null, + }), + ); + const target = await renderTarget(actionConfig.target); + const channelId = actionConfig.channel ? await renderChannel(actionConfig.channel) : null; + const role = await pluginData.guild.roles.fetch(target); if (!role) { - const member = await pluginData.guild.members.fetch(actionConfig.target); + const member = await pluginData.guild.members.fetch(target); if (!member) return; } - if (actionConfig.channel && isValidSnowflake(actionConfig.channel)) { - const channel = await pluginData.guild.channels.fetch(actionConfig.channel); + if (channelId && isValidSnowflake(channelId)) { + const channel = await pluginData.guild.channels.fetch(channelId); if (!channel) return; - const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === actionConfig.target); + const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === target); const allow = new Permissions(overwrite ? overwrite.allow : "0").serialize(); const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize(); const newPerms: Partial> = {}; - let hasPerms = false; for (const key in allow) { if (typeof actionConfig.perms[key] !== "undefined") { @@ -40,17 +65,24 @@ export const ChangePermsAction = automodAction({ } if (allow[key]) { newPerms[key] = true; - hasPerms = true; } else if (deny[key]) { newPerms[key] = false; + } + } + + // takes more code lines but looks cleaner imo + let hasPerms = false; + for (const key in newPerms) { + if (typeof newPerms[key] === "boolean") { hasPerms = true; + break; } } if (overwrite && !hasPerms) { - await channel.permissionOverwrites.delete(actionConfig.target).catch(noop); + await channel.permissionOverwrites.delete(target).catch(noop); return; } - await channel.permissionOverwrites.create(actionConfig.target, newPerms).catch(noop); + await channel.permissionOverwrites.create(target, newPerms).catch(noop); return; }