From 379b38c36312e5b073101c9df58f8947a34610eb Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Thu, 2 Sep 2021 09:23:56 +0530 Subject: [PATCH 1/4] Cookies Update --- play-dl/YouTube/stream.ts | 4 ++-- play-dl/YouTube/utils/extractor.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) 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 51862cc..dc346a1 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] + '}}') if(player_response.playabilityStatus.status === 'ERROR') throw new Error(`While getting info from url \n ${player_response.playabilityStatus.reason}`) if(player_response.playabilityStatus.status === 'LOGIN_REQUIRED') throw new Error(`While getting info from url \n ${ player_response.playabilityStatus.reason || player_response.playabilityStatus.messages[0]}`) @@ -83,8 +85,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 } From 6b0af0df09986269718a443d0f50370e1d0b5867 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Thu, 2 Sep 2021 09:24:29 +0530 Subject: [PATCH 2/4] Package updated --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27757fd..d35ca0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "play-dl", - "version": "0.7.3", + "version": "0.7.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "play-dl", - "version": "0.7.3", + "version": "0.7.6", "license": "MIT", "dependencies": { "got": "^11.8.2" diff --git a/package.json b/package.json index 625fcdb..aee4024 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "play-dl", - "version": "0.7.3", + "version": "0.7.6", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "main": "dist/index.js", "typings": "dist/index.d.ts", From 8e32f9bdf6663188477617b9f244949b0292bc7c Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Thu, 2 Sep 2021 09:38:45 +0530 Subject: [PATCH 3/4] Cookie Examples added --- examples/play - cookies.js | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/play - cookies.js 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); + From 40e9e7676b26f854a815b1de4dc03bd67f8f6925 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Thu, 2 Sep 2021 09:40:29 +0530 Subject: [PATCH 4/4] Conflicts solved --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index d35ca0a..766d1b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "play-dl", - "version": "0.7.6", + "version": "0.7.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "play-dl", - "version": "0.7.6", + "version": "0.7.5", "license": "MIT", "dependencies": { "got": "^11.8.2" diff --git a/package.json b/package.json index aee4024..92cc082 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "play-dl", - "version": "0.7.6", + "version": "0.7.5", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "main": "dist/index.js", "typings": "dist/index.d.ts",