diff --git a/src/discord/commands.rs b/src/discord/commands.rs index 3a091db..00c4dde 100644 --- a/src/discord/commands.rs +++ b/src/discord/commands.rs @@ -87,10 +87,24 @@ async fn start(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { first_names.shuffle(&mut thread_rng()); last_names.shuffle(&mut thread_rng()); + let mut profile_pics = global_data.get_profile_pic_album().await?; + + profile_pics.shuffle(&mut thread_rng()); + for player in players { let first_name = first_names.pop(); let last_name = last_names.pop(); - add_user_to_game(ctx, &guild, &mut global_data, player, first_name, last_name).await?; + let profile_pic_url = profile_pics.pop(); + add_user_to_game( + ctx, + &guild, + &mut global_data, + player, + first_name, + last_name, + profile_pic_url, + ) + .await?; } for player_data in &global_data.game_state()?.player_data { diff --git a/src/discord/helper.rs b/src/discord/helper.rs index 2424305..0b25d28 100644 --- a/src/discord/helper.rs +++ b/src/discord/helper.rs @@ -12,6 +12,7 @@ use crate::game::game_state::PhaseDuration; use crate::game::global_data::GlobalData; use crate::game::player_data::PlayerData; use crate::game::MessageSource; +use crate::imgur::Image; use serenity::prelude::SerenityError; fn filter_source_channel(player_data: &&PlayerData, msg_source: &MessageSource) -> bool { @@ -213,11 +214,16 @@ pub async fn add_user_to_game( discord_user: &Member, first_name: Option, last_name: Option, + profile_pic: Option, ) -> error::Result { - if first_name == None && last_name == None { + if first_name.is_none() && last_name.is_none() { return Err(WoxlfError::RanOutOfCodenames); } + if profile_pic.is_none() { + return Err(WoxlfError::RanOutOfProfilePics); + } + let codename = global_data .templates()? .build_name(global_data, first_name, last_name) @@ -254,7 +260,7 @@ pub async fn add_user_to_game( vote_target: None, codename, channel_webhook_id: webhook.id.0, - profile_pic_url: global_data.get_profile_pic_url().await?, + profile_pic_url: profile_pic.unwrap().link, }; global_data diff --git a/src/error.rs b/src/error.rs index d097e8e..056c82b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ pub enum WoxlfError { ImgurError(ImgurError), RanOutOfCodenames, TemplateError(tera::Error), + RanOutOfProfilePics, } impl std::error::Error for WoxlfError {} @@ -39,6 +40,7 @@ impl Display for WoxlfError { .to_string() } WoxlfError::TemplateError(e) => format!("Template error: {}", e), + WoxlfError::RanOutOfProfilePics => "Ran out of user profile pics".to_string(), }; write!(f, "Woxlf Error: {}", msg) diff --git a/src/game/global_data.rs b/src/game/global_data.rs index 25d4534..a8b0149 100644 --- a/src/game/global_data.rs +++ b/src/game/global_data.rs @@ -11,7 +11,6 @@ use crate::game::Phase; use crate::imgur::{get_album_images, Image}; use crate::messages::MessageTemplates; use chrono::Duration; -use rand::prelude::SliceRandom; #[derive(Debug, Clone)] pub struct GlobalData { @@ -127,14 +126,12 @@ impl GlobalData { Ok(()) } - pub async fn get_profile_pic_url(&self) -> Result { - let images: Vec = get_album_images( + pub async fn get_profile_pic_album(&self) -> Result> { + Ok(get_album_images( &self.cfg.imgur_client_id, &self.game_cfg()?.profile_album_hash, ) - .await?; - - Ok(images.choose(&mut rand::thread_rng()).unwrap().link.clone()) + .await?) } pub fn get_phase_name(&self) -> Result {