summary refs log tree commit diff
path: root/gateway/src/opcodes/VoiceStateUpdate.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 20:10:25 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 20:10:25 +0200
commit2ba4c21f6b62c0ee8c2db06d36d489c6b6b20155 (patch)
treee50f93f1aa3148cd51a5e56bc45c7560e8f3c11d /gateway/src/opcodes/VoiceStateUpdate.ts
parentVOICE_STATE_UPDATE implementation, fix #210 (diff)
downloadserver-2ba4c21f6b62c0ee8c2db06d36d489c6b6b20155.tar.xz
:art: reformat files
Diffstat (limited to '')
-rw-r--r--gateway/src/opcodes/VoiceStateUpdate.ts43
1 files changed, 27 insertions, 16 deletions
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts

index 4719949c..04392b62 100644 --- a/gateway/src/opcodes/VoiceStateUpdate.ts +++ b/gateway/src/opcodes/VoiceStateUpdate.ts
@@ -11,16 +11,20 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { check.call(this, VoiceStateUpdateSchema, data.d); const body = data.d as VoiceStateUpdateSchema; - let voiceState + let voiceState; try { - voiceState = await VoiceState.findOneOrFail({where:{ user_id: this.user_id },relations: ["member", "member.user", "member.roles"]}); - if(voiceState.session_id !== this.session_id && body.channel_id === null) { //Should we also check guild_id === null? + voiceState = await VoiceState.findOneOrFail({ + where: { user_id: this.user_id }, + relations: ["member", "member.user", "member.roles"], + }); + if (voiceState.session_id !== this.session_id && body.channel_id === null) { + //Should we also check guild_id === null? //changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored - return + return; } //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; + if (body.guild_id === null) body.guild_id = voiceState.guild_id; voiceState.assign(body); } catch (error) { voiceState = new VoiceState({ @@ -28,32 +32,39 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { user_id: this.user_id, deaf: false, mute: false, - suppress: false - }) + suppress: false, + }); } //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 + 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; + const { id, ...newObj } = voiceState; await Promise.all([ voiceState.save(), - emitEvent({ event: "VOICE_STATE_UPDATE", data: newObj, guild_id: voiceState.guild_id} as VoiceStateUpdateEvent), + emitEvent({ + event: "VOICE_STATE_UPDATE", + data: newObj, + guild_id: voiceState.guild_id, + } as VoiceStateUpdateEvent), ]); //If it's null it means that we are leaving the channel and this event is not needed - if(voiceState.channel_id !== null) { + if (voiceState.channel_id !== null) { const regions = Config.get().regions; - await emitEvent({ event: "VOICE_SERVER_UPDATE", data: { + await emitEvent({ + event: "VOICE_SERVER_UPDATE", + data: { token: voiceState.token, guild_id: voiceState.guild_id, endpoint: regions.available[0].endpoint, //TODO return best endpoint or default - }, guild_id: voiceState.guild_id } as VoiceServerUpdateEvent) + }, + guild_id: voiceState.guild_id, + } as VoiceServerUpdateEvent); } -} \ No newline at end of file +}