Add territory livefeed commands

This commit is contained in:
Sakuhl 2018-01-03 13:47:12 +01:00
parent e1d5d59aa6
commit 65e031a238
10 changed files with 149 additions and 5 deletions

32
Cargo.lock generated
View File

@ -132,6 +132,26 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "diesel"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_derives 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pq-sys 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "diesel_derives"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "0.4.2" version = "0.4.2"
@ -631,6 +651,14 @@ name = "pkg-config"
version = "0.3.9" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pq-sys"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "0.3.15" version = "0.3.15"
@ -1078,6 +1106,7 @@ dependencies = [
name = "wynnrobot" name = "wynnrobot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"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)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1106,6 +1135,8 @@ dependencies = [
"checksum core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "065a5d7ffdcbc8fa145d6f0746f3555025b9097a9e9cda59f7467abae670c78d" "checksum core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "065a5d7ffdcbc8fa145d6f0746f3555025b9097a9e9cda59f7467abae670c78d"
"checksum crc 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5d02c0aac6bd68393ed69e00bbc2457f3e89075c6349db7189618dc4ddc1d7" "checksum crc 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5d02c0aac6bd68393ed69e00bbc2457f3e89075c6349db7189618dc4ddc1d7"
"checksum crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e34988f7e069e0b2f3bfc064295161e489b2d4e04a2e4248fb94360cdf00b4ec" "checksum crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e34988f7e069e0b2f3bfc064295161e489b2d4e04a2e4248fb94360cdf00b4ec"
"checksum diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de51f2e2321a7db9bdfd7e3457c6ce11dce5009cad7ff9ac25a04879239e5fe6"
"checksum diesel_derives 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "274aeed033dcd9e40f472cd282a3692512fef153283ec4c745873517d0e67f3f"
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum evzht9h3nznqzwl 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "74a8c39674021da88007af7b82f9ff8be7da254706dd279ad10e374906b4b934" "checksum evzht9h3nznqzwl 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "74a8c39674021da88007af7b82f9ff8be7da254706dd279ad10e374906b4b934"
"checksum flate2 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "e6234dd4468ae5d1e2dbb06fe2b058696fdc50a339c68a393aefbf00bc81e423" "checksum flate2 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "e6234dd4468ae5d1e2dbb06fe2b058696fdc50a339c68a393aefbf00bc81e423"
@ -1161,6 +1192,7 @@ dependencies = [
"checksum phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6b07ffcc532ccc85e3afc45865469bf5d9e4ef5bfcf9622e3cfe80c2d275ec03" "checksum phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6b07ffcc532ccc85e3afc45865469bf5d9e4ef5bfcf9622e3cfe80c2d275ec03"
"checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2" "checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = "07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2"
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
"checksum pq-sys 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4dfb5e575ef93a1b7b2a381d47ba7c5d4e4f73bff37cee932195de769aad9a54"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e7944d95d25ace8f377da3ac7068ce517e4c646754c43a1b1849177bbf72e59" "checksum rand 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e7944d95d25ace8f377da3ac7068ce517e4c646754c43a1b1849177bbf72e59"
"checksum redox_syscall 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "07b8f011e3254d5a9b318fde596d409a0001c9ae4c6e7907520c2eaa4d988c99" "checksum redox_syscall 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "07b8f011e3254d5a9b318fde596d409a0001c9ae4c6e7907520c2eaa4d988c99"

View File

@ -10,3 +10,4 @@ serde_derive = "1.0.24"
serde_json = "1.0.8" serde_json = "1.0.8"
serenity = "0.4.5" serenity = "0.4.5"
wynncraft = { git = "https://gitlab.com/Sakuhl/wynncraft" } wynncraft = { git = "https://gitlab.com/Sakuhl/wynncraft" }
diesel = { version = "1.0", features = ["postgres"] }

0
migrations/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();

View File

@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

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

View File

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

View File

@ -9,10 +9,18 @@ extern crate reqwest;
extern crate serde_json; extern crate serde_json;
use serde_json::Value; use serde_json::Value;
#[macro_use]
extern crate diesel;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use std::env; use std::env;
use std::panic; use std::panic;
use std::thread; use std::thread;
mod models;
mod schema;
struct Handler; struct Handler;
impl EventHandler for Handler { impl EventHandler for Handler {
@ -34,6 +42,10 @@ 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") {
wc_livefeed(msg);
} else if msg.content.starts_with("wc!unlivefeed") {
wc_unlivefeed(msg);
} }
} }
@ -196,7 +208,41 @@ fn wc_player(msg: Message) {
).unwrap(); ).unwrap();
} }
fn wc_livefeed(msg: Message) {
use models::LivefeedListener;
use schema::livefeedlisteners;
let connection = establish_connection();
diesel::insert_into(livefeedlisteners::table)
.values(&LivefeedListener {
id: msg.channel_id.0 as i64
})
.execute(&connection)
.expect("Error saving new post");
msg.channel_id.say("Success!").unwrap();
}
fn wc_unlivefeed(msg: Message) {
use schema::livefeedlisteners::dsl::*;
let connection = establish_connection();
let channel_id = msg.channel_id.0 as i64;
let num_deleted = diesel::delete(livefeedlisteners.filter(id.eq(channel_id)))
.execute(&connection)
.expect("Error deleting posts");
msg.channel_id.say("Success!").unwrap();
}
fn territory_livefeed() { fn territory_livefeed() {
use models::LivefeedListener;
use schema::livefeedlisteners::dsl::*;
let connection = establish_connection();
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap(); let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
let mut territories: Value = serde_json::from_reader(resp).unwrap(); let mut territories: Value = serde_json::from_reader(resp).unwrap();
@ -211,13 +257,17 @@ fn territory_livefeed() {
let value = territories.get("territories").unwrap().as_object().unwrap().get(key).unwrap(); 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(); 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() { 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 {})", for listener in livefeedlisteners
.load::<LivefeedListener>(&connection)
.expect("Error loading listeners") {
ChannelId(listener.id as u64).say(format!("{}: ~~{}~~ -> **{}**",
value.get("territory").unwrap().as_str().unwrap(), value.get("territory").unwrap().as_str().unwrap(),
new_value.get("guild").unwrap().as_str().unwrap(), value.get("guild").unwrap().as_str().unwrap(),
value.get("guild").unwrap().as_str().unwrap() new_value.get("guild").unwrap().as_str().unwrap()
)).unwrap(); )).unwrap();
} }
} }
}
territories = new_territories; territories = new_territories;
} }
@ -250,3 +300,10 @@ fn main() {
} }
} }
} }
pub fn establish_connection() -> PgConnection {
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}

7
src/models.rs Normal file
View File

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

5
src/schema.rs Normal file
View File

@ -0,0 +1,5 @@
table! {
livefeedlisteners (id) {
id -> Int8,
}
}