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 std::env;
use std::panic;
use std::thread;
struct Handler;
@ -194,20 +196,57 @@ fn wc_player(msg: Message) {
).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() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set");
// 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
// by Discord for bot users.
let mut client = Client::new(&token, Handler);
thread::Builder::new()
.spawn(|| {
loop {
println!("{:?}", panic::catch_unwind(|| territory_livefeed()));
}
})
.unwrap();
// Finally, start a single shard, and start listening to events.
//
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
loop {
// 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
// by Discord for bot users.
let mut client = Client::new(&token, Handler);
// Finally, start a single shard, and start listening to events.
//
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
}
}