fp/packages/fetchers/src/getUrlFromMessage.ts
CJ_Clippy 4d65294f7d
Some checks failed
ci / build (push) Failing after 1s
move fetchers to their own package
2024-09-05 21:39:08 -08:00

36 lines
1.1 KiB
TypeScript

import { type Interaction } from "discordeno"
import { configs } from "../../../services/bot/src/config.ts"
import { type Stream } from "@futureporn/types"
import { logger } from "@discordeno/bot"
export default async function getUrlFromMessage(interaction: Interaction): Promise<string|null> {
const messageId = interaction.message?.id
const pgRequestUrl = `${configs.postgrestUrl}/streams?discord_message_id=eq.${messageId}`
logger.info(`pgRequestUrl=${pgRequestUrl}`)
const requestOptions = {
method: 'GET',
headers: {
'Authorization': `Bearer ${configs.automationUserJwt}`,
'Content-Type': 'application/json',
'Prefer': 'return=representation'
}
}
try {
const res = await fetch (pgRequestUrl, requestOptions)
if (!res.ok) {
const body = await res.json()
logger.error(body)
throw new Error(`Problem during getUrlFromMessage. res.status=${res.status}, res.statusText=${res.statusText}`)
}
const json = await res.json() as Stream[]
const stream = json[0]
const url = stream?.url
if (!url) return null
else return url
} catch (e) {
logger.error(e)
throw e
}
}