1818
1919package me.duncte123.skybot.commands.music
2020
21- import com.github.natanbc.reliqua.limiter.RateLimiter
2221import dev.arbjerg.lavalink.client.Link
2322import me.duncte123.botcommons.messaging.EmbedUtils
2423import me.duncte123.botcommons.messaging.MessageUtils.sendEmbed
2524import me.duncte123.botcommons.messaging.MessageUtils.sendMsg
26- import me.duncte123.botcommons.web.WebParserUtils
27- import me.duncte123.botcommons.web.WebUtils
2825import me.duncte123.lyrics.model.Lyrics
2926import me.duncte123.lyrics.model.TextLyrics
3027import me.duncte123.lyrics.model.TimedLyrics
3128import me.duncte123.skybot.Variables
3229import me.duncte123.skybot.objects.command.CommandContext
3330import me.duncte123.skybot.objects.command.MusicCommand
34- import me.duncte123.skybot.objects.config.DunctebotConfig
3531import me.duncte123.skybot.utils.chunkForEmbed
3632import net.dv8tion.jda.api.EmbedBuilder
3733import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
3834import net.dv8tion.jda.api.interactions.commands.OptionType
3935import net.dv8tion.jda.api.interactions.commands.build.SubcommandData
40- import java.net.URLEncoder
41- import java.nio.charset.StandardCharsets
4236
4337class LyricsCommand : MusicCommand () {
4438 init {
@@ -53,15 +47,15 @@ class LyricsCommand : MusicCommand() {
5347 val args = ctx.args
5448
5549 if (args.isNotEmpty()) {
56- // TODO: search with lavalink for lyrics?
57- handleSearch(ctx.argsRaw, ctx.config) {
50+ sendMsg(ctx, " Search is not supported atm, sorry " )
51+ /* handleSearch(ctx.argsRaw, ctx.config) {
5852 if (it == null) {
5953 sendMsg(ctx, "There where no lyrics found for `${ctx.argsRaw}`")
6054 return@handleSearch
6155 }
6256
6357 sendEmbed(ctx, it)
64- }
58+ }*/
6559 return
6660 }
6761
@@ -75,16 +69,7 @@ class LyricsCommand : MusicCommand() {
7569
7670 loadLyricsFromLavalink(player.link) {
7771 if (it == null ) {
78- val searchItem = " ${playingTrack.info.title} - ${playingTrack.info.author} "
79-
80- handleSearch(searchItem, ctx.config) { embed ->
81- if (embed == null ) {
82- sendMsg(ctx, " There where no lyrics found for `${playingTrack.info.title} `" )
83- return @handleSearch
84- }
85-
86- sendEmbed(ctx, embed)
87- }
72+ sendMsg(ctx, " There where no lyrics found for `${playingTrack.info.title} `" )
8873 return @loadLyricsFromLavalink
8974 }
9075
@@ -118,17 +103,8 @@ class LyricsCommand : MusicCommand() {
118103
119104 loadLyricsFromLavalink(player.link) {
120105 if (it == null ) {
121- val searchItem = " ${playingTrack.info.title} - ${playingTrack.info.author} "
122-
123- handleSearch(searchItem, variables.config) { embed ->
124- if (embed == null ) {
125- event.hook.sendMessage(" There where no lyrics found for `${playingTrack.info.title} `" )
126- .queue()
127- return @handleSearch
128- }
129-
130- event.hook.sendMessageEmbeds(embed.build()).queue()
131- }
106+ event.hook.sendMessage(" There where no lyrics found for `${playingTrack.info.title} `" )
107+ .queue()
132108 return @loadLyricsFromLavalink
133109 }
134110
@@ -141,15 +117,15 @@ class LyricsCommand : MusicCommand() {
141117
142118 val search = opt.asString
143119
144- // TODO: search with lavalink for lyrics?
145- handleSearch(search, variables.config) {
120+ /* handleSearch(search, variables.config) {
146121 if (it == null) {
147122 event.hook.sendMessage("There where no lyrics found for `$search`").queue()
148123 return@handleSearch
149124 }
150125
151126 event.hook.sendMessageEmbeds(it.build()).queue()
152- }
127+ }*/
128+ event.hook.sendMessage(" Search is not supported atm, sorry" ).queue()
153129 }
154130
155131 private fun loadLyricsFromLavalink (link : Link , cb : (EmbedBuilder ? ) -> Unit ) {
@@ -217,81 +193,5 @@ class LyricsCommand : MusicCommand() {
217193 return builder
218194 }
219195
220- private fun handleSearch (search : String , config : DunctebotConfig , cb : (EmbedBuilder ? ) -> Unit ) {
221- searchForSong(search, config) {
222- if (it == null ) {
223- cb(null )
224- return @searchForSong
225- }
226-
227- cb(buildLyricsEmbed(it))
228- }
229- }
230-
231- private fun searchForSong (search : String , config : DunctebotConfig , callback : (LyricInfo ? ) -> Unit ) {
232- WebUtils .ins.prepareBuilder(
233- WebUtils .defaultRequest()
234- .header(" Authorization" , " Bearer ${config.apis.genius} " )
235- .url(" https://api.genius.com/search?q=${URLEncoder .encode(search, StandardCharsets .UTF_8 )} " ),
236- {
237- it.setRateLimiter(WebUtils .ins.getRateLimiter(" api.genius.com/search" ))
238- },
239- null
240- ).build(
241- WebParserUtils ::toJSONObject,
242- WebParserUtils ::handleError
243- ).async {
244- val results = it[" response" ][" hits" ]
245-
246- if (results.isEmpty) {
247- callback(null )
248- return @async
249- }
250-
251- val firstResult = results.firstOrNull { node -> node[" type" ].asText() == " song" }
252-
253- if (firstResult == null ) {
254- callback(null )
255- return @async
256- }
257-
258- val data = firstResult[" result" ]
259- val path = data[" path" ].asText()
260-
261- loadLyrics(path) { lyrics ->
262- if (lyrics.isNullOrEmpty()) {
263- callback(null )
264- } else {
265- callback(
266- LyricInfo (
267- data[" song_art_image_url" ].asText(),
268- data[" full_title" ].asText(),
269- data[" url" ].asText(),
270- " genius.com" ,
271- lyrics
272- )
273- )
274- }
275- }
276- }
277- }
278-
279- private fun loadLyrics (path : String , callback : (String? ) -> Unit ) {
280- WebUtils .ins.scrapeWebPage(" https://genius.com$path " ) { it.setRateLimiter(RateLimiter .directLimiter()) }
281- .async({
282- val lyricsContainer = it.select(" div[data-lyrics-container]" )
283- val text = lyricsContainer.first()!!
284- .wholeText()
285- .replace(" <br>" , " \n " )
286- .replace(" \n\n\n " , " \n " )
287- .trim()
288-
289- callback(text)
290- }) {
291- LOGGER .error(" Loading lyrics from genius.com failed!" , it)
292- callback(null )
293- }
294- }
295-
296196 private data class LyricInfo (val artUrl : String , val title : String , val url : String? , val source : String , val lyrics : String )
297197}
0 commit comments