package com.jagrosh.jmusicbot.commands.music; import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jlyrics.LyricsClient; import com.jagrosh.jmusicbot.Bot; import com.jagrosh.jmusicbot.audio.AudioHandler; import com.jagrosh.jmusicbot.commands.MusicCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; public class LyricsCmd extends MusicCommand { private final LyricsClient client = new LyricsClient(); public LyricsCmd(Bot bot) { super(bot); this.name = "lyrics"; this.arguments = "[song name]"; this.help = "shows the lyrics of a song"; this.aliases = bot.getConfig().getAliases(this.name); this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS}; } @Override public void doCommand(CommandEvent event) { String title; if(event.getArgs().isEmpty()) { AudioHandler sendingHandler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler(); if (sendingHandler.isMusicPlaying(event.getJDA())) title = sendingHandler.getPlayer().getPlayingTrack().getInfo().title; else { event.replyError("There must be music playing to use that!"); return; } } else title = event.getArgs(); event.getChannel().sendTyping().queue(); client.getLyrics(title).thenAccept(lyrics -> { if(lyrics == null) { event.replyError("Lyrics for `" + title + "` could not be found!" + (event.getArgs().isEmpty() ? " Try entering the song name manually (`lyrics [song name]`)" : "")); return; } EmbedBuilder eb = new EmbedBuilder() .setAuthor(lyrics.getAuthor()) .setColor(event.getSelfMember().getColor()) .setTitle(lyrics.getTitle(), lyrics.getURL()); if(lyrics.getContent().length()>15000) { event.replyWarning("Lyrics for `" + title + "` found but likely not correct: " + lyrics.getURL()); } else if(lyrics.getContent().length()>2000) { String content = lyrics.getContent().trim(); while(content.length() > 2000) { int index = content.lastIndexOf("\n\n", 2000); if(index == -1) index = content.lastIndexOf("\n", 2000); if(index == -1) index = content.lastIndexOf(" ", 2000); if(index == -1) index = 2000; event.reply(eb.setDescription(content.substring(0, index).trim()).build()); content = content.substring(index).trim(); eb.setAuthor(null).setTitle(null, null); } event.reply(eb.setDescription(content).build()); } else event.reply(eb.setDescription(lyrics.getContent()).build()); }); } }