3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-25 10:25:01 +00:00

cleanup & add template rendering

This commit is contained in:
metal 2021-12-10 14:05:13 +00:00 committed by GitHub
parent fb91250208
commit f84461a20d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<Record<PermissionString, boolean | null>> = {};
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;
}