update import to fosscord-server-util
This commit is contained in:
parent
49b3426858
commit
38757c60e1
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -12,10 +12,10 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord-server-util": "github:discord-open-source/discord-server-util",
|
||||
"erlpack": "^0.1.3",
|
||||
"fosscord-server-util": "github:fosscord/fosscord-server-util",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"lambert-db": "^1.1.6",
|
||||
"lambert-db": "^1.1.8",
|
||||
"lambert-server": "^1.1.3",
|
||||
"missing-native-js-functions": "^1.2.3",
|
||||
"node-fetch": "^2.6.1",
|
||||
|
||||
BIN
src/.DS_Store
vendored
BIN
src/.DS_Store
vendored
Binary file not shown.
@ -1,6 +1,7 @@
|
||||
import { db } from "discord-server-util";
|
||||
import { db } from "fosscord-server-util";
|
||||
import { Server as WebSocketServer } from "ws";
|
||||
import { Connection } from "./events/Connection";
|
||||
import Config from "./util/Config";
|
||||
|
||||
export class Server {
|
||||
public ws: WebSocketServer;
|
||||
@ -9,8 +10,15 @@ export class Server {
|
||||
this.ws.on("connection", Connection);
|
||||
}
|
||||
|
||||
async setupSchema() {
|
||||
// TODO: adjust expireAfterSeconds -> lower
|
||||
await db.conn.db.collection("events").createIndex({ created_at: 1 }, { expireAfterSeconds: 60 });
|
||||
}
|
||||
|
||||
async listen(): Promise<void> {
|
||||
await db.init();
|
||||
await this.setupSchema();
|
||||
await Config.init();
|
||||
console.log("listening");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,24 +1,23 @@
|
||||
import { db, User } from "discord-server-util";
|
||||
import { ProviderCache } from "lambert-db";
|
||||
import { db, User, Event } from "fosscord-server-util";
|
||||
import { MongodbProviderCache } from "lambert-db/dist/Mongodb";
|
||||
import WebSocket from "../util/WebSocket";
|
||||
|
||||
export async function setupListener(this: WebSocket) {
|
||||
// TODO: shard guilds (only for bots)
|
||||
// TODO: bot sharding
|
||||
// TODO: close connection on Invalidated Token
|
||||
|
||||
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 } })
|
||||
const eventStream: MongodbProviderCache = await db.data
|
||||
.guilds({ $or: [{ guild_id: { $in: user.guilds } }, { user_id: this.userid }] })
|
||||
.cache({ onlyEvents: true })
|
||||
.init();
|
||||
|
||||
guildCache.on("change", (data) => {
|
||||
console.log(data);
|
||||
eventStream.on("insert", (document: Event) => {
|
||||
console.log("event", document);
|
||||
this.emit(document.event, document.data);
|
||||
});
|
||||
|
||||
this.once("close", async () => {
|
||||
await guildCache.destroy();
|
||||
});
|
||||
this.once("close", () => eventStream.destroy());
|
||||
}
|
||||
|
||||
@ -1,17 +1,20 @@
|
||||
import { CLOSECODES, Payload } from "../util/Constants";
|
||||
import Config from "../util/Config";
|
||||
import WebSocket from "../util/WebSocket";
|
||||
import { checkToken, IdentifySchema } from "discord-server-util";
|
||||
import { check } from "./instanceOf";
|
||||
import { checkToken, IdentifySchema } from "fosscord-server-util";
|
||||
import { setupListener } from "../listener/listener";
|
||||
import { instanceOf } from "lambert-server";
|
||||
|
||||
export async function onIdentify(this: WebSocket, data: Payload) {
|
||||
clearTimeout(this.readyTimeout);
|
||||
if (check.call(this, IdentifySchema, data.d)) return;
|
||||
|
||||
const identify: IdentifySchema = data.d;
|
||||
|
||||
try {
|
||||
var { id } = await checkToken(identify.token);
|
||||
clearTimeout(this.readyTimeout);
|
||||
instanceOf(IdentifySchema, data.d);
|
||||
|
||||
const identify: IdentifySchema = data.d;
|
||||
|
||||
var decoded = await checkToken(identify.token);
|
||||
this.userid = decoded.id;
|
||||
|
||||
await setupListener.call(this);
|
||||
} catch (error) {
|
||||
return this.close(CLOSECODES.Authentication_failed);
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Config } from "discord-server-util";
|
||||
import { Config } from "fosscord-server-util";
|
||||
|
||||
export default {
|
||||
init() {
|
||||
|
||||
Reference in New Issue
Block a user