From c541063ef31ffa5782eaf14eeedd6842c0d9d805 Mon Sep 17 00:00:00 2001 From: metal Date: Mon, 29 Nov 2021 16:35:37 +0000 Subject: [PATCH 1/9] initial --- .../Automod/actions/availableActions.ts | 3 + .../plugins/Automod/actions/changePerms.ts | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 backend/src/plugins/Automod/actions/changePerms.ts diff --git a/backend/src/plugins/Automod/actions/availableActions.ts b/backend/src/plugins/Automod/actions/availableActions.ts index c37f3a39..3f253bc0 100644 --- a/backend/src/plugins/Automod/actions/availableActions.ts +++ b/backend/src/plugins/Automod/actions/availableActions.ts @@ -6,6 +6,7 @@ import { AlertAction } from "./alert"; import { ArchiveThreadAction } from "./archiveThread"; import { BanAction } from "./ban"; import { ChangeNicknameAction } from "./changeNickname"; +import { ChangePermsAction } from "./changePerms"; import { CleanAction } from "./clean"; import { KickAction } from "./kick"; import { LogAction } from "./log"; @@ -36,6 +37,7 @@ export const availableActions: Record> = { set_slowmode: SetSlowmodeAction, start_thread: StartThreadAction, archive_thread: ArchiveThreadAction, + change_perms: ChangePermsAction, }; export const AvailableActions = t.type({ @@ -56,4 +58,5 @@ export const AvailableActions = t.type({ set_slowmode: SetSlowmodeAction.configType, start_thread: StartThreadAction.configType, archive_thread: ArchiveThreadAction.configType, + change_perms: ChangePermsAction.configType, }); diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts new file mode 100644 index 00000000..53d54e04 --- /dev/null +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -0,0 +1,56 @@ +import { Permissions, PermissionString } from "discord.js"; +import * as t from "io-ts"; +import { automodAction } from "../helpers"; +import { tNullable, isValidSnowflake } from "../../../utils"; +import { noop } from "knub/dist/utils"; + +export const ChangePermsAction = automodAction({ + configType: t.type({ + target: t.string, + channel: tNullable(t.string), + perms: t.record(t.keyof(Permissions.FLAGS), t.union([t.boolean, t.null])), + }), + defaultConfig: { + channel: "", + }, + + async apply({ pluginData, contexts, actionConfig, ruleName }) { + const role = await pluginData.guild.roles.fetch(actionConfig.target); + if (!role) { + const member = await pluginData.guild.members.fetch(actionConfig.target); + if (!member) return; + } + + if (actionConfig.channel && isValidSnowflake(actionConfig.channel)) { + const channel = await pluginData.guild.channels.fetch(actionConfig.channel); + if (!channel) return; + const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === actionConfig.target); + const allow = new Permissions(overwrite ? overwrite.allow : "0").serialize(); + const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize(); + const newPerms: Partial> = {}; + + for (const key in allow) { + if (typeof actionConfig.perms[key] !== "undefined") { + newPerms[key] = actionConfig.perms[key]; + continue; + } + if (allow[key]) { + newPerms[key] = true; + } else if (deny[key]) { + newPerms[key] = false; + } + } + await channel.permissionOverwrites.create(actionConfig.target, newPerms).catch(noop); + return; + } + + if (!role) return; + + const perms = new Permissions(role.permissions).serialize(); + for (const key in actionConfig.perms) { + perms[key] = actionConfig.perms[key]; + } + const permsArray: PermissionString[] = Object.keys(perms).filter((key) => perms[key]); + await role.setPermissions(new Permissions(permsArray)).catch(noop); + }, +}); From 42c56db858068cad0b9231d55495b101de39916f Mon Sep 17 00:00:00 2001 From: metal Date: Thu, 9 Dec 2021 16:20:10 +0000 Subject: [PATCH 2/9] fix typings UwU --- backend/src/plugins/Automod/actions/changePerms.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index 53d54e04..5d8afdb6 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -1,17 +1,20 @@ import { Permissions, PermissionString } from "discord.js"; import * as t from "io-ts"; import { automodAction } from "../helpers"; -import { tNullable, isValidSnowflake } from "../../../utils"; +import { tNullable, isValidSnowflake, tPartialDictionary } from "../../../utils"; import { noop } from "knub/dist/utils"; export const ChangePermsAction = automodAction({ configType: t.type({ target: t.string, channel: tNullable(t.string), - perms: t.record(t.keyof(Permissions.FLAGS), t.union([t.boolean, t.null])), + perms: tPartialDictionary(t.keyof(Permissions.FLAGS), tNullable(t.boolean)), }), defaultConfig: { channel: "", + perms: { + SEND_MESSAGES: true, + }, }, async apply({ pluginData, contexts, actionConfig, ruleName }) { @@ -50,7 +53,7 @@ export const ChangePermsAction = automodAction({ for (const key in actionConfig.perms) { perms[key] = actionConfig.perms[key]; } - const permsArray: PermissionString[] = Object.keys(perms).filter((key) => perms[key]); + const permsArray = Object.keys(perms).filter((key) => perms[key]); await role.setPermissions(new Permissions(permsArray)).catch(noop); }, }); From fb91250208c50221ee6df33ac9e31c8d677bf92d Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 13:46:17 +0000 Subject: [PATCH 3/9] check no perms for overrides --- backend/src/plugins/Automod/actions/changePerms.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index 5d8afdb6..fd27094f 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -31,6 +31,7 @@ export const ChangePermsAction = automodAction({ 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") { @@ -39,10 +40,16 @@ export const ChangePermsAction = automodAction({ } if (allow[key]) { newPerms[key] = true; + hasPerms = true; } else if (deny[key]) { newPerms[key] = false; + hasPerms = true; } } + if (overwrite && !hasPerms) { + await channel.permissionOverwrites.delete(actionConfig.target).catch(noop); + return; + } await channel.permissionOverwrites.create(actionConfig.target, newPerms).catch(noop); return; } From f84461a20d51ed49641374ab27a7fa922fa88233 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 14:05:13 +0000 Subject: [PATCH 4/9] 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; } From d5d2ea314e820b646a946a18cd8c9f0bd8cca202 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 17:08:13 +0000 Subject: [PATCH 5/9] remove defaults Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/changePerms.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index 34f16105..a516a6cf 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -16,12 +16,7 @@ export const ChangePermsAction = automodAction({ channel: tNullable(t.string), perms: tPartialDictionary(t.keyof(Permissions.FLAGS), tNullable(t.boolean)), }), - defaultConfig: { - channel: "", - perms: { - SEND_MESSAGES: true, - }, - }, + defaultConfig: {}, async apply({ pluginData, contexts, actionConfig, ruleName }) { const user = contexts.find((c) => c.user)?.user; From 236ba493fe17fd969c62b77d390d48bcab499ef9 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 17:08:29 +0000 Subject: [PATCH 6/9] Update backend/src/plugins/Automod/actions/changePerms.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/changePerms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index a516a6cf..3b1dc6e7 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -41,7 +41,7 @@ export const ChangePermsAction = automodAction({ 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(target); + const member = await pluginData.guild.members.fetch(target).catch(noop); if (!member) return; } From ce03b75331a6425757b4e7272a77a5f20daf8f56 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 17:10:32 +0000 Subject: [PATCH 7/9] Update backend/src/plugins/Automod/actions/changePerms.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/changePerms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index 3b1dc6e7..747a236b 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -49,7 +49,7 @@ export const ChangePermsAction = automodAction({ const channel = await pluginData.guild.channels.fetch(channelId); if (!channel) return; const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === target); - const allow = new Permissions(overwrite ? overwrite.allow : "0").serialize(); + const allow = new Permissions(overwrite?.allow ?? 0n).serialize(); const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize(); const newPerms: Partial> = {}; From 432e237a7be64aed32a177db1618670c111b6942 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 17:10:40 +0000 Subject: [PATCH 8/9] Update backend/src/plugins/Automod/actions/changePerms.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/changePerms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index 747a236b..b4087eb8 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -50,7 +50,7 @@ export const ChangePermsAction = automodAction({ if (!channel) return; const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === target); const allow = new Permissions(overwrite?.allow ?? 0n).serialize(); - const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize(); + const deny = new Permissions(overwrite?.deny ?? 0n).serialize(); const newPerms: Partial> = {}; for (const key in allow) { From c38ef6f0a01abb031d97d92fecb92cef2a425465 Mon Sep 17 00:00:00 2001 From: metal Date: Fri, 10 Dec 2021 17:14:20 +0000 Subject: [PATCH 9/9] .resolve instead of .fetch Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/changePerms.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/actions/changePerms.ts b/backend/src/plugins/Automod/actions/changePerms.ts index b4087eb8..039fd043 100644 --- a/backend/src/plugins/Automod/actions/changePerms.ts +++ b/backend/src/plugins/Automod/actions/changePerms.ts @@ -39,14 +39,14 @@ export const ChangePermsAction = automodAction({ ); const target = await renderTarget(actionConfig.target); const channelId = actionConfig.channel ? await renderChannel(actionConfig.channel) : null; - const role = await pluginData.guild.roles.fetch(target); + const role = pluginData.guild.roles.resolve(target); if (!role) { const member = await pluginData.guild.members.fetch(target).catch(noop); if (!member) return; } if (channelId && isValidSnowflake(channelId)) { - const channel = await pluginData.guild.channels.fetch(channelId); + const channel = pluginData.guild.channels.resolve(channelId); if (!channel) return; const overwrite = channel.permissionOverwrites.cache.find((pw) => pw.id === target); const allow = new Permissions(overwrite?.allow ?? 0n).serialize();