diff --git a/gateway/src/events/Message.ts b/gateway/src/events/Message.ts
deleted file mode 100644
index acc39bb9..00000000
--- a/gateway/src/events/Message.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { CLOSECODES, OPCODES } from "../util/Constants";
-import { WebSocket, Payload } from "@fosscord/gateway";
-var erlpack: any;
-try {
- erlpack = require("@yukikaze-bot/erlpack");
-} catch (error) {}
-import OPCodeHandlers from "../opcodes";
-import { Tuple } from "lambert-server";
-import { check } from "../opcodes/instanceOf";
-import WS from "ws";
-
-const PayloadSchema = {
- op: Number,
- $d: new Tuple(Object, Number), // or number for heartbeat sequence
- $s: Number,
- $t: String,
-};
-
-export async function Message(this: WebSocket, buffer: WS.Data) {
- // TODO: compression
- var data: Payload;
-
- if (this.encoding === "etf" && buffer instanceof Buffer)
- data = erlpack.unpack(buffer);
- else if (this.encoding === "json" && typeof buffer === "string")
- data = JSON.parse(buffer);
- else return;
-
- check.call(this, PayloadSchema, data);
-
- // @ts-ignore
- const OPCodeHandler = OPCodeHandlers[data.op];
- if (!OPCodeHandler) {
- console.error("[Gateway] Unkown opcode " + data.op);
- // TODO: if all opcodes are implemented comment this out:
- // this.close(CLOSECODES.Unknown_opcode);
- return;
- }
-
- try {
- return await OPCodeHandler.call(this, data);
- } catch (error) {
- console.error(error);
- if (!this.CLOSED && this.CLOSING)
- return this.close(CLOSECODES.Unknown_error);
- }
-}
diff --git a/gateway/src/schema/VoiceStateUpdateSchema.ts b/gateway/src/schema/VoiceStateUpdateSchema.ts
deleted file mode 100644
index 9efa191e..00000000
--- a/gateway/src/schema/VoiceStateUpdateSchema.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-export const VoiceStateUpdateSchema = {
- $guild_id: String,
- $channel_id: String,
- self_mute: Boolean,
- self_deaf: Boolean,
- self_video: Boolean,
-};
-
-export interface VoiceStateUpdateSchema {
- guild_id?: string;
- channel_id?: string;
- self_mute: boolean;
- self_deaf: boolean;
- self_video: boolean;
-}
diff --git a/gateway/src/Server.ts b/src/gateway/Server.ts
index 7e1489be..82fbeba2 100644
--- a/gateway/src/Server.ts
+++ b/src/gateway/Server.ts
@@ -1,7 +1,6 @@
-import "missing-native-js-functions";
import dotenv from "dotenv";
dotenv.config();
-import { closeDatabase, Config, initDatabase, initEvent } from "@fosscord/util";
+import { closeDatabase, Config, getOrInitialiseDatabase, initEvent } from "@fosscord/util";
import ws from "ws";
import { Connection } from "./events/Connection";
import http from "http";
@@ -47,7 +46,7 @@ export class Server {
}
async start(): Promise<void> {
- await initDatabase();
+ await getOrInitialiseDatabase();
await Config.init();
await initEvent();
if (!this.server.listening) {
diff --git a/gateway/src/events/Close.ts b/src/gateway/events/Close.ts
index 5b7c512c..5b7c512c 100644
--- a/gateway/src/events/Close.ts
+++ b/src/gateway/events/Close.ts
diff --git a/gateway/src/events/Connection.ts b/src/gateway/events/Connection.ts
index 4954cd08..508b4741 100644
--- a/gateway/src/events/Connection.ts
+++ b/src/gateway/events/Connection.ts
@@ -8,7 +8,7 @@ import { Close } from "./Close";
import { Message } from "./Message";
import { createDeflate } from "zlib";
import { URL } from "url";
-var erlpack: any;
+let erlpack: any;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {}
@@ -27,6 +27,21 @@ export async function Connection(
socket.on("close", Close);
// @ts-ignore
socket.on("message", Message);
+
+ if(process.env.WS_LOGEVENTS)
+ [
+ "close",
+ "error",
+ "upgrade",
+ //"message",
+ "open",
+ "ping",
+ "pong",
+ "unexpected-response"
+ ].forEach(x=>{
+ socket.on(x, y => console.log(x, y));
+ });
+
console.log(`[Gateway] Connections: ${this.clients.size}`);
const { searchParams } = new URL(`http://localhost${request.url}`);
diff --git a/gateway/src/index.ts b/src/gateway/index.ts
index d77ce931..d77ce931 100644
--- a/gateway/src/index.ts
+++ b/src/gateway/index.ts
diff --git a/gateway/src/listener/listener.ts b/src/gateway/listener/listener.ts
index 060de65b..8c69e193 100644
--- a/gateway/src/listener/listener.ts
+++ b/src/gateway/listener/listener.ts
@@ -13,7 +13,6 @@ import {
import { OPCODES } from "../util/Constants";
import { Send } from "../util/Send";
import { WebSocket } from "@fosscord/gateway";
-import "missing-native-js-functions";
import { Channel as AMQChannel } from "amqplib";
import { Recipient } from "@fosscord/util";
@@ -50,10 +49,10 @@ export async function setupListener(this: WebSocket) {
where: { user_id: this.user_id, closed: false },
relations: ["channel"],
}),
- Relationship.find({
+ Relationship.find({ where: {
from_id: this.user_id,
type: RelationshipType.friends,
- }),
+ } }),
]);
const guilds = members.map((x) => x.guild);
diff --git a/gateway/src/opcodes/Heartbeat.ts b/src/gateway/opcodes/Heartbeat.ts
index 50394130..42b72d4b 100644
--- a/gateway/src/opcodes/Heartbeat.ts
+++ b/src/gateway/opcodes/Heartbeat.ts
@@ -2,7 +2,7 @@ import { Payload, WebSocket } from "@fosscord/gateway";
import { setHeartbeat } from "../util/Heartbeat";
import { Send } from "../util/Send";
-export async function onHeartbeat(this: WebSocket, data: Payload) {
+export async function onHeartbeat(this: WebSocket, _data: Payload) {
// TODO: validate payload
setHeartbeat(this);
diff --git a/gateway/src/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index 860000da..44db598c 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -18,16 +18,18 @@ import {
PrivateSessionProjection,
MemberPrivateProjection,
PresenceUpdateEvent,
+ UserSettings,
+ IdentifySchema,
} from "@fosscord/util";
import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants";
import { genSessionId } from "../util/SessionUtils";
import { setupListener } from "../listener/listener";
-import { IdentifySchema } from "../schema/Identify";
// import experiments from "./experiments.json";
const experiments: any = [];
import { check } from "./instanceOf";
import { Recipient } from "@fosscord/util";
+import { OrmUtils } from "@fosscord/util";
// TODO: user sharding
// TODO: check privileged intents, if defined in the config
@@ -50,15 +52,15 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const session_id = genSessionId();
this.session_id = session_id; //Set the session of the WebSocket object
-
+
const [user, read_states, members, recipients, session, application] =
await Promise.all([
User.findOneOrFail({
where: { id: this.user_id },
- relations: ["relationships", "relationships.to"],
+ relations: ["relationships", "relationships.to", "settings"],
select: [...PrivateUserProjection, "relationships"],
}),
- ReadState.find({ user_id: this.user_id }),
+ ReadState.find({ where: { user_id: this.user_id } }),
Member.find({
where: { id: this.user_id },
select: MemberPrivateProjection,
@@ -83,7 +85,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// TODO: public user selection
}),
// save the session and delete it when the websocket is closed
- new Session({
+ await OrmUtils.mergeDeep(new Session(), {
user_id: this.user_id,
session_id: session_id,
// TODO: check if status is only one of: online, dnd, offline, idle
@@ -96,12 +98,17 @@ export async function onIdentify(this: WebSocket, data: Payload) {
},
activities: [],
}).save(),
- Application.findOne({ id: this.user_id }),
+ Application.findOne({ where: { id: this.user_id } }),
]);
if (!user) return this.close(CLOSECODES.Authentication_failed);
+ if (!user.settings) { //settings may not exist after updating...
+ user.settings = new UserSettings();
+ user.settings.id = user.id;
+ //await (user.settings as UserSettings).save();
+ }
- if (!identify.intents) identify.intents = BigInt("0x6ffffffff");
+ if (!identify.intents) identify.intents = "30064771071";
this.intents = new Intents(identify.intents);
if (identify.shard) {
this.shard_id = identify.shard[0];
@@ -117,7 +124,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
return this.close(CLOSECODES.Invalid_shard);
}
}
- var users: PublicUser[] = [];
+ let users: PublicUser[] = [];
const merged_members = members.map((x: Member) => {
return [
@@ -231,7 +238,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const d: ReadyEventData = {
v: 8,
- application,
+ application: {id: application?.id??'', flags: application?.flags??0}, //TODO: check this code!
user: privateUser,
user_settings: user.settings,
// @ts-ignore
diff --git a/gateway/src/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts
index 7503ee61..74996f5b 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/src/gateway/opcodes/LazyRequest.ts
@@ -1,12 +1,9 @@
-import { getPermission, listenEvent, Member, Role } from "@fosscord/util";
-import { LazyRequest } from "../schema/LazyRequest";
+import { getPermission, listenEvent, Member, Role, getOrInitialiseDatabase, LazyRequest } from "@fosscord/util";
import { Send } from "../util/Send";
import { OPCODES } from "../util/Constants";
import { WebSocket, Payload, handlePresenceUpdate } from "@fosscord/gateway";
import { check } from "./instanceOf";
-import "missing-native-js-functions";
import { getRepository } from "typeorm";
-import "missing-native-js-functions";
// TODO: only show roles/members that have access to this channel
// TODO: config: to list all members (even those who are offline) sorted by role, or just those who are online
@@ -17,8 +14,9 @@ async function getMembers(guild_id: string, range: [number, number]) {
throw new Error("range is not a valid array");
}
// TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620
+ // TODO: rewrite this, released in 0.3.0
- let members = await getRepository(Member)
+ let members: Member[] = await (await getOrInitialiseDatabase()).getRepository(Member)
.createQueryBuilder("member")
.where("member.guild_id = :guild_id", { guild_id })
.leftJoinAndSelect("member.roles", "role")
@@ -122,7 +120,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
const ranges = channels![channel_id];
if (!Array.isArray(ranges)) throw new Error("Not a valid Array");
- const member_count = await Member.count({ guild_id });
+ const member_count = await Member.count({ where: { guild_id } });
const ops = await Promise.all(ranges.map((x) => getMembers(guild_id, x)));
// TODO: unsubscribe member_events that are not in op.members
diff --git a/gateway/src/opcodes/PresenceUpdate.ts b/src/gateway/opcodes/PresenceUpdate.ts
index 415df6ee..f31c9161 100644
--- a/gateway/src/opcodes/PresenceUpdate.ts
+++ b/src/gateway/opcodes/PresenceUpdate.ts
@@ -1,6 +1,5 @@
import { WebSocket, Payload } from "@fosscord/gateway";
-import { emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util";
-import { ActivitySchema } from "../schema/Activity";
+import { ActivitySchema, emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util";
import { check } from "./instanceOf";
export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
diff --git a/gateway/src/opcodes/RequestGuildMembers.ts b/src/gateway/opcodes/RequestGuildMembers.ts
index b80721dc..b80721dc 100644
--- a/gateway/src/opcodes/RequestGuildMembers.ts
+++ b/src/gateway/opcodes/RequestGuildMembers.ts
diff --git a/gateway/src/opcodes/Resume.ts b/src/gateway/opcodes/Resume.ts
index 42dc586d..42dc586d 100644
--- a/gateway/src/opcodes/Resume.ts
+++ b/src/gateway/opcodes/Resume.ts
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/src/gateway/opcodes/VoiceStateUpdate.ts
index 321e6b17..c4297a68 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/src/gateway/opcodes/VoiceStateUpdate.ts
@@ -1,4 +1,3 @@
-import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
import { Payload, WebSocket } from "@fosscord/gateway";
import { genVoiceToken } from "../util/SessionUtils";
import { check } from "./instanceOf";
@@ -7,11 +6,13 @@ import {
emitEvent,
Guild,
Member,
- Region,
VoiceServerUpdateEvent,
VoiceState,
VoiceStateUpdateEvent,
+ VoiceStateUpdateSchema,
} from "@fosscord/util";
+import { OrmUtils } from "@fosscord/util";
+import { Region } from "@fosscord/util";
// TODO: check if a voice server is setup
// Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not.
@@ -19,6 +20,11 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
+ if(body.guild_id == null) {
+ console.log(`[Gateway] VoiceStateUpdate called with guild_id == null by user ${this.user_id}!`);
+ return;
+ }
+
let voiceState: VoiceState;
try {
voiceState = await VoiceState.findOneOrFail({
@@ -47,9 +53,9 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
if (body.guild_id === null) body.guild_id = voiceState.guild_id;
- voiceState.assign(body);
+ voiceState = OrmUtils.mergeDeep(voiceState, body);
} catch (error) {
- voiceState = new VoiceState({
+ voiceState = OrmUtils.mergeDeep(new VoiceState(), {
...body,
user_id: this.user_id,
deaf: false,
@@ -84,7 +90,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//If it's null it means that we are leaving the channel and this event is not needed
if (voiceState.channel_id !== null) {
- const guild = await Guild.findOne({ id: voiceState.guild_id });
+ const guild = await Guild.findOne({ where: { id: voiceState.guild_id } });
const regions = Config.get().regions;
let guildRegion: Region;
if (guild && guild.region) {
diff --git a/gateway/src/opcodes/experiments.json b/src/gateway/opcodes/experiments.json
index 0370b5da..0370b5da 100644
--- a/gateway/src/opcodes/experiments.json
+++ b/src/gateway/opcodes/experiments.json
diff --git a/gateway/src/opcodes/index.ts b/src/gateway/opcodes/index.ts
index 027739db..027739db 100644
--- a/gateway/src/opcodes/index.ts
+++ b/src/gateway/opcodes/index.ts
diff --git a/gateway/src/opcodes/instanceOf.ts b/src/gateway/opcodes/instanceOf.ts
index 6fd50852..eb6f6ea1 100644
--- a/gateway/src/opcodes/instanceOf.ts
+++ b/src/gateway/opcodes/instanceOf.ts
@@ -1,4 +1,4 @@
-import { instanceOf } from "lambert-server";
+import { instanceOf } from "@fosscord/util";
import { WebSocket } from "@fosscord/gateway";
import { CLOSECODES } from "../util/Constants";
diff --git a/gateway/src/start.ts b/src/gateway/start.ts
index 09a54751..2000522a 100644
--- a/gateway/src/start.ts
+++ b/src/gateway/start.ts
@@ -5,7 +5,7 @@ import { Server } from "./Server";
import { config } from "dotenv";
config();
-var port = Number(process.env.PORT);
+let port = Number(process.env.PORT);
if (isNaN(port)) port = 3002;
const server = new Server({
diff --git a/gateway/src/util/Constants.ts b/src/gateway/util/Constants.ts
index 692f9028..692f9028 100644
--- a/gateway/src/util/Constants.ts
+++ b/src/gateway/util/Constants.ts
diff --git a/gateway/src/util/Heartbeat.ts b/src/gateway/util/Heartbeat.ts
index f6871cfe..f6871cfe 100644
--- a/gateway/src/util/Heartbeat.ts
+++ b/src/gateway/util/Heartbeat.ts
diff --git a/gateway/src/util/Send.ts b/src/gateway/util/Send.ts
index c8627b03..2a28d8e0 100644
--- a/gateway/src/util/Send.ts
+++ b/src/gateway/util/Send.ts
@@ -1,4 +1,4 @@
-var erlpack: any;
+let erlpack: any;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {
@@ -7,6 +7,8 @@ try {
import { Payload, WebSocket } from "@fosscord/gateway";
export async function Send(socket: WebSocket, data: Payload) {
+ if(process.env.WS_VERBOSE)
+ console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object
diff --git a/gateway/src/util/SessionUtils.ts b/src/gateway/util/SessionUtils.ts
index bf854042..bf854042 100644
--- a/gateway/src/util/SessionUtils.ts
+++ b/src/gateway/util/SessionUtils.ts
diff --git a/gateway/src/util/WebSocket.ts b/src/gateway/util/WebSocket.ts
index e3313f40..9496da85 100644
--- a/gateway/src/util/WebSocket.ts
+++ b/src/gateway/util/WebSocket.ts
@@ -8,8 +8,8 @@ export interface WebSocket extends WS {
session_id: string;
encoding: "etf" | "json";
compress?: "zlib-stream";
- shard_count?: bigint;
- shard_id?: bigint;
+ shard_count?: number;
+ shard_id?: number;
deflate?: Deflate;
heartbeatTimeout: NodeJS.Timeout;
readyTimeout: NodeJS.Timeout;
diff --git a/gateway/src/util/index.ts b/src/gateway/util/index.ts
index 0be5ecee..0be5ecee 100644
--- a/gateway/src/util/index.ts
+++ b/src/gateway/util/index.ts
diff --git a/gateway/src/schema/Activity.ts b/src/util/schemas/ActivitySchema.ts
index e18f66c8..e18f66c8 100644
--- a/gateway/src/schema/Activity.ts
+++ b/src/util/schemas/ActivitySchema.ts
diff --git a/gateway/src/schema/Identify.ts b/src/util/schemas/IdentifySchema.ts
index 21141321..f3d60fb3 100644
--- a/gateway/src/schema/Identify.ts
+++ b/src/util/schemas/IdentifySchema.ts
@@ -1,8 +1,8 @@
-import { ActivitySchema } from "./Activity";
+import { ActivitySchema } from "./ActivitySchema";
export const IdentifySchema = {
token: String,
- $intents: BigInt, // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
+ $intents: String, // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
$properties: Object,
// {
// // discord uses $ in the property key for bots, so we need to double prefix it, because instanceOf treats $ (prefix) as a optional key
@@ -33,7 +33,7 @@ export const IdentifySchema = {
$presence: ActivitySchema,
$compress: Boolean,
$large_threshold: Number,
- $shard: [BigInt, BigInt],
+ $shard: [Number, Number],
$guild_subscriptions: Boolean,
$capabilities: Number,
$client_state: {
@@ -71,11 +71,11 @@ export interface IdentifySchema {
client_version?: string;
system_locale?: string;
};
- intents?: bigint; // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
+ intents?: string; // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
presence?: ActivitySchema;
compress?: boolean;
large_threshold?: number;
- shard?: [bigint, bigint];
+ shard?: [number, number];
guild_subscriptions?: boolean;
capabilities?: number;
client_state?: {
diff --git a/gateway/src/schema/LazyRequest.ts b/src/util/schemas/LazyRequestSchema.ts
index 1fe658bb..1fe658bb 100644
--- a/gateway/src/schema/LazyRequest.ts
+++ b/src/util/schemas/LazyRequestSchema.ts
|