From 50d086bb5c9a659063c4a0153fecefe7d06d2718 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Sun, 1 Nov 2020 14:01:48 +0100 Subject: [PATCH] :bug: Fix everything --- package.json | 6 +++--- src/Player.js | 26 ++++++++++++++++++-------- src/Track.js | 10 +++++----- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 0d27b3d..d34f676 100644 --- a/package.json +++ b/package.json @@ -28,14 +28,14 @@ }, "homepage": "https://github.com/Androz2091/discord-player#readme", "dependencies": { - "discord-ytdl-core": "^4.0.2", + "discord-ytdl-core": "^4.1.3", "merge-options": "^2.0.0", "moment": "^2.27.0", "node-fetch": "^2.6.0", "soundcloud-scraper": "^2.0.0", "spotify-url-info": "^1.3.1", - "ytpl": "^1.0.1", - "ytsr": "^1.0.1" + "youtube-sr": "^1.0.4", + "ytpl": "^1.0.1" }, "devDependencies": { "@discordjs/opus": "^0.3.2", diff --git a/src/Player.js b/src/Player.js index 88e9de5..32bc5e2 100644 --- a/src/Player.js +++ b/src/Player.js @@ -1,6 +1,6 @@ const ytdl = require('discord-ytdl-core') const Discord = require('discord.js') -const ytsr = require('ytsr') +const ytsr = require('youtube-sr') const ytpl = require('ytpl') const spotify = require('spotify-url-info') const soundcloud = require('soundcloud-scraper') @@ -141,7 +141,7 @@ class Player extends EventEmitter { */ _searchTracks (message, query) { return new Promise(async (resolve) => { - const tracks = [] + let tracks = [] let updatedQuery = null let queryType = this.resolveQueryType(query) @@ -164,10 +164,9 @@ class Player extends EventEmitter { } if (queryType === 'youtube-video-keywords') { - await ytsr(updatedQuery || query).then((results) => { - if (results.items.length !== 0) { - const resultsVideo = results.items.filter((i) => i.type === 'video') - tracks.push(...resultsVideo.map((r) => new Track(r, message.author, null))) + await ytsr.search(updatedQuery || query, { type: 'video' }).then((results) => { + if (results.length !== 0) { + tracks = results.map((r) => new Track(r, message.author, null)) } }).catch(() => {}) } @@ -292,7 +291,17 @@ class Player extends EventEmitter { async _handlePlaylist (message, query) { const playlist = await ytpl(query).catch(() => {}) if (!playlist) return this.emit('noResults', message, query) - playlist.tracks = playlist.items.map((item) => new Track(item, message.author)) + playlist.tracks = playlist.items.map((item) => new Track({ + title: item.title, + description: item.description, + views: item.views, + duration: item.duration, + url: item.url, + thumbnail: item.thumbnail, + channel: { + name: item.author.name + } + }, message.author)) playlist.duration = playlist.tracks.reduce((prev, next) => prev + next.duration, 0) playlist.thumbnail = playlist.tracks[0].thumbnail playlist.requestedBy = message.author @@ -300,8 +309,9 @@ class Player extends EventEmitter { const queue = this._addTracksToQueue(message, playlist.tracks) this.emit('playlistAdd', message, queue, playlist) } else { - const track = new Track(playlist.tracks.shift(), message.author) + const track = playlist.tracks.shift() const queue = await this._createQueue(message, track).catch((e) => this.emit('error', message, e)) + this.emit('trackStart', message, queue.tracks[0]) this._addTracksToQueue(message, playlist.tracks) } } diff --git a/src/Track.js b/src/Track.js index 8cf520f..f76b38a 100644 --- a/src/Track.js +++ b/src/Track.js @@ -26,12 +26,12 @@ class Track { * The Youtube URL of the track * @type {string} */ - this.url = videoData.link ?? videoData.url + this.url = videoData.url /** * The video duration (formatted). * @type {string} */ - this.duration = videoData.duration + this.duration = videoData.duration || videoData.durationFormatted /** * The video description * @type {string} @@ -41,17 +41,17 @@ class Track { * The video thumbnail * @type {string} */ - this.thumbnail = videoData.thumbnail + this.thumbnail = typeof videoData.thumbnail === 'object' ? videoData.thumbnail.url : videoData.thumbnail /** * The video views * @type {?number} */ - this.views = videoData.views + this.views = parseInt(videoData.views) /** * The video channel * @type {string} */ - this.author = videoData.author.name + this.author = videoData.channel.name /** * The user who requested the track * @type {Discord.User?}