diff --git a/examples/play - cookies.js b/examples/play - cookies.js new file mode 100644 index 0000000..1886330 --- /dev/null +++ b/examples/play - cookies.js @@ -0,0 +1,48 @@ +const discord = require('discord.js') +const { Intents } = require('discord.js') +const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice') +const youtube = require('play-dl') +const client = new discord.Client({ intents : [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES] , partials : ['CHANNEL', 'MESSAGE']}) +const token = '< YOUR BOT TOKEN >' +const COOKIE = '< YOUR COOKIES >' + + +client.on('messageCreate', async message => { + if(message.content.startsWith('!play')){ + if(!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + const connection = joinVoiceChannel({ + channelId : message.member.voice.channel.id, + guildId : message.guild.id, + adapterCreator: message.guild.voiceAdapterCreator + }) + + let args = message.content.split('play ')[1].split(' ')[0] + let stream = await youtube.stream(args, COOKIE) + /* + OR if you want to get info about youtube link and then stream it + + let yt_info = await youtube.video_info(args, COOKIE) + console.log(yt_info.video_details.title) + let stream = await youtube.stream_from_info(yt_info) + */ + + let resource = createAudioResource(stream.stream, { + inputType : stream.type + }) + let player = createAudioPlayer({ + behaviors: { + noSubscriber: NoSubscriberBehavior.Play + } + }) + player.play(resource) + + connection.subscribe(player) + } +}) + +client.on('ready', () => { + console.log(`We have logged in as ${client.user.tag}!`) +}) + +client.login(token); + diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index de4c448..0ae108f 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -35,8 +35,8 @@ function parseAudioFormats(formats : any[]){ return result } -export async function stream(url : string): Promise{ - let info = await video_info(url) +export async function stream(url : string, cookie? : string): Promise{ + let info = await video_info(url, cookie) let final: any[] = []; let type : StreamType; if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) { diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index fd4d2a7..1de63b8 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -21,14 +21,16 @@ export function validate_playlist(url : string): boolean{ return true } -export async function video_basic_info(url : string){ +export async function video_basic_info(url : string, cookie? : string){ if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') let video_id : string; if(url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0] else if(url.includes('youtube.com/embed/')) video_id = url.split('youtube.com/embed/')[1].split('/')[0] else video_id = url.split('watch?v=')[1].split('&')[0]; let new_url = 'https://www.youtube.com/watch?v=' + video_id - let body = await url_get(new_url) + let body = await url_get(new_url, { + headers : (cookie) ? { 'cookie' : cookie } : {} + }) let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}') let initial_response = JSON.parse(body.split("var ytInitialData = ")[1].split("}};")[0] + '}}') let badge = initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges && initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges[0] @@ -86,8 +88,8 @@ function parseSeconds(seconds : number): string { return hDisplay + mDisplay + sDisplay; } -export async function video_info(url : string) { - let data = await video_basic_info(url) +export async function video_info(url : string, cookie? : string) { + let data = await video_basic_info(url, cookie) if(data.LiveStreamData.isLive === true && data.LiveStreamData.hlsManifestUrl !== null){ return data }