summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-02-14 00:30:07 +0100
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-02-14 00:30:07 +0100
commit8e84d39c7f2531e81eed3a9d01bf4030b19a2c88 (patch)
treeeb89f194780f903388cf968e9073cab4aed75ccc /src/util
parent:art: Channel + Member + Message + Role in separate collection (diff)
downloadserver-8e84d39c7f2531e81eed3a9d01bf4030b19a2c88.tar.xz
:sparkles: getPermission util function
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Permissions.ts33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts
index 89e1d1d3..9cff8c90 100644
--- a/src/util/Permissions.ts
+++ b/src/util/Permissions.ts
@@ -1,8 +1,9 @@
 // https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
 // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
-
+import { MemberModel } from "../models/Member";
+import { ChannelDocument, ChannelModel } from "../models/Channel";
 import { ChannelPermissionOverwrite } from "../models/Channel";
-import { Role } from "../models/Role";
+import { Role, RoleModel } from "../models/Role";
 import { BitField } from "./BitField";
 
 export type PermissionResolvable = string | number | Permissions | PermissionResolvable[];
@@ -80,7 +81,7 @@ export class Permissions extends BitField {
 		user: { id: bigint; roles: bigint[] };
 		guild: { roles: Role[] };
 		channel?: {
-			overwrites: ChannelPermissionOverwrite[];
+			overwrites?: ChannelPermissionOverwrite[];
 		};
 	}) {
 		let roles = guild.roles.filter((x) => user.roles.includes(x.id));
@@ -98,3 +99,29 @@ export class Permissions extends BitField {
 		return permission;
 	}
 }
+
+export async function getPermission(user_id: bigint, guild_id: bigint, channel_id?: bigint) {
+	var member = await MemberModel.findOne({ guild_id, id: user_id }, "roles").exec();
+	if (!member) throw new Error("Member not found");
+
+	var roles = await RoleModel.find({ guild_id, id: { $in: member.roles } }).exec();
+	let channel: ChannelDocument | null = null;
+	if (channel_id) {
+		channel = await ChannelModel.findOne({ id: channel_id }, "permission_overwrites");
+	}
+
+	var permission = Permissions.finalPermission({
+		user: {
+			id: user_id,
+			roles: member.roles,
+		},
+		guild: {
+			roles: roles,
+		},
+		channel: {
+			overwrites: channel?.permission_overwrites,
+		},
+	});
+
+	return permission;
+}