Add territory live feed

This commit is contained in:
Sakuhl 2018-01-03 00:20:23 +01:00
parent 858f4134a0
commit e1d5d59aa6

View File

@ -10,6 +10,8 @@ extern crate serde_json;
use serde_json::Value; use serde_json::Value;
use std::env; use std::env;
use std::panic;
use std::thread;
struct Handler; struct Handler;
@ -194,10 +196,46 @@ fn wc_player(msg: Message) {
).unwrap(); ).unwrap();
} }
fn territory_livefeed() {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let mut territories: Value = serde_json::from_reader(resp).unwrap();
loop {
thread::sleep_ms(10_000); // 10 seconds
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let new_territories: Value = serde_json::from_reader(resp).unwrap();
for key in territories.get("territories").unwrap().as_object().unwrap().keys() {
let value = territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap();
let new_value = new_territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap();
if value.get("guild").unwrap().as_str().unwrap() != new_value.get("guild").unwrap().as_str().unwrap() {
ChannelId(393082718453235715).say(format!("{} has been captured by {} (previously owned by {})",
value.get("territory").unwrap().as_str().unwrap(),
new_value.get("guild").unwrap().as_str().unwrap(),
value.get("guild").unwrap().as_str().unwrap()
)).unwrap();
}
}
territories = new_territories;
}
}
fn main() { fn main() {
// Configure the client with your Discord bot token in the environment. // Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set"); let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set");
thread::Builder::new()
.spawn(|| {
loop {
println!("{:?}", panic::catch_unwind(|| territory_livefeed()));
}
})
.unwrap();
loop {
// Create a new instance of the Client, logging in as a bot. This will // Create a new instance of the Client, logging in as a bot. This will
// automatically prepend your bot token with "Bot ", which is a requirement // automatically prepend your bot token with "Bot ", which is a requirement
// by Discord for bot users. // by Discord for bot users.
@ -211,3 +249,4 @@ fn main() {
println!("Client error: {:?}", why); println!("Client error: {:?}", why);
} }
} }
}