Add lyrics command and format playlists list differently
Signed-off-by: trivernis <trivernis@protonmail.com>pull/2/head
parent
0cb0a4c47b
commit
750ee7d0dc
@ -0,0 +1,45 @@
|
||||
use serenity::client::Context;
|
||||
use serenity::framework::standard::macros::command;
|
||||
use serenity::framework::standard::CommandResult;
|
||||
use serenity::model::channel::Message;
|
||||
|
||||
use crate::commands::music::get_queue_for_guild;
|
||||
use crate::providers::music::lyrics::get_lyrics;
|
||||
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
#[description("Shows the lyrics for the currently playing song")]
|
||||
#[usage("")]
|
||||
async fn lyrics(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).await.unwrap();
|
||||
log::debug!("Fetching lyrics for song playing in {}", guild.id);
|
||||
|
||||
let queue = get_queue_for_guild(ctx, &guild.id).await?;
|
||||
let queue_lock = queue.lock().await;
|
||||
|
||||
if let Some(current) = queue_lock.current() {
|
||||
log::debug!("Playing music. Fetching lyrics for currently playing song...");
|
||||
let metadata = current.metadata();
|
||||
let title = metadata.title.clone().unwrap();
|
||||
let author = metadata.artist.clone().unwrap();
|
||||
|
||||
if let Some(lyrics) = get_lyrics(&*author, &*title).await? {
|
||||
log::trace!("Lyrics for '{}' are {}", title, lyrics);
|
||||
|
||||
msg.channel_id
|
||||
.send_message(ctx, |m| {
|
||||
m.embed(|e| e.title(format!("Lyrics for {}", title)).description(lyrics))
|
||||
})
|
||||
.await?;
|
||||
} else {
|
||||
log::debug!("No lyrics found");
|
||||
msg.channel_id.say(ctx, "No lyrics found").await?;
|
||||
}
|
||||
} else {
|
||||
msg.channel_id
|
||||
.say(ctx, "I'm not playing music right now")
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
use crate::utils::error::BotResult;
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
const API_ENDPOINT: &str = "https://api.lyrics.ovh/v1/";
|
||||
|
||||
pub async fn get_lyrics(artist: &str, title: &str) -> BotResult<Option<String>> {
|
||||
lazy_static::lazy_static! { static ref DOUBLE_LB_REGEX: Regex = Regex::new(r"\n\n").unwrap(); }
|
||||
log::debug!("Requesting lyrics for '{}' by '{}'", title, artist);
|
||||
let request_url = format!("{}{}/{}", API_ENDPOINT, artist, title);
|
||||
log::trace!("Request url is {}", request_url);
|
||||
let response = reqwest::get(request_url).await?;
|
||||
let response_text = response.text().await?;
|
||||
log::trace!("Lyrics Response is {}", response_text);
|
||||
|
||||
let lyrics: Option<Lyrics> = serde_json::from_str(&*response_text).ok();
|
||||
|
||||
Ok(lyrics.map(|l| DOUBLE_LB_REGEX.replace_all(&*l.lyrics, "\n").to_string()))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
struct Lyrics {
|
||||
lyrics: String,
|
||||
}
|
Loading…
Reference in New Issue