probably a simple question for some of you to answer.
I'm trying to check the status of an online audio stream then utilize that stream in some code (https://api.tmw.media/ggradio/stream & https://api.tmw.media/ggradio/stream/ogg). I know I will get a 404 if the file doesn't exist, but I'm having issues where I get stuck awaiting the fetch to return if status 200. Any advice on how I should be doing this?
export async function startBroadcast(this: Root) {
if (!this.extensions.broadcast) {
this.extensions['broadcast'] = this.client.voice?.createBroadcast()
if (this.config.radio?.streamLink) {
if (this.extensions.broadcast) {
const broadcast = this.extensions.broadcast as VoiceBroadcast
let tested: number = 0
for (const link of this.config.radio.streamLink) {
const req = await this.fetch(link)
console.log(req)
if (!req.ok) {
this.log(LoggingLevels.error, `${link} is 404`)
tested++
if (tested == this.config.radio.streamLink.length) {
this.log(
LoggingLevels.emergency,
`All stream links failed. Radio is likely offline`
)
throw new Error('All stream links failed')
}
return
}
const broadcastOptions: StreamOptions = {
highWaterMark: 50,
volume: false
}
this.log(
LoggingLevels.debug,
`Starting up broadcast using stream link: ${link}`
)
broadcast.play(link, broadcastOptions)
break
}
}
}
}
}
(this.fetch
is https://www.npmjs.com/package/node-fetch)