diff --git a/package.json b/package.json index 04951f93..7a7427fb 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,8 @@ "soundcloud-api-client": "0.0.9", "spotify-web-api-node": "^4.0.0", "utf-8-validate": "^5.0.2", - "yt-search": "^1.1.2", "ytdl-core": "^3.1.0", + "ytsr": "^0.1.15", "zlib-sync": "^0.1.6" } } diff --git a/src/commands/play.js b/src/commands/play.js index 2fa8f8ea..b576a799 100644 --- a/src/commands/play.js +++ b/src/commands/play.js @@ -1,7 +1,8 @@ const YouTube = require("simple-youtube-api"); const SpotifyApi = require("spotify-web-api-node"); -const search = require("yt-search"); -const ytdl = require("ytdl-core") +const ytdl = require("ytdl-core"); +const ytsr = require("ytsr"); +const ms = require("ms"); module.exports = { name: "play", @@ -43,25 +44,18 @@ module.exports = { if (!voiceChannel.speakable) return msg.channel.send(client.messages.noPermsSpeak); if (ytdl.validateURL(url)) { - client.funcs.handleVideo({ - url: url - }, - msg, - voiceChannel, - client, - false, - "ytdl" - ); + client.funcs.handleVideo(url, msg, voiceChannel, client, false, "ytdl"); } else if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) { const playlistId = url.split("/playlist/")[1].split("?")[0]; spotify.getPlaylist(playlistId).then( async function (data) { - searchVideos(data, client, msg, voiceChannel) + searchVideos(data, client, msg, voiceChannel); }, function (err) { console.log(err); msg.channel.send(client.messages.noResultsSpotify); - }); + } + ); } else if ( url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/) ) { @@ -71,7 +65,7 @@ module.exports = { for (const video of Object.values(videos)) { const video2 = await youtube.getVideoByID(video.id); await client.funcs.handleVideo( - video2, + video2.url, msg, voiceChannel, client, @@ -85,19 +79,24 @@ module.exports = { ); return lmsg.edit(message); } else { - search(searchString, function (err, res) { - if (err) return console.log(err); - if (res.videos.length === 0) - return msg.channel.send(client.messages.noResults); - client.funcs.handleVideo( - res.videos[0], - msg, - voiceChannel, - client, - false, - "ytdl" - ); - }); + ytsr( + searchString, { + limit: 5, + }, + function (err, res) { + if (err) console.log(err); + if (!res.items[0]) return msg.channel.send(client.messages.noResults); + const videoResults = res.items.filter(item => item.type === "video"); + client.funcs.handleVideo( + videoResults[0].link, + msg, + voiceChannel, + client, + false, + "ytdl" + ); + } + ); } }, }; @@ -108,38 +107,45 @@ async function searchVideos(data, client, msg, voiceChannel) { for (let i = 0; data.body.tracks.items.length > i; i++) { const track = await data.body.tracks.items[i].track; await client.funcs.sleep(250); - search( - `${track.artists[0].name} ${track.name} audio`, + ytsr( + `${track.artists[0].name} ${track.name} audio`, { + limit: 5, + }, async function (err, res) { if (err) return console.log(err); - if (res.videos.length === 0) { - search( - `${track.artists[0].name} ${track.name} lyrics`, + if (!res.items[0]) { + ytsr( + `${track.artists[0].name} ${track.name} lyrics`, { + limit: 5, + }, async function (err, res) { if (err) return console.log(err); - if (res.videos.length === 0) { - search( - `${track.artists[0].name} ${track.name}`, + if (!res.items[0]) { + ytsr( + `${track.artists[0].name} ${track.name}`, { + limit: 5, + }, async function (err, res) { - if (err) return console.log(err); - if (res.videos.length === 0) { + if (err) console.log(err); + if (!res.items[0]) { failed++; - return; } - await client.funcs.handleVideo( - res.videos[0], + const videoResults = res.items.filter(item => item.type === "video"); + client.funcs.handleVideo( + videoResults[0].link, msg, voiceChannel, client, - true, + false, "ytdl" ); } ); return; } + const videoResults = res.items.filter(item => item.type === "video"); await client.funcs.handleVideo( - res.videos[0], + videoResults[0].link, msg, voiceChannel, client, @@ -151,8 +157,9 @@ async function searchVideos(data, client, msg, voiceChannel) { failed++; return; } + const videoResults = res.items.filter(item => item.type === "video"); await client.funcs.handleVideo( - res.videos[0], + videoResults[0].link, msg, voiceChannel, client, @@ -164,15 +171,12 @@ async function searchVideos(data, client, msg, voiceChannel) { } let message; if (failed === 0) { - message = client.messages.playlistAdded.replace( - "%TITLE%", - data.body.name - ); + message = client.messages.playlistAdded.replace("%TITLE%", data.body.name); } else { message = `${client.messages.playlistAdded.replace( - "%TITLE%", - data.body.name - )}\n${client.messages.failedToLoad + failed}`; + "%TITLE%", + data.body.name + )}\n${client.messages.failedToLoad + failed}`; } lmsg.edit(message); } \ No newline at end of file diff --git a/src/commands/search.js b/src/commands/search.js index 3a010e89..b14f01d4 100644 --- a/src/commands/search.js +++ b/src/commands/search.js @@ -1,4 +1,4 @@ -const yts = require('yt-search'); +const ytsr = require('ytsr'); const he = require('he'); module.exports = { @@ -22,10 +22,13 @@ module.exports = { if (voiceChannel.full) return msg.channel.send(client.messages.channelFull); if (!voiceChannel.joinable) return msg.channel.send(client.messages.noPermsConnect); if (!voiceChannel.speakable) return msg.channel.send(client.messages.noPermsSpeak); - yts(searchString, async function (err, res) { + ytsr(searchString, { + limit: 20, + }, async function (err, res) { if (err) return console.log(err); - if (res.videos.length === 0) return msg.channel.send(client.messages.noResults); - const videos = res.videos.slice(0, 10); + if (!res.items[0]) return msg.channel.send(client.messages.noResults); + const videoResults = res.items.filter(item => item.type === "video"); + const videos = videoResults.slice(0, 10); let index = 0; const embed = new Discord.MessageEmbed() .setTitle(client.messages.songSelection) @@ -44,7 +47,7 @@ module.exports = { return msg.channel.send(client.messages.cancellingVideoSelection); } const videoIndex = parseInt(response.first().content) - 1; - return client.funcs.handleVideo(videos[videoIndex], msg, voiceChannel, client, false, "ytdl"); + return client.funcs.handleVideo(videos[videoIndex].link, msg, voiceChannel, client, false, "ytdl"); }) } }; \ No newline at end of file diff --git a/src/commands/searchSimilar.js b/src/commands/searchSimilar.js index 5a9bc00c..e3074e1d 100644 --- a/src/commands/searchSimilar.js +++ b/src/commands/searchSimilar.js @@ -49,9 +49,8 @@ module.exports = { if (songs.length !== 0) { const lmsg = await msg.channel.send(client.messages.loadingSongs); for (let i = 0; i < songs.length; i++) { - client.funcs.handleVideo({ - url: `https://www.youtube.com/watch?v=${songs[i].youtubeId}` - }, msg, voiceChannel, client, true, "ytdl"); + client.funcs.handleVideo( + `https://www.youtube.com/watch?v=${songs[i].youtubeId}`, msg, voiceChannel, client, true, "ytdl"); } const message = client.messages.songsAdded.replace( "%AMOUNT%", diff --git a/src/struct/funcs/handleVideo.js b/src/struct/funcs/handleVideo.js index dc41c1d0..ba6e3f0a 100644 --- a/src/struct/funcs/handleVideo.js +++ b/src/struct/funcs/handleVideo.js @@ -9,10 +9,10 @@ module.exports = async function ( playlist, type ) { - const songInfo = await ytdl.getInfo(resource.url).catch(err => console.log(err)); + const songInfo = await ytdl.getInfo(resource).catch(err => console.log(err)); const song = { title: Discord.Util.escapeMarkdown(songInfo.videoDetails.title), - url: resource.url, + url: resource, author: msg.author, type: type, info: songInfo.videoDetails diff --git a/src/struct/funcs/urlMatch.js b/src/struct/funcs/urlMatch.js index ad698ac9..0e94d1ac 100644 --- a/src/struct/funcs/urlMatch.js +++ b/src/struct/funcs/urlMatch.js @@ -5,7 +5,7 @@ module.exports = async function (client, msg, youtube, voiceChannel, url) { const videos = await playlist.getVideos(); for (const video of Object.values(videos)) { const video2 = await youtube.getVideoByID(video.id); - await client.funcs.handleVideo(video2, msg, voiceChannel, client, true); + await client.funcs.handleVideo(video2.url, msg, voiceChannel, client, true); } let message; message = client.messages.playlistAdded.replace("%TITLE%", playlist.title);