From 205c806369b90898ab467b0ff56d49f523edc073 Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Wed, 8 Mar 2023 13:25:50 -0500 Subject: [PATCH] Removed code and phrases that Discord didn't like --- src/commands/api.rs | 35 ++++++++++++++++++++++++++++++ src/commands/mod.rs | 3 ++- src/commands/profile.rs | 48 +++++++++++++++++++++++++++++++++++++++++ src/commands/random.rs | 2 +- src/commands/slur.rs | 28 ------------------------ src/commands/team.rs | 10 ++++----- src/main.rs | 3 ++- 7 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 src/commands/profile.rs delete mode 100644 src/commands/slur.rs diff --git a/src/commands/api.rs b/src/commands/api.rs index 3ebd9b0..5918127 100644 --- a/src/commands/api.rs +++ b/src/commands/api.rs @@ -19,6 +19,23 @@ pub struct God { ret_msg: Option, } +#[derive(Deserialize, Debug)] +#[allow(dead_code)] +pub struct Profile { + #[serde(rename = "Name")] + pub name: Option, + #[serde(rename = "Personal_Status_Message")] + pub personal_status_message: Option, + pub hz_player_name: Option, + #[serde(rename = "HoursPlayed")] + pub hours_played: i32, + #[serde(rename = "Losses")] + pub losses: i32, + #[serde(rename = "Wins")] + pub wins: i32, + pub ret_msg: Option, +} + async fn get_utc_timestamp() -> String { chrono::Utc::now().format("%Y%m%d%H%M%S").to_string() } @@ -73,3 +90,21 @@ pub async fn get_random_god() -> Result { let name: String = god.name.clone(); Ok(name) } + +pub async fn get_player(player: String) -> Result, 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 session_id: String = create_session().await?.session_id; + + let timestamp: String = get_utc_timestamp().await; + let signature: String = get_signature(&dev_id, "getplayer", &auth_key, ×tamp).await; + + let request: String = format!( + "https://api.smitegame.com/smiteapi.svc/getplayerJson/{dev_id}/{signature}/{session_id}/{timestamp}/{player}" + ); + + let response: Response = reqwest::get(&request).await?; + let profiles: Vec = response.json().await?; + Ok(profiles) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 17f3f8d..aab0d7e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,7 @@ // Group all commands for registration mod api; +pub mod ping; +pub mod profile; pub mod random; pub mod register; -pub mod slur; pub mod team; diff --git a/src/commands/profile.rs b/src/commands/profile.rs new file mode 100644 index 0000000..02ac873 --- /dev/null +++ b/src/commands/profile.rs @@ -0,0 +1,48 @@ +use crate::commands::api::get_player; +use crate::serenity; +use crate::{Context, Error}; + +/// Looks up a player's profile +#[poise::command(slash_command)] +pub async fn profile( + ctx: Context<'_>, + #[rename = "player"] player_name: String, +) -> Result<(), Error> { + let profiles = get_player(player_name).await?; + let profile = profiles.first().unwrap(); + if profile.name.is_none() { + ctx.send(|f| { + f.embed(|f| { + f.title("Hidden") + .description("This profile is hidden.") + .color(serenity::Colour::RED) + }) + }) + .await?; + return Ok(()); + } + + let winrate = + (profile.wins as f32 / (profile.wins as f32 + profile.losses as f32)) * 100 as f32; + ctx.send(|f| { + f.embed(|f| { + f.title(format!("{}", profile.name.as_ref().unwrap())) + .description(format!( + "{}'s statistics.", + profile.hz_player_name.as_ref().unwrap() + )) + .field( + "Status", + format!("{}", profile.personal_status_message.as_ref().unwrap()), + false, + ) + .field("Hours played", format!("{}", profile.hours_played), false) + .field("Wins", format!("{}", profile.wins), true) + .field("Losses", format!("{}", profile.losses), true) + .field("Winrate", format!("{:.2}%", winrate), true) + .color(serenity::Colour::BLURPLE) + }) + }) + .await?; + Ok(()) +} diff --git a/src/commands/random.rs b/src/commands/random.rs index 1ab4f44..91936ec 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 **{god}**, idiot.")) + .description(format!("Your random god is **{god}**!")) .color(serenity::Colour::BLUE) }) }) diff --git a/src/commands/slur.rs b/src/commands/slur.rs deleted file mode 100644 index 7308759..0000000 --- a/src/commands/slur.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::serenity; -use crate::{Context, Error}; -use rand::seq::IteratorRandom; -use std::{ - fs::File, - io::{BufRead, BufReader}, -}; - -/// Basically a ping command -#[poise::command(slash_command, prefix_command)] -pub async fn slur(ctx: Context<'_>) -> Result<(), Error> { - let file: File = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing.")); // Open the quotes file - let file: BufReader = BufReader::new(file); // Read the quotes file - let quotes = file.lines().map(|res| res.expect("Failed to read line.")); - let quote: String = quotes - .choose(&mut rand::thread_rng()) - .expect("No lines in file."); // Pick a random quote - - ctx.send(|f| { - f.embed(|f| { - f.title("DMBrandon Sez") - .description(format!("\"{}\"", quote)) - .color(serenity::Colour::GOLD) - }) - }) - .await?; // Send embed with team picks - Ok(()) -} diff --git a/src/commands/team.rs b/src/commands/team.rs index d827e86..b488847 100644 --- a/src/commands/team.rs +++ b/src/commands/team.rs @@ -38,12 +38,12 @@ pub async fn team( // Make sure there are enough members in the voice channel ctx.send(|f| { f.embed(|f| { - f.title(format!("Custom {}v{} Teams", size, size)) - .description("You don't have enough friends for that, idiot.") + f.title(format!("Custom {size}v{size} Teams")) + .description("There are not enough members in the call!") .color(serenity::Colour::RED) }) }) - .await?; // Insult the user for not having enough friends + .await?; return Ok(()); // Break out early if there are not enough members } @@ -57,7 +57,7 @@ pub async fn team( ctx.send(|f| { f.embed(|f| { f.title(format!("Custom {size}v{size} Teams")) - .description("VER") + .description("Click the button below to move the Chaos players.") .field("Order", team_to_ping(order), false) .field("Chaos", team_to_ping(chaos), false) .color(serenity::Colour::DARK_GREEN) @@ -94,7 +94,7 @@ pub async fn team( .interaction_response_data(|f| { f.embed(|f| { f.title(format!("Custom {size}v{size} Teams")) - .description("VVGO VVW VVX") + .description("Good luck! Have fun!") .field("Order", team_to_ping(order), false) .field("Chaos", team_to_ping(chaos), false) .color(serenity::Colour::DARK_GREEN) diff --git a/src/main.rs b/src/main.rs index 4df5668..c4b6a9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,10 +12,11 @@ async fn main() { let framework = poise::Framework::builder() .options(poise::FrameworkOptions { commands: vec![ - commands::slur::slur(), + commands::ping::ping(), commands::team::team(), commands::random::random(), commands::register::register(), + commands::profile::profile(), ], // IntelliJ doesn't like this, but it's fine. ..Default::default() })