All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.LavaplayerUtil.kt Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
@file:JvmName("LavaplayerUtil")

package dev.schlaubi.lyrics.lavaplayer

import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import dev.schlaubi.lyrics.LyricsClient
import dev.schlaubi.lyrics.LyricsNotFoundException
import dev.schlaubi.lyrics.protocol.Lyrics
import kotlinx.coroutines.future.future
import java.util.concurrent.CompletionStage

/**
 * Finds the lyrics for [track].
 */
@JvmName("findLyricsSuspending")
public suspend fun LyricsClient.findLyrics(track: AudioTrack): Lyrics {
    val videoId = when {
        track is YoutubeAudioTrack -> track.info.identifier
        track.info.isrc != null -> search(track.info.isrc).firstOrNull()?.videoId ?: throw LyricsNotFoundException()
        else -> search("${track.info.title} - ${track.info.author}").firstOrNull()?.videoId
            ?: throw LyricsNotFoundException()
    }

    return requestLyrics(videoId)
}

/**
 * Finds the lyrics for [track].
 *
 * @see CompletionStage
 */
public fun LyricsClient.findLyricsAsync(track: AudioTrack): CompletionStage = future { findLyrics(track) }

/**
 * Finds the lyrics for [track].
 *
 * **Important:** This method blocks the current thread
 */
@JvmName("findLyrics")
public fun LyricsClient.findLyricsBlocking(track: AudioTrack): Lyrics =
    findLyricsAsync(track).toCompletableFuture().join()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy