Add war feed command

This commit is contained in:
Sakuhl 2018-01-12 19:27:31 +01:00
parent feda6d465b
commit dd020543de
6 changed files with 122 additions and 6 deletions

2
Cargo.lock generated
View File

@ -1100,7 +1100,7 @@ dependencies = [
[[package]] [[package]]
name = "wynnrobot" name = "wynnrobot"
version = "0.9.1" version = "0.9.2"
dependencies = [ dependencies = [
"diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -0,0 +1 @@
DROP TABLE warfeedlisteners

View File

@ -0,0 +1,3 @@
CREATE TABLE warfeedlisteners (
id BIGINT PRIMARY KEY
)

View File

@ -42,14 +42,18 @@ impl EventHandler for Handler {
wc_status(msg); wc_status(msg);
} else if msg.content.starts_with("wc!player ") { } else if msg.content.starts_with("wc!player ") {
wc_player(msg); wc_player(msg);
} else if msg.content.starts_with("wc!livefeed") { } else if msg.content == "wc!livefeed" {
wc_livefeed(msg); wc_livefeed(msg);
} else if msg.content.starts_with("wc!unlivefeed") { } else if msg.content == "wc!unlivefeed" {
wc_unlivefeed(msg); wc_unlivefeed(msg);
} else if msg.content == "wc!info" || msg.content == "wc!help" || msg.content == "wc!" { } else if msg.content == "wc!info" || msg.content == "wc!help" || msg.content == "wc!" {
wc_info(msg); wc_info(msg);
} else if msg.content.starts_with("wc!crop ") { } else if msg.content.starts_with("wc!crop ") {
wc_crop(msg); wc_crop(msg);
} else if msg.content == "wc!warfeed" {
wc_warfeed(msg);
} else if msg.content == "wc!unwarfeed" {
wc_unwarfeed(msg);
} }
} }
@ -223,7 +227,7 @@ fn wc_livefeed(msg: Message) {
id: msg.channel_id.0 as i64 id: msg.channel_id.0 as i64
}) })
.execute(&connection) .execute(&connection)
.expect("Error saving new post"); .expect("Error saving new listener");
msg.channel_id.say("Success!").unwrap(); msg.channel_id.say("Success!").unwrap();
} }
@ -236,7 +240,36 @@ fn wc_unlivefeed(msg: Message) {
let channel_id = msg.channel_id.0 as i64; let channel_id = msg.channel_id.0 as i64;
diesel::delete(livefeedlisteners.filter(id.eq(channel_id))) diesel::delete(livefeedlisteners.filter(id.eq(channel_id)))
.execute(&connection) .execute(&connection)
.expect("Error deleting posts"); .expect("Error deleting listener");
msg.channel_id.say("Success!").unwrap();
}
fn wc_warfeed(msg: Message) {
use models::WarfeedListener;
use schema::warfeedlisteners;
let connection = establish_connection();
diesel::insert_into(warfeedlisteners::table)
.values(&WarfeedListener {
id: msg.channel_id.0 as i64
})
.execute(&connection)
.expect("Error saving new listener");
msg.channel_id.say("Success!").unwrap();
}
fn wc_unwarfeed(msg: Message) {
use schema::warfeedlisteners::dsl::*;
let connection = establish_connection();
let channel_id = msg.channel_id.0 as i64;
diesel::delete(warfeedlisteners.filter(id.eq(channel_id)))
.execute(&connection)
.expect("Error deleting listener");
msg.channel_id.say("Success!").unwrap(); msg.channel_id.say("Success!").unwrap();
} }
@ -291,10 +324,63 @@ fn territory_livefeed() {
} }
} }
fn war_livefeed() {
use models::WarfeedListener;
use schema::warfeedlisteners::dsl::*;
let connection = establish_connection();
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayers").unwrap();
let mut players: Value = serde_json::from_reader(resp).unwrap();
loop {
thread::sleep_ms(50_000); // 50 seconds
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayers").unwrap();
let new_players: Value = serde_json::from_reader(resp).unwrap();
for key in new_players.as_object().unwrap().keys() {
if key == "request" {
continue;
}
if !key.starts_with("WAR") {
continue;
}
let war_players = new_players.get(key).unwrap().as_array().unwrap();
for player in war_players.into_iter() {
let old_server = players.as_object().unwrap().iter().map(|(server, players)| {
if server == "request" {
(&**server, false)
} else {
(&**server, players.as_array().unwrap().contains(player))
}
}).filter(|&(_, is_in_it)| is_in_it).next().unwrap_or(("WC?", true)).0;
if old_server != key {
for listener in warfeedlisteners
.load::<WarfeedListener>(&connection)
.expect("Error loading listeners") {
let _ = ChannelId(listener.id as u64).say(format!("{}: ~~{}~~ -> **{}**",
player.as_str().unwrap(),
old_server,
key
));
}
}
}
}
players = new_players;
}
}
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");
env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
/*
thread::Builder::new() thread::Builder::new()
.spawn(|| { .spawn(|| {
loop { loop {
@ -302,6 +388,15 @@ fn main() {
} }
}) })
.unwrap(); .unwrap();
*/
thread::Builder::new()
.spawn(|| {
loop {
println!("{:?}", panic::catch_unwind(|| war_livefeed()));
}
})
.unwrap();
loop { 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

View File

@ -1,7 +1,13 @@
use super::schema::livefeedlisteners; use super::schema::*;
#[derive(Queryable, Insertable)] #[derive(Queryable, Insertable)]
#[table_name="livefeedlisteners"] #[table_name="livefeedlisteners"]
pub struct LivefeedListener { pub struct LivefeedListener {
pub id: i64 pub id: i64
} }
#[derive(Queryable, Insertable)]
#[table_name="warfeedlisteners"]
pub struct WarfeedListener {
pub id: i64
}

View File

@ -3,3 +3,14 @@ table! {
id -> Int8, id -> Int8,
} }
} }
table! {
warfeedlisteners (id) {
id -> Int8,
}
}
allow_tables_to_appear_in_same_query!(
livefeedlisteners,
warfeedlisteners,
);