diff options
Diffstat (limited to 'src/game.rs')
| -rw-r--r-- | src/game.rs | 138 |
1 files changed, 71 insertions, 67 deletions
diff --git a/src/game.rs b/src/game.rs index 47ae18c..d3b0785 100644 --- a/src/game.rs +++ b/src/game.rs @@ -55,7 +55,6 @@ use crate::{ }; #[group] -#[prefix = "game"] #[commands(game, installedgame, ownedgame, updategaem)] pub struct Game; @@ -234,80 +233,85 @@ async fn _game( mut args: Args, min_status: GameStatus, ) -> CommandResult { - let guild = - msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?; - let guild = guild.guild(&ctx).ok_or_else(|| anyhow!("couldn't find guild"))?; + let users = { + let guild = + msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?; + let guild = guild.guild(&ctx).ok_or_else(|| anyhow!("couldn't find guild"))?; - let user_args: Vec<String> = if args.rest().is_empty() { - Vec::new() - } else { - args.quoted().iter::<String>().collect::<StdResult<Vec<_>, ArgError<Infallible>>>()? - }; + let user_args: Vec<String> = if args.rest().is_empty() { + Vec::new() + } else { + args.quoted().iter::<String>().collect::<StdResult<Vec<_>, ArgError<Infallible>>>()? + }; - let mut users = user_args - .into_iter() - .filter_map(|u| { - use std::borrow::Borrow; + let mut users = user_args + .into_iter() + .filter_map(|u| { + use std::borrow::Borrow; - let possible = get_user_id(guild.borrow(), &u); + let possible = get_user_id(guild.borrow(), &u); - debug!("parsed userid {:?}", possible); + debug!("parsed userid {:?}", possible); - match possible { - Err(UserLookupError::NotFound) => { - let _ = util::send( - ctx, - msg.channel_id, - &format!("didn't recognize {}", &u), - msg.tts, - ); - None - }, - Ok(x) => Some(x), - Err(UserLookupError::Ambiguous(x)) => { - let _ = util::send( - ctx, - msg.channel_id, - &format!("too many matches ({}) for {}", x, &u), - msg.tts, - ); - None - }, - } - }) - .filter_map(|uid| { - let res = DISCORD_MAP.get(&uid).map(|s| s.to_lowercase()); + match possible { + Err(UserLookupError::NotFound) => { + let _ = util::send( + ctx, + msg.channel_id, + &format!("didn't recognize {}", &u), + msg.tts, + ); + None + }, + Ok(x) => Some(x), + Err(UserLookupError::Ambiguous(x)) => { + let _ = util::send( + ctx, + msg.channel_id, + &format!("too many matches ({}) for {}", x, &u), + msg.tts, + ); + None + }, + } + }) + .filter_map(|uid| { + let res = DISCORD_MAP.get(&uid).map(|s| s.to_lowercase()); - if let None = res { - let _ = info!("user {} is not recognized", uid); - } + if let None = res { + let _ = info!("user {} is not recognized", uid); + } - res - }) - .collect::<FnvHashSet<_>>(); + res + }) + .collect::<FnvHashSet<_>>(); - let inferred = users.len() == 0; + if users.len() == 0 { + let pairs = guild + .voice_states + .iter() + .filter_map(|(uid, voice)| voice.channel_id.map(|cid| (*uid, cid))) + .collect::<FnvHashMap<_, _>>(); - if users.len() == 0 { - let pairs = guild - .voice_states - .iter() - .filter_map(|(uid, voice)| voice.channel_id.map(|cid| (*uid, cid))) - .collect::<FnvHashMap<_, _>>(); + let channel = + pairs.get(&msg.author.id).cloned().unwrap_or(CONFIG.discord.voice_channel()); - let channel = pairs.get(&msg.author.id).cloned().unwrap_or(CONFIG.discord.voice_channel()); + users = pairs + .iter() + .filter_map(|(uid, cid)| { + if *cid == channel { + DISCORD_MAP.get(uid).map(|s| s.to_lowercase()) + } else { + None + } + }) + .collect::<FnvHashSet<_>>(); + } - users = pairs - .iter() - .filter_map(|(uid, cid)| { - if *cid == channel { - DISCORD_MAP.get(uid).map(|s| s.to_lowercase()) - } else { - None - } - }) - .collect::<FnvHashSet<_>>(); - } + users + }; + + let inferred = users.len() == 0; if inferred && users.len() < 2 || !inferred && users.len() < 1 { info!("too few known users to make game comparison"); @@ -434,7 +438,7 @@ pub async fn updategaem(ctx: &Context, msg: &Message, mut args: Args) -> Command let client = { let data = ctx.data.read().await; - data.get::<HttpKey>().unwrap() + data.get::<HttpKey>().unwrap().clone() }; let arg_user = args.single_quoted::<String>(); @@ -472,7 +476,7 @@ pub async fn updategaem(ctx: &Context, msg: &Message, mut args: Args) -> Command }, }; - let spreadsheet = load_spreadsheet(client).await?; + let spreadsheet = load_spreadsheet(&client).await?; let user_column = (0..spreadsheet.len()) .find(|x| spreadsheet[*x][0].to_lowercase() == username.to_lowercase()); @@ -537,7 +541,7 @@ pub async fn updategaem(ctx: &Context, msg: &Message, mut args: Args) -> Command let client = { let data = ctx.data.read().await; - data.get::<HttpKey>().unwrap() + data.get::<HttpKey>().unwrap().clone() }; let games_owned = client |
