summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-04-06 03:56:19 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-04-06 03:56:19 +0200
commite5f408be0889562f6fa36ac1bb1730e50bf1951f (patch)
tree2e083dd44f11ea97b81fc3864657642e899547f5 /src/util
parent:sparkles: save private data in user.user_data (diff)
downloadserver-e5f408be0889562f6fa36ac1bb1730e50bf1951f.tar.xz
:bug: fix Database
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Database.ts58
-rw-r--r--src/util/Permissions.ts4
2 files changed, 34 insertions, 28 deletions
diff --git a/src/util/Database.ts b/src/util/Database.ts
index 2304378c..f5269675 100644
--- a/src/util/Database.ts
+++ b/src/util/Database.ts
@@ -29,7 +29,7 @@ export class MongooseCache extends EventEmitter {
 		super();
 	}
 
-	async init() {
+	init = async () => {
 		this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" });
 
 		this.stream.on("change", this.change);
@@ -40,9 +40,9 @@ export class MongooseCache extends EventEmitter {
 			const arr = await this.collection.aggregate(this.pipeline).toArray();
 			this.data = arr.length ? arr[0] : arr;
 		}
-	}
+	};
 
-	convertResult(obj: any) {
+	convertResult = (obj: any) => {
 		if (obj instanceof Long) return BigInt(obj.toString());
 		if (typeof obj === "object") {
 			Object.keys(obj).forEach((key) => {
@@ -51,40 +51,44 @@ export class MongooseCache extends EventEmitter {
 		}
 
 		return obj;
-	}
+	};
 
 	change = (doc: ChangeEvent) => {
-		// @ts-ignore
-		if (doc.fullDocument) {
+		try {
 			// @ts-ignore
-			if (!this.opts.onlyEvents) this.data = doc.fullDocument;
-		}
+			if (doc.fullDocument) {
+				// @ts-ignore
+				if (!this.opts.onlyEvents) this.data = doc.fullDocument;
+			}
 
-		switch (doc.operationType) {
-			case "dropDatabase":
-				return this.destroy();
-			case "drop":
-				return this.destroy();
-			case "delete":
-				return this.emit("delete", doc.documentKey._id.toHexString());
-			case "insert":
-				return this.emit("insert", doc.fullDocument);
-			case "update":
-			case "replace":
-				return this.emit("change", doc.fullDocument);
-			case "invalidate":
-				return this.destroy();
-			default:
-				return;
+			switch (doc.operationType) {
+				case "dropDatabase":
+					return this.destroy();
+				case "drop":
+					return this.destroy();
+				case "delete":
+					return this.emit("delete", doc.documentKey._id.toHexString());
+				case "insert":
+					return this.emit("insert", doc.fullDocument);
+				case "update":
+				case "replace":
+					return this.emit("change", doc.fullDocument);
+				case "invalidate":
+					return this.destroy();
+				default:
+					return;
+			}
+		} catch (error) {
+			this.emit("error", error);
 		}
 	};
 
-	destroy() {
-		this.stream.off("change", this.change);
+	destroy = () => {
+		this.stream?.off("change", this.change);
 		this.emit("close");
 
 		if (this.stream.isClosed()) return;
 
 		return this.stream.close();
-	}
+	};
 }
diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts
index ae21e138..809111bf 100644
--- a/src/util/Permissions.ts
+++ b/src/util/Permissions.ts
@@ -158,7 +158,9 @@ export async function getPermission(
 	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();
+	var roles = await RoleModel.find({ guild_id, id: { $in: member.roles } })
+		.lean()
+		.exec();
 	if (channel_id) {
 		channel = await ChannelModel.findOne({ id: channel_id }, "permission_overwrites").exec();
 	}