Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit 6c77425

Browse files
committed
fix: thumbnail error when getting album/playlist
1 parent 5707dea commit 6c77425

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/__test__/index.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@ import { SpotifyPlugin } from "..";
22

33
test.todo("Validate Options");
44
test.todo("SpotifyPlugin#play()");
5-
test("SpotifyPlugin#validate()", () => {
5+
test("SpotifyPlugin#validate()", async () => {
66
const { validate } = new SpotifyPlugin();
77
const validUrls = [
8-
"https://open.spotify.com/artist/3FwYnbtGNt8hJfjNuOfpeG?si=d4c1d832636c4bb1",
98
"https://open.spotify.com/album/5Gu0Ldddj2f6a0q5gitIok?si=781b3339b2bc4015",
109
"https://open.spotify.com/track/6Fbsn9471Xd0vVsMWwhePh?si=f992e1fe1f714674",
1110
"https://open.spotify.com/playlist/37i9dQZEVXbLdGSmz6xilI?si=28a3d4be17304ade",
1211
];
13-
validUrls.forEach(url => {
14-
expect(validate(url)).resolves.toBe(true);
15-
});
12+
await Promise.all(validUrls.map(url => expect(validate(url)).resolves.toBe(true)));
1613
const invalidUrls = [
14+
"https://open.spotify.com/artist/3FwYnbtGNt8hJfjNuOfpeG?si=d4c1d832636c4bb1",
1715
"https://open.spotify.com/show/6vcyNclFEFyS2pVSKFEWJo?si=893aa44bd6504cf9",
1816
"https://open.spotify.com/episode/42wDq6xhm7vbqD3glD1JSd?si=6016d24bf1b54793",
1917
"https://open.spotify.com/not-a-type/42wDq6xhm7vbqD3glD1JSd?si=6016d24bf1b54793",
2018
"https://open.spotify.com/",
2119
"https://www.youtube.com/watch?v=fzcIk6zN-M4",
2220
"not a url",
2321
];
24-
invalidUrls.forEach(url => {
25-
expect(validate(url)).resolves.toBe(false);
26-
});
22+
await Promise.all(invalidUrls.map(url => expect(validate(url)).resolves.toBe(false)));
2723
});

src/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CustomPlugin, DisTubeError, Playlist, Song, checkInvalidKey } from "dis
66
import type { VoiceBasedChannel } from "discord.js";
77
import type { PlayOptions, PlaylistInfo, Queue, SearchResult } from "distube";
88

9-
const SUPPORTED_TYPES = ["album", "artist", "playlist", "track"];
9+
const SUPPORTED_TYPES = ["album", "playlist", "track"];
1010
const API = new SpotifyWebApi();
1111
const spotify = SpotifyUrlInfo(fetch);
1212
let expirationTime = 0;
@@ -22,6 +22,7 @@ declare type SpotifyPluginOptions = {
2222

2323
type Falsy = undefined | null | false | 0 | "";
2424
const isTruthy = <T>(x: T | Falsy): x is T => Boolean(x);
25+
type ClassMethods<T> = { [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never }[keyof T];
2526

2627
const refreshAPIToken = async () => {
2728
if (expirationTime <= Date.now() - 60000) {
@@ -46,7 +47,7 @@ const getItems = async (data: any): Promise<any[]> => {
4647
})
4748
).body;
4849
} catch (e: any) {
49-
process.emitWarning(`${e?.body?.message}`, "SpotifyAPI");
50+
process.emitWarning(`${e?.body?.message}`, "SpotifyApi");
5051
process.emitWarning("There is a Spotify API error, max songs of Spotify playlist is 100.", "SpotifyPlugin");
5152
break;
5253
}
@@ -55,7 +56,8 @@ const getItems = async (data: any): Promise<any[]> => {
5556
return items;
5657
};
5758

58-
const getAPI = (method: string, ...args: any) => (<any>API)[method](...args).then((r: any) => r.body);
59+
const getAPI = <T extends ClassMethods<typeof API>>(method: T, ...args: Parameters<typeof API[T]>) =>
60+
(<any>API[method])(...args).then((r: any) => r.body);
5961

6062
const getDataWithAPI = async (url: string) => {
6163
const parsedURL = parseSpotifyURI(url);
@@ -72,10 +74,6 @@ const getDataWithAPI = async (url: string) => {
7274
data = await getAPI("getAlbum", id);
7375
data.tracks = await getAPI("getAlbumTracks", id, { limit: 50 });
7476
break;
75-
case "artist":
76-
data = await getAPI("getArtist", id);
77-
data.tracks = (await getAPI("getArtistTopTracks", id, "US")).tracks;
78-
break;
7977
case "playlist":
8078
data = await getAPI("getPlaylist", id);
8179
data.tracks = await getAPI("getPlaylistTracks", id, { limit: 100 });
@@ -100,21 +98,31 @@ export class SpotifyPlugin extends CustomPlugin {
10098
checkInvalidKey(options, ["parallel", "emitEventsAfterFetching", "api"], "SpotifyPluginOptions");
10199
this.parallel = options.parallel ?? true;
102100
if (typeof this.parallel !== "boolean") {
103-
throw new DisTubeError("INVALID_TYPE", "boolean", this.parallel, "parallel");
101+
throw new DisTubeError("INVALID_TYPE", "boolean", this.parallel, "SpotifyPluginOptions.parallel");
104102
}
105103
this.emitEventsAfterFetching = options.emitEventsAfterFetching ?? false;
106104
if (typeof this.emitEventsAfterFetching !== "boolean") {
107-
throw new DisTubeError("INVALID_TYPE", "boolean", this.emitEventsAfterFetching, "emitEventsAfterFetching");
105+
throw new DisTubeError(
106+
"INVALID_TYPE",
107+
"boolean",
108+
this.emitEventsAfterFetching,
109+
"SpotifyPluginOptions.emitEventsAfterFetching",
110+
);
108111
}
109112
API.setAccessToken("");
110113
if (options.api !== undefined && (typeof options.api !== "object" || Array.isArray(options.api))) {
111114
throw new DisTubeError("INVALID_TYPE", ["object", "undefined"], options.api, "api");
112115
} else if (options.api) {
113116
if (typeof options.api.clientId !== "string") {
114-
throw new DisTubeError("INVALID_TYPE", "string", options.api.clientId, "api.clientId");
117+
throw new DisTubeError("INVALID_TYPE", "string", options.api.clientId, "SpotifyPluginOptions.api.clientId");
115118
}
116119
if (typeof options.api.clientSecret !== "string") {
117-
throw new DisTubeError("INVALID_TYPE", "string", options.api.clientSecret, "api.clientSecret");
120+
throw new DisTubeError(
121+
"INVALID_TYPE",
122+
"string",
123+
options.api.clientSecret,
124+
"SpotifyPluginOptions.api.clientSecret",
125+
);
118126
}
119127
API.setClientId(options.api.clientId);
120128
API.setClientSecret(options.api.clientSecret);
@@ -163,7 +171,7 @@ export class SpotifyPlugin extends CustomPlugin {
163171
await DT.play(voiceChannel, result, options);
164172
} else {
165173
const name = data.name;
166-
const thumbnail = data.images[0]?.url;
174+
const thumbnail = (data.coverArt?.sources || data.images)?.[0]?.url;
167175
const queries: string[] = (await getItems(data))
168176
.map(item => {
169177
const track = item.track || item;

0 commit comments

Comments
 (0)