From c541063ef31ffa5782eaf14eeedd6842c0d9d805 Mon Sep 17 00:00:00 2001 From: metal Date: Mon, 29 Nov 2021 16:35:37 +0000 Subject: [PATCH] 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); + }, +});