summary refs log tree commit diff
path: root/gateway/src/opcodes/VoiceStateUpdate.ts
diff options
context:
space:
mode:
authorAlTech98 <altech123159@gmail.com>2021-09-04 11:41:41 +0200
committerAlTech98 <altech123159@gmail.com>2021-09-04 11:41:41 +0200
commitb965d93e514fee81fbf62a31a1b768ec1395b98b (patch)
tree2f8e3195487eca2f815f4c86324f4e3e26b90688 /gateway/src/opcodes/VoiceStateUpdate.ts
parent:bug: fix ReadyEventData (diff)
downloadserver-b965d93e514fee81fbf62a31a1b768ec1395b98b.tar.xz
VOICE_SERVER_UPDATE now has the endpoint of the guild's region
Diffstat (limited to '')
-rw-r--r--gateway/src/opcodes/VoiceStateUpdate.ts13
1 files changed, 10 insertions, 3 deletions
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts
index fba0db1f..95a01608 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, Member, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util";
+import { Config, emitEvent, Guild, Member, Region, 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.
@@ -11,7 +11,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 	check.call(this, VoiceStateUpdateSchema, data.d);
 	const body = data.d as VoiceStateUpdateSchema;
 
-	let voiceState;
+	let voiceState: VoiceState;
 	try {
 		voiceState = await VoiceState.findOneOrFail({
 			where: { user_id: this.user_id }
@@ -69,14 +69,21 @@ 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 regions = Config.get().regions;
+		let guildRegion: Region;
+		if (guild && guild.region) {
+			guildRegion = regions.available.filter(r => (r.id === guild.region))[0]
+		} else {
+			guildRegion = regions.available.filter(r => (r.id === regions.default))[0]
+		}
 
 		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
+				endpoint: guildRegion.endpoint,
 			},
 			guild_id: voiceState.guild_id,
 		} as VoiceServerUpdateEvent);