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

Commit

Permalink
feat: easier to override youtube search
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Mar 20, 2024
1 parent 128c070 commit 650baec
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 69 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,27 @@ new SpotifyPlugin({
},
});
```

##### Use SoundCloudPlugin to search instead of YouTube

```ts
import { DisTube } from "distube";
import { SpotifyPlugin } from "@distube/spotify";
import { SoundCloudPlugin } from "@distube/soundcloud";

const scPlugin = new SoundCloudPlugin();

class NewSpotifyPlugin extends SpotifyPlugin {
override async search(query: string, metadata: any) {
try {
return new Song((await scPlugin.search(query, { limit: 1 }))[0], { metadata });
} catch {
return null;
}
}
}

const distube = new DisTube(client, {
plugins: [new NewSpotifyPlugin(), scPlugin],
});
```
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[install]
dev = true
optional = false
peer = false
109 changes: 54 additions & 55 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,80 +1,64 @@
{
"name": "@distube/spotify",
"version": "1.5.2",
"description": "A DisTube custom plugin for supporting Spotify.",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"exports": "./dist/index.js",
"directories": {
"lib": "src"
},
"files": [
"dist"
],
"scripts": {
"test": "jest",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --write \"**/*.{js,ts,json,yml,yaml,md}\"",
"build": "tsup",
"build:check": "tsc --noEmit",
"update": "ncu -u && bun update",
"postinstall": "husky install",
"prepack": "bun run build && pinst -d",
"postpack": "pinst -e"
},
"author": "Skick",
"repository": {
"type": "git",
"url": "git+https://github.com/distubejs/spotify.git"
},
"keywords": [
"distube",
"plugin",
"spotify",
"discord",
"music"
],
"author": "Skick",
"license": "MIT",
"bugs": {
"url": "https://github.com/distubejs/spotify/issues"
"main": "./dist/index.js",
"dependencies": {
"spotify-uri": "^4.0.1",
"spotify-url-info": "^3.2.13",
"spotify-web-api-node": "^5.0.2",
"undici": "^6.9.0"
},
"homepage": "https://github.com/distubejs/spotify#readme",
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@discordjs/opus": "^0.9.0",
"@commitlint/cli": "^19.2.0",
"@commitlint/config-conventional": "^19.1.0",
"@discordjs/voice": "^0.16.1",
"@jest/globals": "^29.7.0",
"@types/spotify-web-api-node": "^5.0.11",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"commitlint": "^18.4.3",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"commitlint": "^19.2.0",
"discord.js": "^14.14.1",
"distube": "^4.1.1",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-config-distube": "^1.6.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-jsdoc": "^46.9.1",
"husky": "^8.0.3",
"eslint-plugin-jsdoc": "^48.2.1",
"husky": "^9.0.11",
"jest": "^29.7.0",
"nano-staged": "^0.8.0",
"pinst": "^3.0.0",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"tsup": "^8.0.2",
"typescript": "^5.4.2"
},
"dependencies": {
"spotify-uri": "^4.0.1",
"spotify-url-info": "^3.2.10",
"spotify-web-api-node": "^5.0.2",
"undici": "^6.2.1"
"exports": "./dist/index.js",
"bugs": {
"url": "https://github.com/distubejs/spotify/issues"
},
"peerDependencies": {
"distube": "^3.3.1||4"
"description": "A DisTube custom plugin for supporting Spotify.",
"directories": {
"lib": "src"
},
"files": [
"dist"
],
"funding": "https://github.com/skick1234/DisTube?sponsor=1",
"homepage": "https://github.com/distubejs/spotify#readme",
"keywords": [
"distube",
"plugin",
"spotify",
"discord",
"music"
],
"license": "MIT",
"nano-staged": {
"*.ts": [
"prettier --write",
Expand All @@ -84,5 +68,20 @@
"prettier --write"
]
},
"funding": "https://github.com/distubejs/ytdl-core?sponsor=1"
"peerDependencies": {
"distube": "^3.3.1||4"
},
"scripts": {
"test": "jest",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --write \"**/*.{js,ts,json,yml,yaml,md}\"",
"build": "tsup",
"build:check": "tsc --noEmit",
"update": "ncu -u && bun update",
"postinstall": "husky install",
"prepack": "bun run build && pinst -d",
"postpack": "pinst -e"
},
"typings": "./dist/index.d.ts"
}
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { API } from "./API";
import { CustomPlugin, DisTubeError, Playlist, Song, checkInvalidKey } from "distube";
import type { VoiceBasedChannel } from "discord.js";
import type { PlayOptions, PlaylistInfo, Queue, SearchResult } from "distube";
import type { PlayOptions, PlaylistInfo, Queue } from "distube";

type Falsy = undefined | null | false | 0 | "";
const isTruthy = <T>(x: T | Falsy): x is T => Boolean(x);
Expand Down Expand Up @@ -83,8 +83,9 @@ export class SpotifyPlugin extends CustomPlugin {
const { member, textChannel, skip, position, metadata } = Object.assign({ position: 0 }, options);
if (data.type === "track") {
const query = `${data.name} ${data.artists.map((a: any) => a.name).join(" ")}`;
const result = await this.search(query);
const result = await this.search(query, metadata);
if (!result) throw new DisTubeError("SPOTIFY_PLUGIN_NO_RESULT", `Cannot find "${query}" on YouTube.`);
result.member = member;
await DT.play(voiceChannel, result, options);
} else {
const { name, thumbnail, tracks } = data;
Expand All @@ -93,9 +94,10 @@ export class SpotifyPlugin extends CustomPlugin {
const getFirstSong = async () => {
const firstQuery = queries.shift();
if (!firstQuery) return;
const result = await this.search(firstQuery);
const result = await this.search(firstQuery, metadata);
if (!result) return;
firstSong = new Song(result, { member, metadata });
result.member = member;
firstSong = result;
};
while (!firstSong) await getFirstSong();

Expand All @@ -115,17 +117,17 @@ export class SpotifyPlugin extends CustomPlugin {
const playlist = new Playlist(playlistInfo, { member, metadata });
const fetchTheRest = async (q: Queue, fs: Song) => {
if (queries.length) {
let results: (SearchResult | null)[] = [];
let results: (Song | null)[] = [];
if (this.parallel) {
results = await Promise.all(queries.map(query => this.search(query)));
results = await Promise.all(queries.map(query => this.search(query, metadata)));
} else {
for (let i = 0; i < queries.length; i++) {
results[i] = await this.search(queries[i]);
results[i] = await this.search(queries[i], metadata);
}
}
playlist.songs = results.filter(isTruthy).map(r => {
const s = new Song(r, { member, metadata });
playlist.songs = results.filter(isTruthy).map(s => {
s.playlist = playlist;
s.member = member;
return s;
});
q.addToQueue(playlist.songs, !skip && position > 0 ? position + 1 : position);
Expand Down Expand Up @@ -157,9 +159,9 @@ export class SpotifyPlugin extends CustomPlugin {
}
}

async search(query: string) {
async search(query: string, metadata: any) {
try {
return (await this.distube.search(query, { limit: 1 }))[0];
return new Song((await this.distube.search(query, { limit: 1 }))[0], { metadata });
} catch {
return null;
}
Expand Down
3 changes: 0 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { describe, expect, test } from "@jest/globals";
import { SpotifyPlugin } from "@";

test.todo("Validate Options");
test.todo("SpotifyPlugin#play()");

describe("SpotifyPlugin#validate()", () => {
const plugin = new SpotifyPlugin();

Expand Down

0 comments on commit 650baec

Please sign in to comment.