Add firstResult param

Closes #200
Closes #186
This commit is contained in:
Androz2091 2020-12-22 10:13:23 +01:00
parent c46a7282bb
commit f9c6bf29fe
2 changed files with 8 additions and 4 deletions

View file

@ -137,9 +137,10 @@ class Player extends EventEmitter {
* @ignore * @ignore
* @param {Discord.Message} message * @param {Discord.Message} message
* @param {string} query * @param {string} query
* @param {boolean} firstResult
* @returns {Promise<Track>} * @returns {Promise<Track>}
*/ */
_searchTracks (message, query) { _searchTracks (message, query, firstResult) {
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
let tracks = [] let tracks = []
let updatedQuery = null let updatedQuery = null
@ -173,6 +174,8 @@ class Player extends EventEmitter {
if (tracks.length === 0) return this.emit('noResults', message, query) if (tracks.length === 0) return this.emit('noResults', message, query)
if (firstResult) resolve(tracks[0])
this.emit('searchResults', message, query, tracks) this.emit('searchResults', message, query, tracks)
const collector = message.channel.createMessageCollector((m) => m.author.id === message.author.id, { 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`. * 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 {Discord.Message} message Discord `message`
* @param {String|Track} query Search query or a valid `Track` object. * @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<void>} * @returns {Promise<void>}
* *
* @example * @example
* client.player.play(message, "Despacito"); * client.player.play(message, "Despacito");
*/ */
async play (message, query) { async play (message, query, firstResult) {
const isPlaying = this.isPlaying(message) const isPlaying = this.isPlaying(message)
if (this.util.isYTPlaylistLink(query)) { if (this.util.isYTPlaylistLink(query)) {
return this._handlePlaylist(message, query) return this._handlePlaylist(message, query)
@ -340,7 +344,7 @@ class Player extends EventEmitter {
} }
}, message.author, this) }, message.author, this)
} else { } else {
trackToPlay = await this._searchTracks(message, query) trackToPlay = await this._searchTracks(message, query, firstResult)
} }
if (trackToPlay) { if (trackToPlay) {
if (this.isPlaying(message)) { if (this.isPlaying(message)) {

2
typings/index.d.ts vendored
View file

@ -25,7 +25,7 @@ declare module 'discord-player' {
public isPlaying(message: Message): boolean; public isPlaying(message: Message): boolean;
public setFilters(message: Message, newFilters: Partial<Filters>): Promise<void>; public setFilters(message: Message, newFilters: Partial<Filters>): Promise<void>;
public play(message: Message, query: string | Track): Promise<void>; public play(message: Message, query: string | Track, firstResult: boolean): Promise<void>;
public pause(message: Message): void; public pause(message: Message): void;
public resume(message: Message): void; public resume(message: Message): void;
public stop(message: Message): void; public stop(message: Message): void;