-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (47 loc) · 1.94 KB
/
Copy pathindex.js
File metadata and controls
60 lines (47 loc) · 1.94 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
import BaseModule from './structures/module/BaseModule.js'
import SpotifyAPI from './structures/api/SpotifyAPI.js'
import { HostNames } from './util/Constants.js'
export default class TrackSpotify extends BaseModule {
/**
* @param {Main} main
*/
constructor(main) {
super(main);
this.register(TrackSpotify, {
name: 'trackSpotify',
requires: ['trackResolver']
});
}
/**
* @private
* @param {string} pathname
*/
async _resolve(pathname) {
if (pathname.includes('/track/')) {
return { type: 'song', data: new this.resolvableTrack(this._m, (await this.spotify.getTrack(pathname.split('/track/')[1])).body)};
}
const isAlbum = pathname.includes('/album/');
const isArtist = pathname.includes('/artist/');
const data = isAlbum
? (await this.spotify.getAlbum(pathname.split('/album/')[1])).body
: isArtist
? (await this.spotify.getArtistTopTracks(pathname.split('/artist/')[1])).body
: (await this.spotify.getPlaylist(pathname.split('/playlist/')[1])).body;
const trackList = [];
data.tracks.items.forEach((item) => trackList.push(new this.resolvableTrack(this._m, (isAlbum || isArtist) ? item : item.track, isAlbum ? data.images[0]?.url : null)));
this._m.emit(isAlbum ? 'albumPlayed' : isArtist ? 'artistPlayed' : 'playlistPlayed');
return { type: isAlbum ? 'album' : isArtist ? 'artist top 10' : 'playlist', data: trackList };
}
async init() {
this.spotify = new SpotifyAPI(this._m, this.auth.credentials.api.spotify);
this.resolvableTrack = (await import('./structures/track/SpotifyTrack.js')).default;
this.modules.trackResolver.registerResolver(this.name, HostNames);
return true;
}
/**
* @param {URL} url
*/
resolve(url) {
return this._resolve(url.pathname);
}
}