rustcord/src/commands/lyrics.rs

53 lines
1.4 KiB
Rust

use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use serde::Deserialize;
use serenity::{
framework::standard::{macros::command, Args, CommandError, CommandResult},
model::channel::Message,
prelude::*,
};
use std::env;
#[derive(Deserialize)]
struct Response {
result: Content,
artist: String,
song: String,
}
#[derive(Deserialize)]
struct Content {
lyrics: String,
}
#[command]
async fn lyrics(ctx: &Context, message: &Message, args: Args) -> CommandResult {
// TODO: use https://orion.apiseeds.com/api/music/lyric/:artist/:track instead
let mut url = String::from("https://mourits.xyz:2096/?q=");
// check if input is not empty
let input = args.rest().trim();
if input.is_empty() {
return Err("Called without input!".into());
}
// encode into url
url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC));
let request = reqwest::get(&url).await?;
let resp: Response = request
.json()
.await
.map_err(|_| CommandError::from("Could not find lyrics"))?;
let _ = message
.channel_id
.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title(format!("{} by {}", resp.song, resp.artist))
.description(resp.result.lyrics)
.colour(0xffd1dc)
})
})
.await;
Ok(())
}