84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { getVideoInfo, YouTube, ytDownload } from "../../deps.ts";
|
|
import { bufferIter, retry } from "../../utils/mod.ts";
|
|
import { demux } from "../demux/mod.ts";
|
|
import { createAudioSource, empty } from "./audio-source.ts";
|
|
|
|
import { errorMessageCallback, isPlaylist } from "../../../utils.ts";
|
|
|
|
export async function getYoutubeSources(guildId: bigint, added_by?: string, queries: string[]) {
|
|
const sources = queries.map((query) => getYoutubeSource(query, guildId, added_by));
|
|
const awaitedSources = await Promise.all(sources);
|
|
|
|
return awaitedSources
|
|
.filter((source) => source !== undefined)
|
|
.map((source) => source!);
|
|
}
|
|
|
|
/*export async function getYoutubeSource(query: string, guildId: bigint, added_by?: string) {
|
|
if(isPlaylist(query)) {
|
|
const playlist = await YouTube.getPlaylist(query);
|
|
const count = playlist.videoCount;
|
|
const sources = [];
|
|
|
|
for(const video of playlist.videos) {
|
|
const videoId = video.id ? video.id : "";
|
|
sources.push(getVideo(videoId, guildId, added_by));
|
|
}
|
|
|
|
return sources;
|
|
}
|
|
|
|
return await getVideo(query, guildId, added_by);
|
|
}*/
|
|
|
|
async function getYoutubeSource(query: string, guildId: bigint, added_by?: string) {
|
|
try {
|
|
const result = await getVideoInfo(query);
|
|
if(result.videoDetails.videoId) {
|
|
const id = result.videoDetails.videoId;
|
|
const title = result.videoDetails.title;
|
|
|
|
return createAudioSource(title, async () => {
|
|
const stream = await retry(
|
|
async () =>
|
|
await ytDownload(id, {
|
|
mimeType: `audio/webm; codecs="opus"`,
|
|
})
|
|
);
|
|
if (stream === undefined) {
|
|
errorMessageCallback(guildId, `There was an error trying to play **${title}**:\n
|
|
The stream couldn't be found`);
|
|
console.log(`Failed to play ${title}\n Returning empty stream`);
|
|
return empty();
|
|
}
|
|
return bufferIter(demux(stream));
|
|
}, guildId, added_by);
|
|
}
|
|
//const result = await ytDownload(query, { mimeType: `audio/webm; codecs="opus"`, });
|
|
//console.log(result);
|
|
|
|
/*const results = await YouTube.search(query, { limit: 1, type: "video" });
|
|
if (results.length > 0) {
|
|
const { id, title } = results[0];
|
|
return createAudioSource(title!, async () => {
|
|
const stream = await retry(
|
|
async () =>
|
|
await ytDownload(id!, {
|
|
mimeType: `audio/webm; codecs="opus"`,
|
|
})
|
|
);
|
|
if (stream === undefined) {
|
|
errorMessageCallback(guildId, `There was an error trying to play **${title}**:\n
|
|
The stream couldn't be found`);
|
|
console.log(`Failed to play ${title}\n Returning empty stream`);
|
|
return empty();
|
|
}
|
|
return bufferIter(demux(stream));
|
|
}, guildId, added_by);
|
|
}*/
|
|
} catch(err) {
|
|
console.error(err);
|
|
return undefined;
|
|
}
|
|
}
|