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": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord-server-util": "github:discord-open-source/discord-server-util",
|
|
||||||
"erlpack": "^0.1.3",
|
"erlpack": "^0.1.3",
|
||||||
|
"fosscord-server-util": "github:fosscord/fosscord-server-util",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"lambert-db": "^1.1.6",
|
"lambert-db": "^1.1.8",
|
||||||
"lambert-server": "^1.1.3",
|
"lambert-server": "^1.1.3",
|
||||||
"missing-native-js-functions": "^1.2.3",
|
"missing-native-js-functions": "^1.2.3",
|
||||||
"node-fetch": "^2.6.1",
|
"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 { Server as WebSocketServer } from "ws";
|
||||||
import { Connection } from "./events/Connection";
|
import { Connection } from "./events/Connection";
|
||||||
|
import Config from "./util/Config";
|
||||||
|
|
||||||
export class Server {
|
export class Server {
|
||||||
public ws: WebSocketServer;
|
public ws: WebSocketServer;
|
||||||
@ -9,8 +10,15 @@ export class Server {
|
|||||||
this.ws.on("connection", Connection);
|
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> {
|
async listen(): Promise<void> {
|
||||||
await db.init();
|
await db.init();
|
||||||
|
await this.setupSchema();
|
||||||
|
await Config.init();
|
||||||
console.log("listening");
|
console.log("listening");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,23 @@
|
|||||||
import { db, User } from "discord-server-util";
|
import { db, User, Event } from "fosscord-server-util";
|
||||||
import { ProviderCache } from "lambert-db";
|
|
||||||
import { MongodbProviderCache } from "lambert-db/dist/Mongodb";
|
import { MongodbProviderCache } from "lambert-db/dist/Mongodb";
|
||||||
import WebSocket from "../util/WebSocket";
|
import WebSocket from "../util/WebSocket";
|
||||||
|
|
||||||
export async function setupListener(this: 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();
|
const user: User = await db.data.users({ id: this.userid }).get();
|
||||||
|
|
||||||
// * MongoDB specific $in query to get all guilds of the user
|
// * MongoDB specific $in query to get all guilds of the user
|
||||||
const guildCache: MongodbProviderCache = await db.data
|
const eventStream: MongodbProviderCache = await db.data
|
||||||
.guilds({ id: { $id: user.guilds } })
|
.guilds({ $or: [{ guild_id: { $in: user.guilds } }, { user_id: this.userid }] })
|
||||||
.cache({ onlyEvents: true })
|
.cache({ onlyEvents: true })
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
guildCache.on("change", (data) => {
|
eventStream.on("insert", (document: Event) => {
|
||||||
console.log(data);
|
console.log("event", document);
|
||||||
|
this.emit(document.event, document.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.once("close", async () => {
|
this.once("close", () => eventStream.destroy());
|
||||||
await guildCache.destroy();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,17 +1,20 @@
|
|||||||
import { CLOSECODES, Payload } from "../util/Constants";
|
import { CLOSECODES, Payload } from "../util/Constants";
|
||||||
import Config from "../util/Config";
|
|
||||||
import WebSocket from "../util/WebSocket";
|
import WebSocket from "../util/WebSocket";
|
||||||
import { checkToken, IdentifySchema } from "discord-server-util";
|
import { checkToken, IdentifySchema } from "fosscord-server-util";
|
||||||
import { check } from "./instanceOf";
|
import { setupListener } from "../listener/listener";
|
||||||
|
import { instanceOf } from "lambert-server";
|
||||||
|
|
||||||
export async function onIdentify(this: WebSocket, data: Payload) {
|
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 {
|
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) {
|
} catch (error) {
|
||||||
return this.close(CLOSECODES.Authentication_failed);
|
return this.close(CLOSECODES.Authentication_failed);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { Config } from "discord-server-util";
|
import { Config } from "fosscord-server-util";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
init() {
|
init() {
|
||||||
|
|||||||
Reference in New Issue
Block a user