36 lines
1.1 KiB
TypeScript
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
|
|
}
|
|
} |