Spotify support & bug fixes (#40)

Co-authored-by: Androz2091 <androz2091@gmail.com>
This commit is contained in:
Snowflake 2020-07-04 17:08:49 +05:45 committed by GitHub
parent 5b6d1c496b
commit a0cdcbc461
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 139 deletions

View file

@ -28,16 +28,16 @@
},
"homepage": "https://github.com/Androz2091/discord-player#readme",
"dependencies": {
"@discordjs/opus": "^0.3.2",
"discord-ytdl-core": "^4.0.2",
"merge-options": "^2.0.0",
"node-fetch": "^2.6.0",
"simple-youtube-api": "^5.2.1",
"ytpl": "^0.1.21",
"spotify-url-info": "^1.3.1",
"ytpl": "^0.1.22",
"ytsr": "^0.1.15"
},
"devDependencies": {
"discord.js": "discordjs/discord.js",
"@discordjs/opus": "^0.3.2",
"discord.js": "^12.2.0",
"eslint": "^7.1.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",

View file

@ -2,7 +2,7 @@ const ytdl = require('discord-ytdl-core')
const Discord = require('discord.js')
const ytsr = require('ytsr')
const ytpl = require('ytpl')
const spotify = require('spotify-url-info')
const Queue = require('./Queue')
const Track = require('./Track')
@ -176,10 +176,15 @@ class Player {
}
}
}
const matchSpotifyURL = query.match(/https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-){22})/)
if (matchSpotifyURL) {
const spotifyData = await spotify.getPreview(query).catch(e => resolve([]))
query = `${spotifyData.artist} - ${spotifyData.track}`
}
// eslint-disable-next-line no-useless-escape
const matchURL = query.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/)
if (matchURL) {
query = matchURL[1]
const matchYoutubeURL = query.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/)
if (matchYoutubeURL) {
query = matchYoutubeURL[1]
}
ytsr(query, (err, results) => {
if (results.items.length < 1) return resolve([])
@ -814,10 +819,11 @@ class Player {
if (queue.stream) queue.stream.destroy()
queue.stream = newStream
queue.voiceConnection.play(newStream, {
type: 'opus'
type: 'opus',
bitrate: 'auto'
})
if (currentStreamTime) {
queue.voiceConnection.dispatcher.streamTime += currentStreamTime
queue.playing.streamTime += currentStreamTime
}
queue.voiceConnection.dispatcher.setVolumeLogarithmic(queue.calculatedVolume / 200)
// When the track starts
@ -826,6 +832,8 @@ class Player {
})
// When the track ends
queue.voiceConnection.dispatcher.on('finish', () => {
// reset streamTime
if (queue.repeatMode) queue.playing.streamTime = 0
// Play the next track
return this._playTrack(queue.guildID, false)
})

View file

@ -64,7 +64,7 @@ class Queue extends EventEmitter {
}
get calculatedVolume () {
return this.filters.bassboost ? this.volume + 40 : this.volume
return this.filters.bassboost ? this.volume + 50 : this.volume
}
}

View file

@ -56,6 +56,11 @@ class Track {
* @type {Queue}
*/
this.queue = queue
/**
* Stream time of the track (available on applying filters)
*/
this.streamTime = 0
}
/**