Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (69 loc) · 2.13 KB
/
Copy pathindex.js
File metadata and controls
87 lines (69 loc) · 2.13 KB
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
import BaseModule from './structures/BaseModule.js'
import LavaTrack from './structures/LavaTrack.js'
import ResolvableTrack from './structures/ResolvableTrack.js'
export default class TrackResolver extends BaseModule {
_cache = new Map();
/**
* @param {Main} main
*/
constructor(main) {
super(main);
this.register(TrackResolver, {
name: 'trackResolver',
requires: ['lavalink']
});
Object.assign(this, { ResolvableTrack });
}
/**
* The default resolve function, resolves through LavaLink
* @private
* @param {URL} url
*/
async _resolve(url) {
const data = await this.modules.lavalink.conn.getNode().rest.resolve(url.toString());
if (!data) return { data };
if (data.type === 'PLAYLIST') {
const tracks = [];
data.tracks.forEach((track) => tracks.push(new LavaTrack(track)));
this._m.emit('playlistPlayed');
return { type: 'playlist', data: tracks };
}
return { type: 'song', data: new LavaTrack(data.tracks[0])};
}
/**
* Checks if the arguments given are valid inputs to be resolved to a Track
* @param {string[]} args
*/
isValidResolvable(args) {
if (args.length > 1) return false;
try {
new URL(args[0]);
return true;
}
catch (e) {
return false;
}
}
/**
* @param {string} moduleName The modulename of the resolver
* @param {string[]} hostnames The supported hostnames this resolver will handle
*/
registerResolver(moduleName, hostnames) {
const module = this.modules[moduleName];
for (const host of hostnames) {
this._cache.set(host, module);
}
}
/**
* @param {string} url
* @returns {Promise<TrackObject|*>}
*/
resolve(url) {
const urlInstance = new URL(url);
const resolver = this._cache.get(urlInstance.hostname);
if (resolver) {
return resolver.resolve(urlInstance);
}
return this._resolve(urlInstance);
}
}