summary refs log tree commit diff
path: root/gateway
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-03 15:57:44 +0200
committerGitHub <noreply@github.com>2021-09-03 15:57:44 +0200
commit3a3de120dca58ac79920d45af8b3166815fea637 (patch)
tree07b9d0362549c2f3b2bbc31d0233ce7cc1f03295 /gateway
parent:bug: fix #311 (diff)
parent:bug: only delete session id for authenticated connections (diff)
downloadserver-3a3de120dca58ac79920d45af8b3166815fea637.tar.xz
Merge pull request #314 from AlTech98/master
Fix VoiceStateUpdate
Diffstat (limited to 'gateway')
-rw-r--r--gateway/src/events/Close.ts2
-rw-r--r--gateway/src/opcodes/VoiceStateUpdate.ts24
2 files changed, 20 insertions, 6 deletions
diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts
index b4fed316..2f274ec4 100644
--- a/gateway/src/events/Close.ts
+++ b/gateway/src/events/Close.ts
@@ -4,7 +4,7 @@ import { Session } from "@fosscord/util";
 
 export async function Close(this: WebSocket, code: number, reason: string) {
 	console.log("[WebSocket] closed", code, reason);
-	await Session.delete({ session_id: this.session_id });
+	if (this.session_id) await Session.delete({ session_id: this.session_id });
 	// @ts-ignore
 	this.off("message", Message);
 }
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts
index 04392b62..fba0db1f 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/gateway/src/opcodes/VoiceStateUpdate.ts
@@ -2,7 +2,7 @@ import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
 import { Payload } from "../util/Constants";
 import WebSocket from "../util/WebSocket";
 import { check } from "./instanceOf";
-import { Config, emitEvent, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util";
+import { Config, emitEvent, Member, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util";
 import { genVoiceToken } from "../util/SessionUtils";
 // 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.
@@ -14,8 +14,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 	let voiceState;
 	try {
 		voiceState = await VoiceState.findOneOrFail({
-			where: { user_id: this.user_id },
-			relations: ["member", "member.user", "member.roles"],
+			where: { user_id: this.user_id }
 		});
 		if (voiceState.session_id !== this.session_id && body.channel_id === null) {
 			//Should we also check guild_id === null?
@@ -23,6 +22,15 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 			return;
 		}
 
+		//If a user change voice channel between guild we should send a left event first
+		if (voiceState.guild_id !== body.guild_id && voiceState.session_id === this.session_id) {
+			await emitEvent({
+				event: "VOICE_STATE_UPDATE",
+				data: { ...voiceState, channel_id: null },
+				guild_id: voiceState.guild_id,
+			})
+		}
+
 		//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);
@@ -36,12 +44,18 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 		});
 	}
 
+	//TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user
+	//TODO the member.user should only have these properties: avatar, discriminator, id, username
+	//TODO this may fail
+	voiceState.member = await Member.findOneOrFail({
+		where: { id: voiceState.user_id, guild_id: voiceState.guild_id },
+		relations: ["user", "roles"],
+	})
+
 	//If the session changed we generate a new token
 	if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken();
 	voiceState.session_id = this.session_id;
 
-	//TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user
-	//TODO the member.user should only have these properties: avatar, discriminator, id, username
 	const { id, ...newObj } = voiceState;
 
 	await Promise.all([