-
Notifications
You must be signed in to change notification settings - Fork 3
/
QueueManager.ts
362 lines (308 loc) · 10 KB
/
QueueManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* eslint-disable @typescript-eslint/ban-types */
import type { VoiceConnection } from "@discordjs/voice"
import type { SearchResponseAll } from "soundcloud-downloader/src/search"
import type { TrackInfo as SCDLTrackInfo } from "soundcloud-downloader/src/info"
import type { AudioManagerOptions, AudioManagerEvents } from "../audio/AudioManager"
import type { PlaylistVideo, YoutubeSearchVideoInfo, YoutubeVideoDetails } from "youtube-scrapper"
import { Track } from "./Track"
import { QueueHandler } from "./QueueHandler"
import { TypedEmitter } from "tiny-typed-emitter"
import { AudioManager } from "../audio/AudioManager"
import { QueueManagerValidation as validation } from "../validation"
import { getVideoInfo, getPlaylistInfo, search } from "youtube-scrapper"
/**
* A RegExp instance to identify youtube playlist url
*/
export const PLAYLIST_URL = /^(?:(?:http|https):\/\/)?(?:www\.)?youtube\.com\/playlist\?list=((?:PL|UU|LL|RD|OL)[\w-]{16,41})$/
/**
* A RegExp instance to identify youtube video url
*/
export const VIDEO_URL = /^(?:(?:http|https):\/\/)?(?:youtu\.be\/|(?:(?:www|m)\.)?youtube\.com\/(?:watch\?v=|v\/|embed\/))([\w-]{11})$/
/**
* Track metadata of youtube search result
*/
export type youtubeMetadata = PlaylistVideo | YoutubeSearchVideoInfo | YoutubeVideoDetails
/**
* Track metadata of soundcloud search result
*/
export type soundcloudMetadata = SCDLTrackInfo
/**
* The youtube search result type
*/
export type youtubeSearchResultType = "video" | "playlist" | "search"
/**
* The soundcloud search result type
*/
export type soundcloudSearchResultType = "track" | "set" | "search"
/**
* The youtube video types
*/
export interface youtubeVideoType {
video: YoutubeVideoDetails
playlist: PlaylistVideo
search: YoutubeSearchVideoInfo
}
/**
* The youtube search options
*/
export interface youtubeSearchOptions {
query: string
fullPlaylist?: boolean
}
/**
* The soundcloud search options
*/
export interface soundcloudSearchOptions {
query: string
searchLimit?: number
searchOffset?: number
setLimit?: number
}
/**
* The youtube search result
*/
export interface youtubeSearchResult<T extends youtubeSearchResultType> {
type: T,
tracks: Track<youtubeVideoType[T]>[]
}
/**
* The soundcloud search result
*/
export interface soundcloudSearchResult {
type: soundcloudSearchResultType,
tracks: Track<soundcloudMetadata>[]
}
/**
* The AudioManager-like that can be passed to queue manager
*/
export type AudioManagerResolvable = AudioManager | AudioManagerOptions
export interface QueueManagerEvents {
/**
* {@link AudioManagerEvents.audioStart | AudioStartCallback}
*/
audioStart: AudioManagerEvents["audioStart"]
/**
* {@link AudioManagerEvents.audioEnd | AudioEndCallback}
*/
audioEnd: AudioManagerEvents["audioEnd"]
/**
* {@link AudioManagerEvents.audioError | AudioErrorCallback}
*/
audioError: AudioManagerEvents["audioError"]
/**
* @param guildID The guildID of the linked connection in queue player
*/
queueStart(guildID: string): void
/**
* @param guildID The guildID of the linked connection in queue player
*/
queueEnd(guildID: string): void
}
/**
* The manager of queue handler
*
* Example:
* ```ts
* import { QueueManager, CacheManagerImpl, youtubeMetadata, soundcloudMetadata } from "discord-media-player"
* const manager = new QueueManager<youtubeMetadata | soundcloudMetadata>({
* //cache is optional
* cache: new CacheManagerImpl()
* })
* ...
* ```
*/
export class QueueManager<TM extends object = {}, M = unknown> extends TypedEmitter<QueueManagerEvents> {
/**
* Emitted whenever an audio is started playing
*
* Listener must implement {@link AudioManagerEvents.audioStart | AudioStartCallback}
* @event
*/
static AUDIO_START = "audioStart"
/**
* Emitted whenever an audio is ended after playing
*
* Listener must implement {@link AudioManagerEvents.audioEnd | AudioEndCallback}
* @event
*/
static AUDIO_END = "audioEnd"
/**
* Emitted whenever an error is thrown while getting audio source before playing
*
* Listener must implement {@link AudioManagerEvents.audioError | AudioErrorCallback}
* @event
*/
static AUDIO_ERROR = "audioError"
/**
* Emitted whenever a queue is starting to play audio
*
* Listener must implement {@link QueueManagerEvents.queueStart | QueueStartCallback}
* @event
*/
static QUEUE_START = "queueStart"
/**
* Emitted whenever a queue is ended
*
* Listener must implement {@link QueueManagerEvents.queueEnd | QueueEndCallback}
* @event
*/
static QUEUE_END = "queueEnd"
/**
* The audio manager of the queue
*/
public readonly audioManager: AudioManager
/**
* The queue manager metadata (if any)
*/
public readonly metadata?: M
/**
* @internal
*/
private _handlers = new Map<string, QueueHandler<TM>>()
/**
* @param manager The audio manager resolvable
*/
constructor(manager: AudioManagerResolvable, metadata?: M) {
super()
validation.validateAudioManager(manager as AudioManager)
this.metadata = metadata
if (manager instanceof AudioManager) this.audioManager = manager
else this.audioManager = new AudioManager(manager)
this.audioManager.on("audioStart", this.emit.bind(this, "audioStart"))
this.audioManager.on("audioEnd", this.emit.bind(this, "audioEnd"))
this.audioManager.on("audioError", this.emit.bind(this, "audioError"))
}
/**
* Get queue handler from list if exist, otherwise create new
* @param connection The voice connection
* @returns The queue handler
*/
getHandler(connection: VoiceConnection): QueueHandler<TM> {
validation.validateConnection(connection)
const guildID = connection.joinConfig.guildId
let handler = this._handlers.get(guildID)
if (!handler) {
const player = this.audioManager.getPlayer(connection)
handler = new QueueHandler(this, player)
this._handlers.set(guildID, handler)
}
return handler
}
/**
* Delete queue handler from list
* @param connection The voice connection
* @returns false if failed or doesn't exist, true if deleted
*/
deleteHandler(connection: VoiceConnection): boolean {
validation.validateConnection(connection)
const guildID = connection.joinConfig.guildId
if (!this._handlers.has(guildID)) return false
const success = this._handlers.delete(guildID)
if (success) this.audioManager.deletePlayer(connection)
return success
}
/**
* @internal
*/
_deleteHandlerIfExist(guildID: string): void {
if (this._handlers.has(guildID)) this._handlers.delete(guildID)
}
/**
* Search for a youtube track
* @param options The youtube search options
* @returns The youtube search result
*/
async youtubeSearch(options: youtubeSearchOptions): Promise<youtubeSearchResult<youtubeSearchResultType>> {
validation.validateYoutubeSearchOptions(options)
const tracks: Track<youtubeMetadata>[] = []
const type: youtubeSearchResultType = PLAYLIST_URL.test(options.query)
? "playlist"
: VIDEO_URL.test(options.query)
? "video"
: "search"
if (type === "video") {
const videoID = VIDEO_URL.exec(options.query)[1]
const { details } = await getVideoInfo(`https://www.youtube.com/watch?v=${videoID}`)
tracks.push(new Track({
sourceType: 0,
urlOrLocation: details.url,
metadata: details
}))
} else if (type === "playlist") {
const playlistID = PLAYLIST_URL.exec(options.query)[1]
const playlist = await getPlaylistInfo(`https://www.youtube.com/playlist?list=${playlistID}`, { full: options.fullPlaylist ?? false })
for (const video of playlist.tracks) {
tracks.push(new Track({
sourceType: 0,
urlOrLocation: video.url,
metadata: video
}))
}
} else {
const searchResult = await search(options.query)
for (const video of searchResult.videos) {
tracks.push(new Track({
sourceType: 0,
urlOrLocation: video.url,
metadata: video
}))
}
}
return { type, tracks }
}
/**
* Search for a soundcloud track
* @param options The soundcloud search options
* @returns The soundcloud search result
*/
async soundcloudSearch(options: soundcloudSearchOptions): Promise<soundcloudSearchResult> {
validation.validateSoundcloudSearchOptions(options)
const { soundcloud } = this.audioManager
const tracks: Track<soundcloudMetadata>[] = []
const type: soundcloudSearchResultType = soundcloud.isPlaylistURL(options.query)
? "set"
: soundcloud.isValidUrl(options.query)
? "track"
: "search"
if (type === "track") {
const track = await soundcloud.getInfo(options.query)
tracks.push(new Track({
sourceType: 1,
urlOrLocation: track.permalink_url,
metadata: track
}))
} else if (type === "set") {
const set = await soundcloud.getSetInfo(options.query)
for (const track of set.tracks) {
if (tracks.length >= options.setLimit) break
tracks.push(new Track({
sourceType: 1,
urlOrLocation: track.permalink_url,
metadata: track
}))
}
} else {
const searchResult = await soundcloud.search({
query: options.query,
limit: options.searchLimit ?? 1,
offset: options.searchOffset ?? 0
})
// eslint-disable-next-line no-inner-declarations
async function getNext(searchResponse: SearchResponseAll) {
for (const track of searchResponse.collection) {
if (tracks.length >= options.searchLimit) break
tracks.push(new Track({
sourceType: 1,
urlOrLocation: track.permalink_url,
metadata: track as soundcloudMetadata
}))
}
if (tracks.length < options.searchLimit && searchResponse.next_href) await getNext(await soundcloud.search({
nextHref: searchResponse.next_href
}))
}
await getNext(searchResult)
}
return { type, tracks }
}
}