Skip to content

Commit

Permalink
Updating spotify token, search limit fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Olebeh committed Oct 11, 2022
1 parent 7b02e85 commit c72f94d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "olebeh-music-player",
"version": "1.0.5",
"version": "1.0.6",
"description": "🎵 A high quality Discord Music Player that supports YouTube, Spotify and local files (for now)",
"main": "./dist/index.js",
"types": "./typings/index.d.ts",
Expand Down
21 changes: 15 additions & 6 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Player extends TypedEmitter<PlayerEvents> {
geniusAPIToken?: string
readonly voiceUtils = new VoiceUtils()
readonly client: Discord.Client<true>
private _spotifyToken = false
private _idleCooldowns: { [guildId: string]: NodeJS.Timer } = {}
private _emptyCooldowns: { [guildId: string]: NodeJS.Timer } = {}

Expand All @@ -28,7 +29,9 @@ export class Player extends TypedEmitter<PlayerEvents> {

this.geniusAPIToken = options?.geniusAPIToken
this.client = client

if (options?.authorization) playdl.setToken(options?.authorization)
if (options?.authorization?.spotify) this._spotifyToken = true
this._playerHandler()
}

Expand Down Expand Up @@ -234,7 +237,7 @@ export class Player extends TypedEmitter<PlayerEvents> {
let source = validation ? validation.split('_')[0] : undefined
let type = validation ? validation.split('_')[1] : undefined

const YouTubeSearchResult = async (video?: YouTubeVideo, playList?: YouTubePlayList) => {
const YouTubeSearchResult = async (videos?: YouTubeVideo[], playList?: YouTubePlayList) => {
const ytToTrack = (video: YouTubeVideo) => {
return new Track(this, {
title: video.title ?? `Unnamed track`,
Expand Down Expand Up @@ -273,8 +276,10 @@ export class Player extends TypedEmitter<PlayerEvents> {
})

playlist.tracks = tracks
} else if (video) {
tracks.push(ytToTrack(video))
} else if (videos) {
videos.slice(0, limit).forEach(video => {
tracks.push(ytToTrack(video))
})
}

return {
Expand Down Expand Up @@ -338,13 +343,17 @@ export class Player extends TypedEmitter<PlayerEvents> {
if (type === `video`) {
const tracks = await playdl.video_info(query)

return await YouTubeSearchResult(tracks.video_details)
return await YouTubeSearchResult([tracks.video_details])
} else if (type === `playlist`) {
const playlist = await playdl.playlist_info(query)

return await YouTubeSearchResult(undefined, playlist)
}
} else if (source === `sp`) {
if (playdl.is_expired() && this._spotifyToken) {
await playdl.refreshToken()
}

if (type === `track`) {
const tracks = await playdl.spotify(query) as SpotifyTrack

Expand Down Expand Up @@ -382,8 +391,8 @@ export class Player extends TypedEmitter<PlayerEvents> {
} as SearchResult
}

const videos = await playdl.search(query, { source: { youtube: 'video' } })
const videos = await playdl.search(query, { source: { youtube: 'video' }, limit })

return await YouTubeSearchResult(videos[0])
return await YouTubeSearchResult(videos)
}
}
2 changes: 1 addition & 1 deletion src/declarations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface PlayerEvents {
}

export interface PlayerOptions {
authorization: {
authorization?: {
spotify?: {
client_id: string
client_secret: string
Expand Down
27 changes: 11 additions & 16 deletions src/utils/StreamDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export class StreamDispatcher extends EventEmitter<VoiceEvents> {
this.voiceConnection.on(`stateChange`, async (_, newState) => {
if (newState.status === VoiceConnectionStatus.Disconnected) {
if (newState.reason === VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) {
try {
await entersState(this.voiceConnection, VoiceConnectionStatus.Connecting, this.connectionTimeout)
} catch {
try {
if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed) this.voiceConnection.destroy();
} catch (err) {
this.emit(`error`, err as AudioPlayerError);
await entersState(this.voiceConnection, VoiceConnectionStatus.Connecting, this.connectionTimeout).catch(() => {
if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed) {
try {
this.voiceConnection.destroy()
} catch (err) {
this.emit(`error`, err as AudioPlayerError)
}
}
}
})
} else if (this.voiceConnection.rejoinAttempts < 5) {
await new Promise((r) => setTimeout(r, (this.voiceConnection.rejoinAttempts + 1) * 5000).unref());
this.voiceConnection.rejoin();
Expand All @@ -87,23 +87,18 @@ export class StreamDispatcher extends EventEmitter<VoiceEvents> {
this.emit(`error`, err as AudioPlayerError);
}
}
} else if (newState.status === VoiceConnectionStatus.Destroyed) {

} else if (!this.readyLock && (newState.status === VoiceConnectionStatus.Connecting || newState.status === VoiceConnectionStatus.Signalling)) {
this.readyLock = true;
try {
await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, this.connectionTimeout)
} catch {
await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, this.connectionTimeout).catch(() => {
if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed) {
try {
this.voiceConnection.destroy();
} catch (err) {
this.emit(`error`, err as AudioPlayerError);
}
}
} finally {
this.readyLock = false;
}
})
this.readyLock = false;
}
});

Expand Down

0 comments on commit c72f94d

Please sign in to comment.