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 percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use serde::Deserialize; use serde::Deserialize;
use serenity::{ use serenity::{
framework::standard::{macros::command, Args, CommandResult}, framework::standard::{macros::command, Args, CommandError, CommandResult},
model::channel::Message, model::channel::Message,
prelude::*, 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 // TODO: use https://orion.apiseeds.com/api/music/lyric/:artist/:track instead
let mut url = String::from("https://mourits.xyz:2096/?q="); let mut url = String::from("https://mourits.xyz:2096/?q=");
// check if input is not empty // 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()); return Err("Called without input!".into());
} }
v => v,
};
// encode into url // encode into url
url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC)); url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC));
let request = match reqwest::get(&url).await { let request = reqwest::get(&url).await?;
Ok(v) => v,
Err(e) => return Err(e.into()),
};
let resp: Response = match request.json().await { let resp: Response = request
Ok(v) => v, .json()
Err(_) => return Err("Could not find lyrics".into()), .await
}; .map_err(|_| CommandError::from("Could not find lyrics"))?;
let _ = message let _ = message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {