From f9c6bf29fe22dc3db5f2f2ae4b218bbb46f350a8 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Tue, 22 Dec 2020 10:13:23 +0100 Subject: [PATCH] :sparkles: Add firstResult param Closes #200 Closes #186 --- src/Player.js | 10 +++++++--- typings/index.d.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Player.js b/src/Player.js index c7cde36..65e61e5 100644 --- a/src/Player.js +++ b/src/Player.js @@ -137,9 +137,10 @@ class Player extends EventEmitter { * @ignore * @param {Discord.Message} message * @param {string} query + * @param {boolean} firstResult * @returns {Promise} */ - _searchTracks (message, query) { + _searchTracks (message, query, firstResult) { return new Promise(async (resolve) => { let tracks = [] let updatedQuery = null @@ -173,6 +174,8 @@ class Player extends EventEmitter { if (tracks.length === 0) return this.emit('noResults', message, query) + if (firstResult) resolve(tracks[0]) + this.emit('searchResults', message, query, tracks) const collector = message.channel.createMessageCollector((m) => m.author.id === message.author.id, { @@ -312,12 +315,13 @@ class Player extends EventEmitter { * Play a track in the server. Supported query types are `keywords`, `YouTube video links`, `YouTube playlists links`, `Spotify track link` or `SoundCloud song link`. * @param {Discord.Message} message Discord `message` * @param {String|Track} query Search query or a valid `Track` object. + * @param {boolean} firstResult Whether the bot should play the first song found on youtube with the given query * @returns {Promise} * * @example * client.player.play(message, "Despacito"); */ - async play (message, query) { + async play (message, query, firstResult) { const isPlaying = this.isPlaying(message) if (this.util.isYTPlaylistLink(query)) { return this._handlePlaylist(message, query) @@ -340,7 +344,7 @@ class Player extends EventEmitter { } }, message.author, this) } else { - trackToPlay = await this._searchTracks(message, query) + trackToPlay = await this._searchTracks(message, query, firstResult) } if (trackToPlay) { if (this.isPlaying(message)) { diff --git a/typings/index.d.ts b/typings/index.d.ts index 6dbf463..65269b0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -25,7 +25,7 @@ declare module 'discord-player' { public isPlaying(message: Message): boolean; public setFilters(message: Message, newFilters: Partial): Promise; - public play(message: Message, query: string | Track): Promise; + public play(message: Message, query: string | Track, firstResult: boolean): Promise; public pause(message: Message): void; public resume(message: Message): void; public stop(message: Message): void;