diff --git a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts
index 3b310325..ba801298 100644
--- a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts
+++ b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts
@@ -17,7 +17,7 @@
*/
import { route } from "@spacebar/api";
-import { Channel, DiscordApiErrors, emitEvent, getPermission, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
+import { Channel, DiscordApiErrors, emitEvent, getPermission, Member, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { ChannelType, VoiceStateUpdateSchema } from "@spacebar/schemas";
@@ -59,17 +59,16 @@ router.patch(
if (!body.suppress) body.request_to_speak_timestamp = new Date();
if (body.request_to_speak_timestamp) perms.hasThrow("REQUEST_TO_SPEAK");
- const voice_state = await VoiceState.findOne({
+ const voiceState = await VoiceState.findOne({
where: {
guild_id,
channel_id: body.channel_id,
user_id,
},
- relations: { member: true },
});
- if (!voice_state) throw DiscordApiErrors.UNKNOWN_VOICE_STATE;
+ if (!voiceState) throw DiscordApiErrors.UNKNOWN_VOICE_STATE;
- voice_state.assign(body);
+ voiceState.assign(body);
const channel = await Channel.findOneOrFail({
where: { guild_id, id: body.channel_id },
});
@@ -77,13 +76,20 @@ router.patch(
throw DiscordApiErrors.CANNOT_EXECUTE_ON_THIS_CHANNEL_TYPE;
}
+ voiceState.member = await Member.findOneOrFail({
+ where: {
+ id: voiceState.user_id,
+ guild_id: voiceState.guild_id,
+ },
+ });
+
await Promise.all([
- voice_state.save(),
+ voiceState.save(),
emitEvent({
event: "VOICE_STATE_UPDATE",
data: {
- ...voice_state.toPublicVoiceState(),
- member: voice_state.member.toPublicMember(),
+ ...voiceState.toPublicVoiceState(),
+ member: voiceState.member.toPublicMember(),
},
guild_id,
} satisfies VoiceStateUpdateEvent),
diff --git a/src/gateway/events/Close.ts b/src/gateway/events/Close.ts
index 322a9c4e..8c375253 100644
--- a/src/gateway/events/Close.ts
+++ b/src/gateway/events/Close.ts
@@ -17,7 +17,7 @@
*/
import { WebSocket } from "@spacebar/gateway";
-import { emitEvent, PresenceUpdateEvent, Session, SessionsReplace, User, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
+import { emitEvent, Member, PresenceUpdateEvent, Session, SessionsReplace, User, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
export async function Close(this: WebSocket, code: number, reason: Buffer) {
console.log("[WebSocket] closed", code, reason.toString());
@@ -32,7 +32,6 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) {
const voiceState = await VoiceState.findOne({
where: { user_id: this.user_id },
- relations: { member: true },
});
// clear the voice state for this session if user was in voice channel
@@ -48,6 +47,12 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) {
voiceState.self_video = false;
await voiceState.save();
+ voiceState.member = await Member.findOneOrFail({
+ where: {
+ id: voiceState.user_id,
+ guild_id: prevGuildId,
+ },
+ });
// let the users in previous guild/channel know that user disconnected
await emitEvent({
event: "VOICE_STATE_UPDATE",
diff --git a/src/gateway/opcodes/StreamDelete.ts b/src/gateway/opcodes/StreamDelete.ts
index aaa3361c..b7dcd2cc 100644
--- a/src/gateway/opcodes/StreamDelete.ts
+++ b/src/gateway/opcodes/StreamDelete.ts
@@ -1,5 +1,5 @@
import { parseStreamKey, Payload, WebSocket } from "@spacebar/gateway";
-import { emitEvent, Stream, StreamDeleteEvent, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
+import { emitEvent, Member, Stream, StreamDeleteEvent, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util";
import { check } from "./instanceOf";
import { StreamDeleteSchema } from "@spacebar/schemas";
@@ -53,6 +53,12 @@ export async function onStreamDelete(this: WebSocket, data: Payload) {
if (voiceState) {
voiceState.self_stream = false;
await voiceState.save();
+ voiceState.member = await Member.findOneOrFail({
+ where: {
+ id: voiceState.user_id,
+ guild_id: voiceState.guild_id,
+ },
+ });
await emitEvent({
event: "VOICE_STATE_UPDATE",
|