|
1 | 1 | # Getting Lyrics |
2 | 2 |
|
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. |
4 | 73 |
|
5 | 74 | ## Example |
6 | 75 |
|
@@ -29,5 +98,5 @@ const embed = new EmbedBuilder() |
29 | 98 | return interaction.followUp({ embeds: [embed] }); |
30 | 99 | ``` |
31 | 100 |
|
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. |
0 commit comments