music: fix urls

This commit is contained in:
Cynthia Foxwell 2022-12-10 15:52:40 -07:00
parent 77e205ec2f
commit a4acf3a7f2

View file

@ -1,6 +1,7 @@
const {Collection} = require("oceanic.js");
const {
AudioPlayerStatus,
StreamType,
createAudioPlayer,
createAudioResource,
} = require("@discordjs/voice");
@ -189,6 +190,25 @@ async function createVoiceConnection(guild_id, voice_id, text_id) {
return state;
}
async function processUrl(url) {
const res = await fetch(url);
const contentType = res.headers.get("Content-Type");
let type = StreamType.Arbitrary;
if (contentType == "video/webm" || contentType == "audio/webm") {
type = StreamType.WebmOpus;
} else if (contentType == "audio/ogg" || contentType == "video/ogg") {
type = StreamType.OggOpus;
} else if (contentType == "audio/opus") {
type = StreamType.Opus;
}
return {
stream: await res.body(),
type,
};
}
async function enqueue({
guild_id,
voice_id,
@ -249,7 +269,7 @@ async function enqueue({
title = info.title;
length = info.duration;
thumbnail = info.artwork_url;
media = await playdl.stream(streamUrl);
media = await processUrl(streamUrl);
} else if (type == "file") {
title = url;
let info;
@ -275,7 +295,7 @@ async function enqueue({
}
length = info.duration ? Math.floor(info.duration) * 1000 : 0;
media = await playdl.stream(url);
media = await processUrl(url);
}
if (connection.status && connection.status != AudioPlayerStatus.Idle) {