diff --git a/package-lock.json b/package-lock.json index ce1ccfa3..1c950900 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "discord-server-util": "github:discord-open-source/discord-server-util", "erlpack": "^0.1.3", "jsonwebtoken": "^8.5.1", - "lambert-db": "^1.1.2", + "lambert-db": "^1.1.5", "lambert-server": "^1.1.0", "missing-native-js-functions": "^1.2.2", "node-fetch": "^2.6.1", @@ -364,11 +364,11 @@ }, "node_modules/discord-server-util": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#84d714324ad65de94cfb3076daa8f6622a54e438", + "resolved": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#862200a80d7db52c04abb2300cbb480e2f835dfb", "license": "ISC", "dependencies": { "jsonwebtoken": "^8.5.1", - "lambert-db": "^1.1.4", + "lambert-db": "^1.1.5", "missing-native-js-functions": "^1.2.2" } }, @@ -760,9 +760,9 @@ "integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ==" }, "node_modules/lambert-db": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.4.tgz", - "integrity": "sha512-rRAZuGYiDGZTq8mgx4kqpvE1ESH6WUZdp+lvQJhAs/sJ2hAPIZg4IbsJh2RR3RiUt6E950WOzTPBgN786D0tYA==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.5.tgz", + "integrity": "sha512-07n2YzY0D9PiJySUR9zpe9/MFIzdvJ/pRWUF4GyvgqoUw3Z0eIUYjQ+Yd4o5ZANaZAwgCiTAwGTBtGmGXycOsw==", "dependencies": { "missing-native-js-functions": "^1.1.8", "mongodb": "^3.6.2", @@ -2050,11 +2050,11 @@ "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, "discord-server-util": { - "version": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#84d714324ad65de94cfb3076daa8f6622a54e438", + "version": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#862200a80d7db52c04abb2300cbb480e2f835dfb", "from": "discord-server-util@github:discord-open-source/discord-server-util", "requires": { "jsonwebtoken": "^8.5.1", - "lambert-db": "^1.1.4", + "lambert-db": "^1.1.5", "missing-native-js-functions": "^1.2.2" } }, @@ -2367,9 +2367,9 @@ "integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ==" }, "lambert-db": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.4.tgz", - "integrity": "sha512-rRAZuGYiDGZTq8mgx4kqpvE1ESH6WUZdp+lvQJhAs/sJ2hAPIZg4IbsJh2RR3RiUt6E950WOzTPBgN786D0tYA==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.5.tgz", + "integrity": "sha512-07n2YzY0D9PiJySUR9zpe9/MFIzdvJ/pRWUF4GyvgqoUw3Z0eIUYjQ+Yd4o5ZANaZAwgCiTAwGTBtGmGXycOsw==", "requires": { "missing-native-js-functions": "^1.1.8", "mongodb": "^3.6.2", diff --git a/package.json b/package.json index be636285..96fa23b9 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "discord-server-util": "github:discord-open-source/discord-server-util", "erlpack": "^0.1.3", "jsonwebtoken": "^8.5.1", - "lambert-db": "^1.1.2", + "lambert-db": "^1.1.5", "lambert-server": "^1.1.0", "missing-native-js-functions": "^1.2.2", "node-fetch": "^2.6.1", diff --git a/src/events/Connection.ts b/src/events/Connection.ts index 815d84cf..f0c433f7 100644 --- a/src/events/Connection.ts +++ b/src/events/Connection.ts @@ -5,11 +5,12 @@ import { Message } from "./Message"; import { setHeartbeat } from "../util/setHeartbeat"; import { Send } from "../util/Send"; import { CLOSECODES, OPCODES } from "../util/Constants"; +import { setupListener } from "../listener/listener"; // TODO: check rate limit // TODO: specify rate limit in config -export function Connection(this: Server, socket: WebSocket, request: IncomingMessage) { +export async function Connection(this: Server, socket: WebSocket, request: IncomingMessage) { try { socket.on("close", Close); socket.on("message", Message); @@ -29,13 +30,15 @@ export function Connection(this: Server, socket: WebSocket, request: IncomingMes setHeartbeat(socket); - Send(socket, { + await Send(socket, { op: OPCODES.Hello, d: { heartbeat_interval: 1000 * 30, }, }); + await setupListener.call(socket); + socket.readyTimeout = setTimeout(() => { return socket.close(CLOSECODES.Session_timed_out); }, 1000 * 30); diff --git a/src/listener/listener.ts b/src/listener/listener.ts new file mode 100644 index 00000000..12abaf86 --- /dev/null +++ b/src/listener/listener.ts @@ -0,0 +1,24 @@ +import { db, User } from "discord-server-util"; +import { ProviderCache } from "lambert-db"; +import { MongodbProviderCache } from "lambert-db/dist/Mongodb"; +import WebSocket from "../util/WebSocket"; + +export async function setupListener(this: WebSocket) { + // TODO: shard guilds (only for bots) + + const user: User = await db.data.users({ id: this.userid }).get(); + + // * MongoDB specific $in query to get all guilds of the user + const guildCache: MongodbProviderCache = await db.data + .guilds({ id: { $id: user.guilds } }) + .cache({ onlyEvents: true }) + .init(); + + guildCache.on("change", (data) => { + console.log(data); + }); + + this.once("close", async () => { + await guildCache.destroy(); + }); +}