3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-25 18:25:03 +00:00
This commit is contained in:
metal 2021-11-29 16:35:37 +00:00 committed by GitHub
parent ff160be1fa
commit c541063ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View file

@ -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<Record<PermissionString, boolean | null>> = {};
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[] = <any>Object.keys(perms).filter((key) => perms[key]);
await role.setPermissions(new Permissions(permsArray)).catch(noop);
},
});