diff --git a/src/gateway/opcodes/StreamCreate.ts b/src/gateway/opcodes/StreamCreate.ts index 1b8fa7e..cd3c4b7 100644 --- a/src/gateway/opcodes/StreamCreate.ts +++ b/src/gateway/opcodes/StreamCreate.ts @@ -46,7 +46,11 @@ // TODO: actually apply preferred_region from the event payload const regions = Config.get().regions; - const guildRegion = regions.available.filter((r) => r.id === regions.default)[0]; + const guildRegion = regions.available.find((r) => r.id === regions.default); + + if (!guildRegion) { + throw new Error("No default region configured"); + } // first make sure theres no other streams for this user that somehow didnt get cleared await Stream.delete({ diff --git a/src/gateway/opcodes/VoiceStateUpdate.ts b/src/gateway/opcodes/VoiceStateUpdate.ts index a128ee3..89ead6e 100644 --- a/src/gateway/opcodes/VoiceStateUpdate.ts +++ b/src/gateway/opcodes/VoiceStateUpdate.ts @@ -127,11 +127,21 @@ where: { id: voiceState.guild_id }, }); const regions = Config.get().regions; - let guildRegion: Region; + let guildRegion: Region | undefined; + + const defaultRegion = regions.available.find((r) => r.id === regions.default); + if (guild && guild.region) { - guildRegion = regions.available.filter((r) => r.id === guild.region)[0]; + // in case the configured guild region does not exist (which can + // happen when server regions config is updated after guild creation), + // fallback to default region + guildRegion = regions.available.find((r) => r.id === guild.region) ?? defaultRegion; } else { - guildRegion = regions.available.filter((r) => r.id === regions.default)[0]; + guildRegion = defaultRegion; + } + + if (!guildRegion) { + throw new Error("Unable to find suitable region due to misconfiguration of regions"); } await emitEvent({