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

Commit 650baec

Browse files
committed
feat: easier to override youtube search
1 parent 128c070 commit 650baec

File tree

6 files changed

+95
-69
lines changed

6 files changed

+95
-69
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,27 @@ new SpotifyPlugin({
6666
},
6767
});
6868
```
69+
70+
##### Use SoundCloudPlugin to search instead of YouTube
71+
72+
```ts
73+
import { DisTube } from "distube";
74+
import { SpotifyPlugin } from "@distube/spotify";
75+
import { SoundCloudPlugin } from "@distube/soundcloud";
76+
77+
const scPlugin = new SoundCloudPlugin();
78+
79+
class NewSpotifyPlugin extends SpotifyPlugin {
80+
override async search(query: string, metadata: any) {
81+
try {
82+
return new Song((await scPlugin.search(query, { limit: 1 }))[0], { metadata });
83+
} catch {
84+
return null;
85+
}
86+
}
87+
}
88+
89+
const distube = new DisTube(client, {
90+
plugins: [new NewSpotifyPlugin(), scPlugin],
91+
});
92+
```

bun.lockb

-5.88 KB
Binary file not shown.

bunfig.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[install]
2+
dev = true
3+
optional = false
4+
peer = false

package.json

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,64 @@
11
{
22
"name": "@distube/spotify",
33
"version": "1.5.2",
4-
"description": "A DisTube custom plugin for supporting Spotify.",
5-
"main": "./dist/index.js",
6-
"typings": "./dist/index.d.ts",
7-
"exports": "./dist/index.js",
8-
"directories": {
9-
"lib": "src"
10-
},
11-
"files": [
12-
"dist"
13-
],
14-
"scripts": {
15-
"test": "jest",
16-
"lint": "eslint .",
17-
"lint:fix": "eslint . --fix",
18-
"prettier": "prettier --write \"**/*.{js,ts,json,yml,yaml,md}\"",
19-
"build": "tsup",
20-
"build:check": "tsc --noEmit",
21-
"update": "ncu -u && bun update",
22-
"postinstall": "husky install",
23-
"prepack": "bun run build && pinst -d",
24-
"postpack": "pinst -e"
25-
},
4+
"author": "Skick",
265
"repository": {
276
"type": "git",
287
"url": "git+https://github.com/distubejs/spotify.git"
298
},
30-
"keywords": [
31-
"distube",
32-
"plugin",
33-
"spotify",
34-
"discord",
35-
"music"
36-
],
37-
"author": "Skick",
38-
"license": "MIT",
39-
"bugs": {
40-
"url": "https://github.com/distubejs/spotify/issues"
9+
"main": "./dist/index.js",
10+
"dependencies": {
11+
"spotify-uri": "^4.0.1",
12+
"spotify-url-info": "^3.2.13",
13+
"spotify-web-api-node": "^5.0.2",
14+
"undici": "^6.9.0"
4115
},
42-
"homepage": "https://github.com/distubejs/spotify#readme",
4316
"devDependencies": {
44-
"@commitlint/cli": "^18.4.3",
45-
"@commitlint/config-conventional": "^18.4.3",
46-
"@discordjs/opus": "^0.9.0",
17+
"@commitlint/cli": "^19.2.0",
18+
"@commitlint/config-conventional": "^19.1.0",
4719
"@discordjs/voice": "^0.16.1",
4820
"@jest/globals": "^29.7.0",
4921
"@types/spotify-web-api-node": "^5.0.11",
50-
"@typescript-eslint/eslint-plugin": "^6.16.0",
51-
"@typescript-eslint/parser": "^6.16.0",
52-
"commitlint": "^18.4.3",
22+
"@typescript-eslint/eslint-plugin": "^7.2.0",
23+
"@typescript-eslint/parser": "^7.2.0",
24+
"commitlint": "^19.2.0",
5325
"discord.js": "^14.14.1",
5426
"distube": "^4.1.1",
55-
"eslint": "^8.56.0",
27+
"eslint": "^8.57.0",
5628
"eslint-config-distube": "^1.6.4",
5729
"eslint-config-prettier": "^9.1.0",
5830
"eslint-plugin-deprecation": "^2.0.0",
59-
"eslint-plugin-jsdoc": "^46.9.1",
60-
"husky": "^8.0.3",
31+
"eslint-plugin-jsdoc": "^48.2.1",
32+
"husky": "^9.0.11",
6133
"jest": "^29.7.0",
6234
"nano-staged": "^0.8.0",
6335
"pinst": "^3.0.0",
64-
"prettier": "^3.1.1",
65-
"ts-jest": "^29.1.1",
66-
"tsup": "^8.0.1",
67-
"typescript": "^5.3.3"
36+
"prettier": "^3.2.5",
37+
"ts-jest": "^29.1.2",
38+
"tsup": "^8.0.2",
39+
"typescript": "^5.4.2"
6840
},
69-
"dependencies": {
70-
"spotify-uri": "^4.0.1",
71-
"spotify-url-info": "^3.2.10",
72-
"spotify-web-api-node": "^5.0.2",
73-
"undici": "^6.2.1"
41+
"exports": "./dist/index.js",
42+
"bugs": {
43+
"url": "https://github.com/distubejs/spotify/issues"
7444
},
75-
"peerDependencies": {
76-
"distube": "^3.3.1||4"
45+
"description": "A DisTube custom plugin for supporting Spotify.",
46+
"directories": {
47+
"lib": "src"
7748
},
49+
"files": [
50+
"dist"
51+
],
52+
"funding": "https://github.com/skick1234/DisTube?sponsor=1",
53+
"homepage": "https://github.com/distubejs/spotify#readme",
54+
"keywords": [
55+
"distube",
56+
"plugin",
57+
"spotify",
58+
"discord",
59+
"music"
60+
],
61+
"license": "MIT",
7862
"nano-staged": {
7963
"*.ts": [
8064
"prettier --write",
@@ -84,5 +68,20 @@
8468
"prettier --write"
8569
]
8670
},
87-
"funding": "https://github.com/distubejs/ytdl-core?sponsor=1"
71+
"peerDependencies": {
72+
"distube": "^3.3.1||4"
73+
},
74+
"scripts": {
75+
"test": "jest",
76+
"lint": "eslint .",
77+
"lint:fix": "eslint . --fix",
78+
"prettier": "prettier --write \"**/*.{js,ts,json,yml,yaml,md}\"",
79+
"build": "tsup",
80+
"build:check": "tsc --noEmit",
81+
"update": "ncu -u && bun update",
82+
"postinstall": "husky install",
83+
"prepack": "bun run build && pinst -d",
84+
"postpack": "pinst -e"
85+
},
86+
"typings": "./dist/index.d.ts"
8887
}

src/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { API } from "./API";
22
import { CustomPlugin, DisTubeError, Playlist, Song, checkInvalidKey } from "distube";
33
import type { VoiceBasedChannel } from "discord.js";
4-
import type { PlayOptions, PlaylistInfo, Queue, SearchResult } from "distube";
4+
import type { PlayOptions, PlaylistInfo, Queue } from "distube";
55

66
type Falsy = undefined | null | false | 0 | "";
77
const isTruthy = <T>(x: T | Falsy): x is T => Boolean(x);
@@ -83,8 +83,9 @@ export class SpotifyPlugin extends CustomPlugin {
8383
const { member, textChannel, skip, position, metadata } = Object.assign({ position: 0 }, options);
8484
if (data.type === "track") {
8585
const query = `${data.name} ${data.artists.map((a: any) => a.name).join(" ")}`;
86-
const result = await this.search(query);
86+
const result = await this.search(query, metadata);
8787
if (!result) throw new DisTubeError("SPOTIFY_PLUGIN_NO_RESULT", `Cannot find "${query}" on YouTube.`);
88+
result.member = member;
8889
await DT.play(voiceChannel, result, options);
8990
} else {
9091
const { name, thumbnail, tracks } = data;
@@ -93,9 +94,10 @@ export class SpotifyPlugin extends CustomPlugin {
9394
const getFirstSong = async () => {
9495
const firstQuery = queries.shift();
9596
if (!firstQuery) return;
96-
const result = await this.search(firstQuery);
97+
const result = await this.search(firstQuery, metadata);
9798
if (!result) return;
98-
firstSong = new Song(result, { member, metadata });
99+
result.member = member;
100+
firstSong = result;
99101
};
100102
while (!firstSong) await getFirstSong();
101103

@@ -115,17 +117,17 @@ export class SpotifyPlugin extends CustomPlugin {
115117
const playlist = new Playlist(playlistInfo, { member, metadata });
116118
const fetchTheRest = async (q: Queue, fs: Song) => {
117119
if (queries.length) {
118-
let results: (SearchResult | null)[] = [];
120+
let results: (Song | null)[] = [];
119121
if (this.parallel) {
120-
results = await Promise.all(queries.map(query => this.search(query)));
122+
results = await Promise.all(queries.map(query => this.search(query, metadata)));
121123
} else {
122124
for (let i = 0; i < queries.length; i++) {
123-
results[i] = await this.search(queries[i]);
125+
results[i] = await this.search(queries[i], metadata);
124126
}
125127
}
126-
playlist.songs = results.filter(isTruthy).map(r => {
127-
const s = new Song(r, { member, metadata });
128+
playlist.songs = results.filter(isTruthy).map(s => {
128129
s.playlist = playlist;
130+
s.member = member;
129131
return s;
130132
});
131133
q.addToQueue(playlist.songs, !skip && position > 0 ? position + 1 : position);
@@ -157,9 +159,9 @@ export class SpotifyPlugin extends CustomPlugin {
157159
}
158160
}
159161

160-
async search(query: string) {
162+
async search(query: string, metadata: any) {
161163
try {
162-
return (await this.distube.search(query, { limit: 1 }))[0];
164+
return new Song((await this.distube.search(query, { limit: 1 }))[0], { metadata });
163165
} catch {
164166
return null;
165167
}

tests/index.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { describe, expect, test } from "@jest/globals";
22
import { SpotifyPlugin } from "@";
33

4-
test.todo("Validate Options");
5-
test.todo("SpotifyPlugin#play()");
6-
74
describe("SpotifyPlugin#validate()", () => {
85
const plugin = new SpotifyPlugin();
96

0 commit comments

Comments
 (0)