From 774fcc9373e4dd76e47d9361ae5371978658dbd5 Mon Sep 17 00:00:00 2001 From: notsapinho <52896767+notsapinho@users.noreply.github.com> Date: Thu, 8 Apr 2021 11:59:00 -0300 Subject: [PATCH] Update listener.ts --- src/listener/listener.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/listener/listener.ts b/src/listener/listener.ts index 85dd33fc..50ca8e65 100644 --- a/src/listener/listener.ts +++ b/src/listener/listener.ts @@ -36,18 +36,7 @@ export async function setupListener(this: WebSocket) { ); await eventStream.init(); - eventStream.on("insert", async (document) => { - dispatch.call(this, document); - - const newUser = await UserModel.findOne({ id: user.id }).lean().exec(); - var newGuilds = user.guilds; - - if (shard_count) { - newGuilds = user.guilds.filter((x) => (BigInt(x) >> 22n) % shard_count === shard_id); - } - - eventStream.changeStream([{ $match: { $or: [{ "fullDocument.guild_id": { $in: newGuilds } }, { "fullDocument.user_id": newUser.id }] } }]); - }); + eventStream.on("insert", dispatch.bind(this)); this.once("close", () => eventStream.destroy()); } @@ -62,6 +51,17 @@ export async function dispatch(this: WebSocket, document: Event) { permission = await getPermission(this.user_id, document.guild_id, channel_id); } + if(document.event === "GUILD_CREATE") { + const newUser = await UserModel.findOne({ id: this.user_id }).lean().exec(); + var newGuilds = newUser.guilds; + + if (shard_count) { + newGuilds = newUser.guilds.filter((x) => (BigInt(x) >> 22n) % shard_count === shard_id); + } + + eventStream.changeStream([{ $match: { $or: [{ "fullDocument.guild_id": { $in: newGuilds } }, { "fullDocument.user_id": newUser.id }] } }]); + } + // check intents: https://discord.com/developers/docs/topics/gateway#gateway-intents switch (document.event) { case "GUILD_CREATE":