Show online members

This commit is contained in:
Sakuhl 2018-02-26 13:24:25 +01:00
parent f13b082796
commit 23a17143b4

View File

@ -10,6 +10,8 @@ use ::serde_json::Value;
use failure::Error;
use ::std::collections::HashSet;
use ::ResponseError;
pub fn wc_guild(command: &str, msg: &Message) -> Result<(), Error> {
@ -65,40 +67,42 @@ pub fn wc_guild(command: &str, msg: &Message) -> Result<(), Error> {
Ok(message)
})().unwrap_or_else(|e| format!("\nError getting territories: {}", e));
let online_players = (|| {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayers")?;
let players: Value = serde_json::from_reader(resp)?;
let mut online_players = HashSet::new();
for key in players.as_object().unwrap().keys() {
if key == "request" {
continue;
}
let players = players.get(key).unwrap().as_array().unwrap();
for p in players {
online_players.insert(p.as_str().unwrap().to_owned());
}
}
let guild_online_players = guild.members.iter().filter(|x| online_players.contains(&x.name)).map(|x| &*x.name).collect::<Vec<_>>().as_slice().join(", ");
Ok(guild_online_players)
})().unwrap_or_else(|e: Error| format!("\nError getting online players: {}", e));
let full_msg = format!(
"**Guild**: {}
**prefix**: {}
**Created**: {}
**Level**: {}
**Members**: {}",
**Members**: {}
**Online members**: {}",
guild.name,
guild.prefix,
guild.created_friendly,
guild.level,
guild.members.len()
guild.members.len(),
online_players
) + &message;
for part in full_msg.split('\n').collect::<Vec<_>>().chunks(48) {
msg.channel_id.say(part.join("\n"))?;
}
/*
msg.channel_id.say(
format!(
"**Guild**: {}
**prefix**: {}
**Created**: {}
**Level**: {}
**Members**: {}
**Territories**: {}",
guild.name,
guild.prefix,
guild.created_friendly,
guild.level,
guild.members.len(),
territories_count
)
)?;
};
*/
Ok(())
}