Clean up match statements for lyrics command

This commit is contained in:
charlotte ✨ 2021-07-07 23:56:24 +01:00
parent a02bcecb5b
commit 854fb63e48
1 changed files with 10 additions and 15 deletions

View File

@ -1,7 +1,7 @@
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use serde::Deserialize;
use serenity::{
framework::standard::{macros::command, Args, CommandResult},
framework::standard::{macros::command, Args, CommandError, CommandResult},
model::channel::Message,
prelude::*,
};
@ -24,24 +24,19 @@ 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 = match args.rest().trim() {
"" => {
let input = args.rest().trim();
if input.is_empty() {
return Err("Called without input!".into());
}
v => v,
};
// encode into url
url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC));
let request = match reqwest::get(&url).await {
Ok(v) => v,
Err(e) => return Err(e.into()),
};
let request = reqwest::get(&url).await?;
let resp: Response = match request.json().await {
Ok(v) => v,
Err(_) => return Err("Could not find lyrics".into()),
};
let resp: Response = request
.json()
.await
.map_err(|_| CommandError::from("Could not find lyrics"))?;
let _ = message
.channel_id
.send_message(&ctx.http, |m| {