Skip to content

Commit 16bd4e2

Browse files
committed
docs: document synced lyrics api
1 parent 434d1f2 commit 16bd4e2

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

apps/website/src/data/docs.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

apps/website/src/data/showcase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ export const ShowcaseResource: IShowcase = {
196196
url: 'https://npm.im/discord-player-tidal'
197197
},
198198
{
199-
name: "discord-player-youtubei",
200-
description: "Discord Player Youtubei is module that uses the Innertube API to extractor your songs providing more stability and better speeds.",
201-
url: "https://npm.im/discord-player-youtubei"
199+
name: 'discord-player-youtubei',
200+
description: 'Discord Player Youtubei is module that uses the Innertube API to extractor your songs providing more stability and better speeds.',
201+
url: 'https://npm.im/discord-player-youtubei'
202202
}
203203
])
204204
};

apps/website/src/pages/guide/_guides/examples/getting_lyrics.mdx

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,75 @@
11
# Getting Lyrics
22

3-
How cool would it be if you could get lyrics of the desired song? Well, Discord Player's Lyrics extractor can help you with that. This feature is powered by [node-genius-lyrics](https://npm.im/genius-lyrics) library.
3+
Discord Player provides two different methods for getting lyrics of a song. The first method is more robust and also provides synced lyrics. The second method is the old method and is powered by [node-genius-lyrics](https://npm.im/genius-lyrics) library.
4+
5+
## New Method
6+
7+
This method is built into discord-player itself and provides both plaintext and synced lyrics. This method is more robust and reliable than the old method. It is powered by [lrclib](https://liclib.net).
8+
9+
#### Plain Lyrics
10+
11+
```js
12+
const lyrics = await player.lyrics.search({
13+
q: 'alan walker faded'
14+
}); // this is a lot better than genius but sometimes gives weird result, specify artistName as well in such situations
15+
16+
if (!lyrics.length) return interaction.followUp({ content: 'No lyrics found', ephemeral: true });
17+
18+
const trimmedLyrics = lyrics[0].plainLyrics.substring(0, 1997);
19+
20+
const embed = new EmbedBuilder()
21+
.setTitle(lyrics[0].title)
22+
.setURL(lyrics[0].url)
23+
.setThumbnail(lyrics[0].thumbnail)
24+
.setAuthor({
25+
name: lyrics[0].artist.name,
26+
iconURL: lyrics[0].artist.image,
27+
url: lyrics[0].artist.url
28+
})
29+
.setDescription(trimmedLyrics.length === 1997 ? `${trimmedLyrics}...` : trimmedLyrics)
30+
.setColor('Yellow');
31+
32+
return interaction.followUp({ embeds: [embed] });
33+
```
34+
35+
#### Synced Lyrics
36+
37+
```js
38+
const results = await player.lyrics.search({
39+
q: 'alan walker faded'
40+
}); // this is a lot better than genius but sometimes gives weird result, specify artistName as well in such situations
41+
42+
const first = results[0];
43+
44+
if (!first.syncedLyrics) {
45+
return; // no synced lyrics available
46+
}
47+
48+
// load raw lyrics to the queue
49+
const syncedLyrics = queue.syncedLyrics(lyrics);
50+
51+
syncedLyrics.at(timestampInMilliseconds); // manually get a line at a specific timestamp
52+
53+
// Listen to live updates. This will be called whenever discord-player detects a new line in the lyrics
54+
syncedLyrics.onChange(async (lyrics, timestamp) => {
55+
// timestamp = timestamp in lyrics (not queue's time)
56+
// lyrics = line in that timestamp
57+
console.log(timestamp, lyrics);
58+
await interaction.channel?.send({
59+
content: `[${timestamp}]: ${lyrics}`
60+
});
61+
});
62+
63+
const unsubscribe = syncedLyrics.subscribe(); // start watching the queue for live updates. The onChange will not be called unless subscribe() has been called.
64+
65+
unsubscribe(); // stop watching the queue for live updates
66+
```
67+
68+
**Note:** Discord Player relies on queue's current time to detect the current line in the lyrics. It does not validate if the current track is the same as the track for which lyrics were fetched.
69+
70+
# Old Method
71+
72+
This feature is powered by [node-genius-lyrics](https://npm.im/genius-lyrics) library.
473
574
## Example
675
@@ -29,5 +98,5 @@ const embed = new EmbedBuilder()
2998
return interaction.followUp({ embeds: [embed] });
3099
```
31100
32-
> **Note:**
33-
> It may not find lyrics when using `track.title` as the search query as they contain other infos than just song title.
101+
> **Note for old method:**
102+
> It may not find lyrics when using `track.title` as the search query as they contain other infos than just song title.

packages/discord-player/src/fabric/Track.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Track<T = unknown> {
3636
private __metadata: T | null = null;
3737
private __reqMetadataFn: () => Promise<T | null>;
3838
public cleanTitle: string;
39-
public live: boolean = false
39+
public live: boolean = false;
4040

4141
/**
4242
* Track constructor
@@ -58,7 +58,7 @@ export class Track<T = unknown> {
5858
this.__metadata = data.metadata ?? null;
5959
this.__reqMetadataFn = data.requestMetadata || (() => Promise.resolve<T | null>(null));
6060
this.cleanTitle = data.cleanTitle ?? Util.cleanTitle(this.title, this.source);
61-
this.live = data.live ?? false
61+
this.live = data.live ?? false;
6262
}
6363

6464
/**

packages/extractor/src/extractors/SoundCloudExtractor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ export class SoundCloudExtractor extends BaseExtractor<SoundCloudExtractorInit>
218218
}
219219

220220
public async stream(info: Track) {
221-
222221
const url = await this.internal.util.streamLink(info.url).catch(Util.noop);
223222
if (!url) throw new Error('Could not extract stream from this track source');
224223

@@ -230,7 +229,7 @@ export class SoundCloudExtractor extends BaseExtractor<SoundCloudExtractorInit>
230229
return this.stream(track);
231230
}
232231

233-
const query = sourceExtractor?.createBridgeQuery(track) ?? `${track.author} - ${track.title}`
232+
const query = sourceExtractor?.createBridgeQuery(track) ?? `${track.author} - ${track.title}`;
234233

235234
const info = await this.handle(query, {
236235
requestedBy: track.requestedBy,

0 commit comments

Comments
 (0)