From aec4f6fd1d64e1a3c99a3d1112b25f3fe69d9b6a Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Fri, 3 Sep 2021 13:27:15 +0200 Subject: [PATCH 1/5] VoiceState fix for db update --- gateway/src/opcodes/VoiceStateUpdate.ts | 15 ++++++++++----- util/src/entities/VoiceState.ts | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts index 04392b62..10808c10 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? @@ -36,12 +35,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([ diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts index d7a032c7..bb1deb3d 100644 --- a/util/src/entities/VoiceState.ts +++ b/util/src/entities/VoiceState.ts @@ -32,7 +32,7 @@ export class VoiceState extends BaseClass { @ManyToOne(() => User) user: User; - @JoinColumn({ name: "user_id" }) + @JoinColumn([{ name: "user_id", referencedColumnName: "id" },{ name: "guild_id", referencedColumnName: "guild_id" }]) @ManyToOne(() => Member) member: Member; From c9024692f72f75a5eb9ef0acb3c716fd9e24a4dd Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Fri, 3 Sep 2021 14:10:34 +0200 Subject: [PATCH 2/5] Temp fix for Guild.voice_states not working --- util/src/entities/VoiceState.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts index bb1deb3d..56eb244e 100644 --- a/util/src/entities/VoiceState.ts +++ b/util/src/entities/VoiceState.ts @@ -32,8 +32,9 @@ export class VoiceState extends BaseClass { @ManyToOne(() => User) user: User; - @JoinColumn([{ name: "user_id", referencedColumnName: "id" },{ name: "guild_id", referencedColumnName: "guild_id" }]) - @ManyToOne(() => Member) + // @JoinColumn([{ name: "user_id", referencedColumnName: "id" },{ name: "guild_id", referencedColumnName: "guild_id" }]) + // @ManyToOne(() => Member) + //TODO find a way to make it work without breaking Guild.voice_states member: Member; @Column() From 4187a8d1963a47c40cdf0ea8b998a1ca705a7d86 Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Fri, 3 Sep 2021 14:12:44 +0200 Subject: [PATCH 3/5] Make the user leave the current channel before joining one in another guild --- gateway/src/opcodes/VoiceStateUpdate.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts index 10808c10..668508fd 100644 --- a/gateway/src/opcodes/VoiceStateUpdate.ts +++ b/gateway/src/opcodes/VoiceStateUpdate.ts @@ -22,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) { + 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); From 48036bdad577740c3b3ccf839870b7c190a1618a Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Fri, 3 Sep 2021 14:23:33 +0200 Subject: [PATCH 4/5] Fix to send the voice leave event only to the right session --- gateway/src/opcodes/VoiceStateUpdate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts index 668508fd..fba0db1f 100644 --- a/gateway/src/opcodes/VoiceStateUpdate.ts +++ b/gateway/src/opcodes/VoiceStateUpdate.ts @@ -23,7 +23,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { } //If a user change voice channel between guild we should send a left event first - if (voiceState.guild_id !== body.guild_id) { + if (voiceState.guild_id !== body.guild_id && voiceState.session_id === this.session_id) { await emitEvent({ event: "VOICE_STATE_UPDATE", data: { ...voiceState, channel_id: null }, From c45e1b0f10cd1b99cf83465dedb120f10d6298e5 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 3 Sep 2021 15:57:29 +0200 Subject: [PATCH 5/5] :bug: only delete session id for authenticated connections --- gateway/src/events/Close.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); }