create getPipeline function + clean up

This commit is contained in:
Flam3rboy 2021-04-08 18:52:57 +02:00
parent 9d9a4c7e72
commit f268e9c42f

View File

@ -2,6 +2,7 @@ import { db, Event, MongooseCache, UserModel, getPermission, Permissions } from
import { OPCODES } from "../util/Constants"; import { OPCODES } from "../util/Constants";
import { Send } from "../util/Send"; import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import "missing-native-js-functions";
// TODO: close connection on Invalidated Token // TODO: close connection on Invalidated Token
// TODO: check intent // TODO: check intent
@ -12,194 +13,188 @@ import WebSocket from "../util/WebSocket";
// https://discord.com/developers/docs/topics/gateway#sharding // https://discord.com/developers/docs/topics/gateway#sharding
export interface DispatchOpts { export interface DispatchOpts {
eventStream: any; eventStream: MongooseCache;
user_guilds: Array<string>; guilds: Array<string>;
shard_count?: bigint; }
shard_id?: bigint;
function getPipeline(this: WebSocket, guilds: string[]) {
if (this.shard_count) {
guilds = guilds.filter((x) => (BigInt(x) >> 22n) % this.shard_count === this.shard_id);
}
return [
{
$match: {
$or: [{ "fullDocument.guild_id": { $in: guilds } }, { "fullDocument.user_id": this.user_id }],
},
},
];
} }
export async function setupListener(this: WebSocket) { export async function setupListener(this: WebSocket) {
const user = await UserModel.findOne({ id: this.user_id }).lean().exec(); const user = await UserModel.findOne({ id: this.user_id }).lean().exec();
var user_guilds = user.guilds; var guilds = user.guilds;
const shard_count = 10n;
const shard_id = 0n;
if (shard_count) { const eventStream = new MongooseCache(db.collection("events"), getPipeline.call(this, guilds), {
user_guilds = user.guilds.filter((x) => (BigInt(x) >> 22n) % shard_count === shard_id); onlyEvents: true,
} });
const eventStream = new MongooseCache( await eventStream.init();
db.collection("events"), eventStream.on("insert", (document: Event) => dispatch.call(this, document, { eventStream, guilds }));
[
{
$match: {
$or: [{ "fullDocument.guild_id": { $in: user_guilds } }, { "fullDocument.user_id": user.id }]
}
}
],
{
onlyEvents: true
}
);
await eventStream.init(); this.once("close", () => eventStream.destroy());
eventStream.on("insert", (document) => dispatch.call(this, { eventStream, user_guilds, shard_count, shard_id }, document));
this.once("close", () => eventStream.destroy());
} }
export async function dispatch(this: WebSocket, { eventStream, user_guilds, shard_count, shard_id }: DispatchOpts, document: Event) { export async function dispatch(this: WebSocket, document: Event, { eventStream, guilds }: DispatchOpts) {
var permission = new Permissions("ADMINISTRATOR"); // default permission for dms var permission = new Permissions("ADMINISTRATOR"); // default permission for dms
console.log("event", document); console.log("event", document);
if (document.guild_id) { if (document.guild_id) {
if (!this.intents.has("GUILDS")) return; if (!this.intents.has("GUILDS")) return;
const channel_id = document.channel_id || document.data?.channel_id; const channel_id = document.channel_id || document.data?.channel_id;
permission = await getPermission(this.user_id, document.guild_id, channel_id); permission = await getPermission(this.user_id, document.guild_id, channel_id);
} }
if (document.event === "GUILD_CREATE") { if (document.event === "GUILD_CREATE") {
user_guilds.push(document.guild_id); guilds.push(document.guild_id);
eventStream.changeStream(getPipeline.call(this, guilds));
} else if (document.event === "GUILD_DELETE") {
guilds.remove(document.guild);
eventStream.changeStream(getPipeline.call(this, guilds));
}
if (shard_count) { // check intents: https://discord.com/developers/docs/topics/gateway#gateway-intents
user_guilds = user_guilds.filter((x) => (BigInt(x) >> 22n) % shard_count === shard_id); switch (document.event) {
} case "GUILD_CREATE":
case "GUILD_DELETE":
case "GUILD_UPDATE":
case "GUILD_ROLE_CREATE":
case "GUILD_ROLE_UPDATE":
case "GUILD_ROLE_DELETE":
case "CHANNEL_CREATE":
case "CHANNEL_DELETE":
case "CHANNEL_UPDATE":
// gets sent if GUILDS intent is set (already checked in if document.guild_id)
break;
case "GUILD_INTEGRATIONS_UPDATE":
if (!this.intents.has("GUILD_INTEGRATIONS")) return;
break;
case "WEBHOOKS_UPDATE":
if (!this.intents.has("GUILD_WEBHOOKS")) return;
break;
case "GUILD_EMOJI_UPDATE":
if (!this.intents.has("GUILD_EMOJIS")) return;
break;
// only send them, if the user subscribed for this part of the member list, or is a bot
case "GUILD_MEMBER_ADD":
case "GUILD_MEMBER_REMOVE":
case "GUILD_MEMBER_UPDATE":
if (!this.intents.has("GUILD_MEMBERS")) return;
break;
case "VOICE_STATE_UPDATE":
if (!this.intents.has("GUILD_VOICE_STATES")) return;
break;
case "GUILD_BAN_ADD":
case "GUILD_BAN_REMOVE":
if (!this.intents.has("GUILD_BANS")) return;
break;
case "INVITE_CREATE":
case "INVITE_DELETE":
if (!this.intents.has("GUILD_INVITES")) return;
case "PRESENCE_UPDATE":
if (!this.intents.has("GUILD_PRESENCES")) return;
break;
case "MESSAGE_CREATE":
case "MESSAGE_DELETE":
case "MESSAGE_DELETE_BULK":
case "MESSAGE_UPDATE":
case "CHANNEL_PINS_UPDATE":
if (!this.intents.has("GUILD_MESSAGES") && document.guild_id) return;
if (!this.intents.has("DIRECT_MESSAGES") && !document.guild_id) return;
break;
case "MESSAGE_REACTION_ADD":
case "MESSAGE_REACTION_REMOVE":
case "MESSAGE_REACTION_REMOVE_ALL":
case "MESSAGE_REACTION_REMOVE_EMOJI":
if (!this.intents.has("GUILD_MESSAGE_REACTIONS") && document.guild_id) return;
if (!this.intents.has("DIRECT_MESSAGE_REACTIONS") && !document.guild_id) return;
break;
eventStream.changeStream([{ $match: { $or: [{ "fullDocument.guild_id": { $in: user_guilds } }, { "fullDocument.user_id": document.user_id }] } }]); case "TYPING_START":
} if (!this.intents.has("GUILD_MESSAGE_TYPING") && document.guild_id) return;
if (!this.intents.has("DIRECT_MESSAGE_TYPING") && !document.guild_id) return;
break;
case "READY": // will be sent by the gateway
case "USER_UPDATE":
case "APPLICATION_COMMAND_CREATE":
case "APPLICATION_COMMAND_DELETE":
case "APPLICATION_COMMAND_UPDATE":
default:
// Any events not defined in an intent are considered "passthrough" and will always be sent to you.
break;
}
// check intents: https://discord.com/developers/docs/topics/gateway#gateway-intents // check permissions
switch (document.event) { switch (document.event) {
case "GUILD_CREATE": case "GUILD_INTEGRATIONS_UPDATE":
case "GUILD_DELETE": if (!permission.has("MANAGE_GUILD")) return;
case "GUILD_UPDATE": break;
case "GUILD_ROLE_CREATE": case "WEBHOOKS_UPDATE":
case "GUILD_ROLE_UPDATE": if (!permission.has("MANAGE_WEBHOOKS")) return;
case "GUILD_ROLE_DELETE": break;
case "CHANNEL_CREATE": case "GUILD_MEMBER_ADD":
case "CHANNEL_DELETE": case "GUILD_MEMBER_REMOVE":
case "CHANNEL_UPDATE": case "GUILD_MEMBER_UPDATE":
// gets sent if GUILDS intent is set (already checked in if document.guild_id) // only send them, if the user subscribed for this part of the member list, or is a bot
break; break;
case "GUILD_INTEGRATIONS_UPDATE": case "GUILD_BAN_ADD":
if (!this.intents.has("GUILD_INTEGRATIONS")) return; case "GUILD_BAN_REMOVE":
break; if (!permission.has("BAN_MEMBERS")) break;
case "WEBHOOKS_UPDATE": break;
if (!this.intents.has("GUILD_WEBHOOKS")) return; case "INVITE_CREATE":
break; case "INVITE_DELETE":
case "GUILD_EMOJI_UPDATE": if (!permission.has("MANAGE_GUILD")) break;
if (!this.intents.has("GUILD_EMOJIS")) return; case "PRESENCE_UPDATE":
break; break;
// only send them, if the user subscribed for this part of the member list, or is a bot case "VOICE_STATE_UPDATE":
case "GUILD_MEMBER_ADD": case "MESSAGE_CREATE":
case "GUILD_MEMBER_REMOVE": case "MESSAGE_DELETE":
case "GUILD_MEMBER_UPDATE": case "MESSAGE_DELETE_BULK":
if (!this.intents.has("GUILD_MEMBERS")) return; case "MESSAGE_UPDATE":
break; case "CHANNEL_PINS_UPDATE":
case "VOICE_STATE_UPDATE": case "MESSAGE_REACTION_ADD":
if (!this.intents.has("GUILD_VOICE_STATES")) return; case "MESSAGE_REACTION_REMOVE":
break; case "MESSAGE_REACTION_REMOVE_ALL":
case "GUILD_BAN_ADD": case "MESSAGE_REACTION_REMOVE_EMOJI":
case "GUILD_BAN_REMOVE": case "TYPING_START":
if (!this.intents.has("GUILD_BANS")) return; // only gets send if the user is alowed to view the current channel
break; if (!permission.has("VIEW_CHANNEL")) return;
case "INVITE_CREATE": break;
case "INVITE_DELETE": case "GUILD_CREATE":
if (!this.intents.has("GUILD_INVITES")) return; case "GUILD_DELETE":
case "PRESENCE_UPDATE": case "GUILD_UPDATE":
if (!this.intents.has("GUILD_PRESENCES")) return; case "GUILD_ROLE_CREATE":
break; case "GUILD_ROLE_UPDATE":
case "MESSAGE_CREATE": case "GUILD_ROLE_DELETE":
case "MESSAGE_DELETE": case "CHANNEL_CREATE":
case "MESSAGE_DELETE_BULK": case "CHANNEL_DELETE":
case "MESSAGE_UPDATE": case "CHANNEL_UPDATE":
case "CHANNEL_PINS_UPDATE": case "GUILD_EMOJI_UPDATE":
if (!this.intents.has("GUILD_MESSAGES") && document.guild_id) return; case "READY": // will be sent by the gateway
if (!this.intents.has("DIRECT_MESSAGES") && !document.guild_id) return; case "USER_UPDATE":
break; case "APPLICATION_COMMAND_CREATE":
case "MESSAGE_REACTION_ADD": case "APPLICATION_COMMAND_DELETE":
case "MESSAGE_REACTION_REMOVE": case "APPLICATION_COMMAND_UPDATE":
case "MESSAGE_REACTION_REMOVE_ALL": default:
case "MESSAGE_REACTION_REMOVE_EMOJI": // always gets sent
if (!this.intents.has("GUILD_MESSAGE_REACTIONS") && document.guild_id) return; // Any events not defined in an intent are considered "passthrough" and will always be sent
if (!this.intents.has("DIRECT_MESSAGE_REACTIONS") && !document.guild_id) return; break;
break; }
case "TYPING_START": return Send(this, {
if (!this.intents.has("GUILD_MESSAGE_TYPING") && document.guild_id) return; op: OPCODES.Dispatch,
if (!this.intents.has("DIRECT_MESSAGE_TYPING") && !document.guild_id) return; t: document.event,
break; d: document.data,
case "READY": // will be sent by the gateway s: this.sequence++,
case "USER_UPDATE": });
case "APPLICATION_COMMAND_CREATE":
case "APPLICATION_COMMAND_DELETE":
case "APPLICATION_COMMAND_UPDATE":
default:
// Any events not defined in an intent are considered "passthrough" and will always be sent to you.
break;
}
// check permissions
switch (document.event) {
case "GUILD_INTEGRATIONS_UPDATE":
if (!permission.has("MANAGE_GUILD")) return;
break;
case "WEBHOOKS_UPDATE":
if (!permission.has("MANAGE_WEBHOOKS")) return;
break;
case "GUILD_MEMBER_ADD":
case "GUILD_MEMBER_REMOVE":
case "GUILD_MEMBER_UPDATE":
// only send them, if the user subscribed for this part of the member list, or is a bot
break;
case "GUILD_BAN_ADD":
case "GUILD_BAN_REMOVE":
if (!permission.has("BAN_MEMBERS")) break;
break;
case "INVITE_CREATE":
case "INVITE_DELETE":
if (!permission.has("MANAGE_GUILD")) break;
case "PRESENCE_UPDATE":
break;
case "VOICE_STATE_UPDATE":
case "MESSAGE_CREATE":
case "MESSAGE_DELETE":
case "MESSAGE_DELETE_BULK":
case "MESSAGE_UPDATE":
case "CHANNEL_PINS_UPDATE":
case "MESSAGE_REACTION_ADD":
case "MESSAGE_REACTION_REMOVE":
case "MESSAGE_REACTION_REMOVE_ALL":
case "MESSAGE_REACTION_REMOVE_EMOJI":
case "TYPING_START":
// only gets send if the user is alowed to view the current channel
if (!permission.has("VIEW_CHANNEL")) return;
break;
case "GUILD_CREATE":
case "GUILD_DELETE":
case "GUILD_UPDATE":
case "GUILD_ROLE_CREATE":
case "GUILD_ROLE_UPDATE":
case "GUILD_ROLE_DELETE":
case "CHANNEL_CREATE":
case "CHANNEL_DELETE":
case "CHANNEL_UPDATE":
case "GUILD_EMOJI_UPDATE":
case "READY": // will be sent by the gateway
case "USER_UPDATE":
case "APPLICATION_COMMAND_CREATE":
case "APPLICATION_COMMAND_DELETE":
case "APPLICATION_COMMAND_UPDATE":
default:
// always gets sent
// Any events not defined in an intent are considered "passthrough" and will always be sent
break;
}
return Send(this, {
op: OPCODES.Dispatch,
t: document.event,
d: document.data,
s: this.sequence++
});
} }