Code cleanup

This commit is contained in:
Em (Ethan) Ruszanowski 2023-02-13 15:37:52 -05:00
parent e943135d2e
commit 832c78a603
No known key found for this signature in database
GPG key ID: B4B3EFE1F35DF9D6
4 changed files with 20 additions and 32 deletions

1
.gitignore vendored
View file

@ -14,3 +14,4 @@ Cargo.lock
/target
/.idea/
/quotes.txt

View file

@ -19,9 +19,8 @@ pub struct God {
ret_msg: Option<String>,
}
async fn get_utc_timestamp() -> Result<String, Error> {
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<String, Error> {
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<Session, Error> {
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, &timestamp).await?;
let timestamp: String = get_utc_timestamp().await;
let signature: String = get_signature(&dev_id, "createsession", &auth_key, &timestamp).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<Vec<God>, 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, &timestamp).await?;
let timestamp: String = get_utc_timestamp().await;
let signature: String = get_signature(&dev_id, "getgods", &auth_key, &timestamp).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<Vec<God>, Error> {
pub async fn get_random_god() -> Result<String, Error> {
let gods: Vec<God> = 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)
}

View file

@ -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)
})
})

View file

@ -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::<Vec<String>>()
.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
)
})