🐛 Fix everything

This commit is contained in:
Androz2091 2020-11-01 14:01:48 +01:00
parent 6ff0d6ad42
commit 50d086bb5c
3 changed files with 26 additions and 16 deletions

View file

@ -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",

View file

@ -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)
}
}

View file

@ -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?}