From 832c78a603aa38825ed83aca1a9f34f24976c2d5 Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Mon, 13 Feb 2023 15:37:52 -0500 Subject: [PATCH] Code cleanup --- .gitignore | 1 + src/commands/api.rs | 35 ++++++++++++----------------------- src/commands/random.rs | 2 +- src/commands/team.rs | 14 ++++++-------- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index f7a8089..4f33c95 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ Cargo.lock /target /.idea/ +/quotes.txt diff --git a/src/commands/api.rs b/src/commands/api.rs index 582525b..3ebd9b0 100644 --- a/src/commands/api.rs +++ b/src/commands/api.rs @@ -19,9 +19,8 @@ pub struct God { ret_msg: Option, } -async fn get_utc_timestamp() -> Result { - let timestamp: String = chrono::Utc::now().format("%Y%m%d%H%M%S").to_string(); - Ok(timestamp) +async fn get_utc_timestamp() -> String { + chrono::Utc::now().format("%Y%m%d%H%M%S").to_string() } async fn get_signature( @@ -29,24 +28,20 @@ async fn get_signature( method: &str, auth_key: &String, timestamp: &String, -) -> Result { - let hash: Digest = md5::compute(format!("{}{}{}{}", dev_id, method, auth_key, timestamp)); - let signature: String = format!("{:x}", hash); - Ok(signature) +) -> String { + let hash: Digest = md5::compute(format!("{dev_id}{method}{auth_key}{timestamp}")); + format!("{:x}", hash) } async fn create_session() -> Result { let dev_id: String = std::env::var("DEV_ID").expect("Missing DEV_ID"); let auth_key: String = std::env::var("AUTH_KEY").expect("Missing AUTH_KEY"); - let timestamp: String = get_utc_timestamp().await?; - let signature: String = get_signature(&dev_id, "createsession", &auth_key, ×tamp).await?; + let timestamp: String = get_utc_timestamp().await; + let signature: String = get_signature(&dev_id, "createsession", &auth_key, ×tamp).await; let request: String = format!( - "https://api.smitegame.com/smiteapi.svc/createsessionJson/{dev_id}/{signature}/{timestamp}", - dev_id = dev_id, - signature = signature, - timestamp = timestamp + "https://api.smitegame.com/smiteapi.svc/createsessionJson/{dev_id}/{signature}/{timestamp}" ); let response: Response = reqwest::get(&request).await?; @@ -60,15 +55,11 @@ async fn get_gods() -> Result, Error> { let session_id: String = create_session().await?.session_id; - let timestamp: String = get_utc_timestamp().await?; - let signature: String = get_signature(&dev_id, "getgods", &auth_key, ×tamp).await?; + let timestamp: String = get_utc_timestamp().await; + let signature: String = get_signature(&dev_id, "getgods", &auth_key, ×tamp).await; let request: String = format!( - "https://api.smitegame.com/smiteapi.svc/getgodsJson/{id}/{signature}/{session}/{timestamp}/1", - id = dev_id, - signature = signature, - session = session_id, - timestamp = timestamp, + "https://api.smitegame.com/smiteapi.svc/getgodsJson/{dev_id}/{signature}/{session_id}/{timestamp}/1" ); let response: Response = reqwest::get(&request).await?; @@ -78,9 +69,7 @@ async fn get_gods() -> Result, Error> { pub async fn get_random_god() -> Result { let gods: Vec = get_gods().await?; - let god: &God = gods - .choose(&mut rand::thread_rng()) - .expect("Couldn't pick random god."); + let god: &God = gods.choose(&mut rand::thread_rng()).unwrap(); let name: String = god.name.clone(); Ok(name) } diff --git a/src/commands/random.rs b/src/commands/random.rs index 64b94c0..1ab4f44 100644 --- a/src/commands/random.rs +++ b/src/commands/random.rs @@ -15,7 +15,7 @@ pub async fn god(ctx: Context<'_>) -> Result<(), Error> { ctx.send(|f| { f.embed(|f| { f.title("Random God") - .description(format!("Try not to throw with **{}**, idiot.", god)) + .description(format!("Try not to throw with **{god}**, idiot.")) .color(serenity::Colour::BLUE) }) }) diff --git a/src/commands/team.rs b/src/commands/team.rs index f403e5b..d827e86 100644 --- a/src/commands/team.rs +++ b/src/commands/team.rs @@ -9,11 +9,10 @@ use crate::{Context, Error}; /// Return a string of pingable IDs from a slice of string UserIds fn team_to_ping(team: &[&String]) -> String { - return team - .iter() - .map(|o| format!("<@{}>", o)) + team.iter() + .map(|o| format!("<@{o}>")) .collect::>() - .join(", "); + .join(", ") } /// Splits up players for custom matches @@ -57,7 +56,7 @@ pub async fn team( ctx.send(|f| { f.embed(|f| { - f.title(format!("Custom {}v{} Teams", size, size)) + f.title(format!("Custom {size}v{size} Teams")) .description("VER") .field("Order", team_to_ping(order), false) .field("Chaos", team_to_ping(chaos), false) @@ -83,7 +82,7 @@ pub async fn team( { let guild: Guild = ctx.guild().unwrap(); // Grab guild from context for user in chaos { - let member: Member = guild.member(ctx, UserId(user.parse().unwrap())).await?; // Get the member in the correct guild + let member: Member = guild.member(ctx, UserId(user.parse()?)).await?; // Get the member in the correct guild member .move_to_voice_channel(ctx, chaos_channel.id()) .await?; // Move the member to the correct voice channel @@ -94,7 +93,7 @@ pub async fn team( ir.kind(serenity::InteractionResponseType::UpdateMessage) .interaction_response_data(|f| { f.embed(|f| { - f.title(format!("Custom {}v{} Teams", size, size)) + f.title(format!("Custom {size}v{size} Teams")) .description("VVGO VVW VVX") .field("Order", team_to_ping(order), false) .field("Chaos", team_to_ping(chaos), false) @@ -109,7 +108,6 @@ pub async fn team( .disabled(true) // with disabled button .style(serenity::ButtonStyle::Primary) .label("Quit Sibelius") // and new text - .custom_id(uuid_team) }, // Use the context ID as button ID ) })