OttoBot/src/main/kotlin/nl/voidcorp/dbot/music/SecondaryCommands.kt

82 lines
2.7 KiB
Kotlin

package nl.voidcorp.dbot.music
import khttp.get
import net.dv8tion.jda.core.EmbedBuilder
import nl.voidcorp.dbot.commands
import nl.voidcorp.dbot.commands.MusicCategory
import nl.voidcorp.dbot.commands.UnityCommand
import java.time.LocalDateTime
val lyricsCommand = UnityCommand("lyrics", "Search for lyrics!", MusicCategory) { ce ->
val song =
if (!ce.hasArgs) {
val scheduler = guildMusicMap[ce.guild.idLong]
if ((scheduler != null) && scheduler.voiceChannel.members.contains(ce.member)) {
val info = scheduler.player.playingTrack.info
println(info.author)
println(info.title)
findInfo(info.title)
} else {
ce.reply("Please either provide a search term or play a song using the bot!")
return@UnityCommand
}
} else {
findInfo(ce.args)
}
if (song != null) {
val text = findText(song)
if (text.isBlank()) {
ce.reply("Sorry, couldn't find any lyrics for this song...")
} else {
val eb = EmbedBuilder().setTimestamp(LocalDateTime.now())
.setFooter("Requested by ${ce.member.effectiveName}", ce.member.user.effectiveAvatarUrl)
.setColor(ce.selfMember.color)
eb.setTitle("Lyrics for ${song.artist} - ${song.title}")
eb.setDescription(text)
ce.reply(eb.build())
}
} else {
ce.reply("Sorry, couldn't find any lyrics for this song...")
}
}
fun musicExtra() {
commands.add(lyricsCommand)
}
data class Song(val artist: String, val title: String)
fun findInfo(search: String): Song? {
val res = khttp.get(
"https://api.genius.com/search?q=${search.replace(" ", "%20")}",
headers = mapOf("Authorization" to "Bearer eqn-1xrvrAKtoIFC-pIgNiW7cRzSvaF49wjFzasEu7coSLpufVVnv_IGVnxUIT43")
)
val hits = res.jsonObject.getJSONObject("response").getJSONArray("hits")!!
return if (hits.length() == 0) {
null
} else {
val hit = hits.getJSONObject(0).getJSONObject("result")!!
println(hit)
Song(hit.getJSONObject("primary_artist").getString("name"), hit.getString("title"))
}
}
fun findText(song: Song): String {
val res =
get(
"https://orion.apiseeds.com/api/music/lyric/${song.artist}/${song.title.replace(
" ",
"%20"
)}?apikey=8pLAxkCnWJNGWRaoPcbCFpUAKdKD77zmlcjs2FKYjdH00MDyNr6lXLHb3PQZsKJI"
)
return if (res.jsonObject.isNull("error")) {
res.jsonObject.getJSONObject("result").getJSONObject("track").getString("text")
} else {
""
}
}