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:
parent
fb91250208
commit
f84461a20d
1 changed files with 41 additions and 9 deletions
|
@ -3,6 +3,12 @@ import * as t from "io-ts";
|
||||||
import { automodAction } from "../helpers";
|
import { automodAction } from "../helpers";
|
||||||
import { tNullable, isValidSnowflake, tPartialDictionary } from "../../../utils";
|
import { tNullable, isValidSnowflake, tPartialDictionary } from "../../../utils";
|
||||||
import { noop } from "knub/dist/utils";
|
import { noop } from "knub/dist/utils";
|
||||||
|
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
|
||||||
|
import {
|
||||||
|
guildToTemplateSafeGuild,
|
||||||
|
savedMessageToTemplateSafeSavedMessage,
|
||||||
|
userToTemplateSafeUser,
|
||||||
|
} from "../../../utils/templateSafeObjects";
|
||||||
|
|
||||||
export const ChangePermsAction = automodAction({
|
export const ChangePermsAction = automodAction({
|
||||||
configType: t.type({
|
configType: t.type({
|
||||||
|
@ -18,20 +24,39 @@ export const ChangePermsAction = automodAction({
|
||||||
},
|
},
|
||||||
|
|
||||||
async apply({ pluginData, contexts, actionConfig, ruleName }) {
|
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) {
|
if (!role) {
|
||||||
const member = await pluginData.guild.members.fetch(actionConfig.target);
|
const member = await pluginData.guild.members.fetch(target);
|
||||||
if (!member) return;
|
if (!member) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionConfig.channel && isValidSnowflake(actionConfig.channel)) {
|
if (channelId && isValidSnowflake(channelId)) {
|
||||||
const channel = await pluginData.guild.channels.fetch(actionConfig.channel);
|
const channel = await pluginData.guild.channels.fetch(channelId);
|
||||||
if (!channel) return;
|
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 allow = new Permissions(overwrite ? overwrite.allow : "0").serialize();
|
||||||
const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize();
|
const deny = new Permissions(overwrite ? overwrite.deny : "0").serialize();
|
||||||
const newPerms: Partial<Record<PermissionString, boolean | null>> = {};
|
const newPerms: Partial<Record<PermissionString, boolean | null>> = {};
|
||||||
let hasPerms = false;
|
|
||||||
|
|
||||||
for (const key in allow) {
|
for (const key in allow) {
|
||||||
if (typeof actionConfig.perms[key] !== "undefined") {
|
if (typeof actionConfig.perms[key] !== "undefined") {
|
||||||
|
@ -40,17 +65,24 @@ export const ChangePermsAction = automodAction({
|
||||||
}
|
}
|
||||||
if (allow[key]) {
|
if (allow[key]) {
|
||||||
newPerms[key] = true;
|
newPerms[key] = true;
|
||||||
hasPerms = true;
|
|
||||||
} else if (deny[key]) {
|
} else if (deny[key]) {
|
||||||
newPerms[key] = false;
|
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;
|
hasPerms = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (overwrite && !hasPerms) {
|
if (overwrite && !hasPerms) {
|
||||||
await channel.permissionOverwrites.delete(actionConfig.target).catch(noop);
|
await channel.permissionOverwrites.delete(target).catch(noop);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await channel.permissionOverwrites.create(actionConfig.target, newPerms).catch(noop);
|
await channel.permissionOverwrites.create(target, newPerms).catch(noop);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue