From 005b629a21d1dc524fcfdc0de58418aa1b4a8b63 Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Mon, 12 Feb 2018 11:02:45 +0100 Subject: [PATCH] Prefix configuration --- .../down.sql | 1 + .../up.sql | 4 +++ src/commands/feed.rs | 12 ++----- src/commands/mod.rs | 36 +++++++++++++++++++ src/main.rs | 26 ++++++++++---- src/models.rs | 7 ++++ src/schema.rs | 8 +++++ 7 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 migrations/2018-02-11-111637_create_discord_prefix_list/down.sql create mode 100644 migrations/2018-02-11-111637_create_discord_prefix_list/up.sql diff --git a/migrations/2018-02-11-111637_create_discord_prefix_list/down.sql b/migrations/2018-02-11-111637_create_discord_prefix_list/down.sql new file mode 100644 index 0000000..0597a79 --- /dev/null +++ b/migrations/2018-02-11-111637_create_discord_prefix_list/down.sql @@ -0,0 +1 @@ +DROP TABLE prefixes \ No newline at end of file diff --git a/migrations/2018-02-11-111637_create_discord_prefix_list/up.sql b/migrations/2018-02-11-111637_create_discord_prefix_list/up.sql new file mode 100644 index 0000000..939b15c --- /dev/null +++ b/migrations/2018-02-11-111637_create_discord_prefix_list/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE prefixes ( + id BIGINT PRIMARY KEY, + prefix VARCHAR NOT NULL +) \ No newline at end of file diff --git a/src/commands/feed.rs b/src/commands/feed.rs index 0d4b271..e9566e5 100644 --- a/src/commands/feed.rs +++ b/src/commands/feed.rs @@ -4,7 +4,6 @@ use serenity::model::*; use ::diesel; use ::diesel::prelude::*; -use ::diesel::pg::PgConnection; use ::diesel::result::DatabaseErrorKind; use ::serde_json; @@ -12,11 +11,13 @@ use ::serde_json::Value; use ::reqwest; -use ::std::{env, thread}; +use ::std::thread; use failure; use failure::Error; +use super::establish_connection; + pub fn territory_livefeed() { use models::LivefeedListener; use schema::livefeedlisteners::dsl::*; @@ -179,10 +180,3 @@ pub fn wc_unwarfeed(msg: &Message) -> Result<(), Error> { Ok(()) } - -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)) -} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index fbfc084..678305a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -8,9 +8,15 @@ use ::reqwest; use ::serde_json; use ::serde_json::Value; +use diesel; +use diesel::pg::PgConnection; +use diesel::prelude::*; + use failure; use failure::Error; +use std::env; + pub mod player; pub mod guild; pub mod feed; @@ -19,6 +25,29 @@ pub use player::*; pub use guild::*; pub use feed::{wc_livefeed, wc_unlivefeed, wc_warfeed, wc_unwarfeed}; +pub fn wc_prefix(command: &str, msg: &Message) -> Result<(), Error> { + use models::PrefixConfig; + use schema::prefixes::dsl::*; + + let connection = establish_connection(); + + let new_prefix = &command[7..].to_owned(); + + diesel::insert_into(prefixes) + .values(&PrefixConfig { + id: msg.guild_id().unwrap().0 as i64, + prefix: new_prefix.clone() + }) + .on_conflict(id) + .do_update() + .set(prefix.eq(new_prefix)) + .execute(&connection)?; + + msg.channel_id.say(format!("Prefix is now {:?}", new_prefix))?; + + Ok(()) +} + pub fn wc_status(msg: &Message) -> Result<(), Error> { let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum")?.text().unwrap(); @@ -205,4 +234,11 @@ pub fn wc_shout(command: &str, msg: &Message) -> Result<(), Error> { } msg.channel_id.say("Success!")?; Ok(()) +} + +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)) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 450723a..b74ec68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ extern crate serde_json; #[macro_use] extern crate diesel; +use diesel::prelude::*; #[macro_use] extern crate failure; @@ -36,6 +37,7 @@ use std::thread; mod models; mod schema; +use schema::prefixes; mod commands; use commands::*; @@ -84,7 +86,19 @@ impl EventHandler for Handler { // Event handlers are dispatched through multi-threading, and so multiple // of a single event can be dispatched simultaneously. fn on_message(&self, _: Context, msg: Message) { - if let Err(error) = self.process_message(&msg) { + let cfg = Config::generate().unwrap(); + let prefix = cfg.prefix; + let connection = commands::establish_connection(); + + let prefix = prefixes::table + .select(prefixes::prefix) + .filter(prefixes::id.eq(msg.guild_id().unwrap_or(GuildId(0)).0 as i64)) + .load::(&connection) + .expect("Error loading prefix").get(0) + .map(|x| x.to_owned()) + .unwrap_or(prefix); + + if let Err(error) = self.process_message(&msg, &prefix) { //eprintln!("Error: {}", error); let _ = msg.channel_id.say(format!("Error: {}", error)); for cause in error.causes().skip(1) { @@ -92,8 +106,7 @@ impl EventHandler for Handler { let _ = msg.channel_id.say(format!("caused by: {}", cause)); } } - let cfg = Config::generate().unwrap(); - let prefix = cfg.prefix; + if msg.content.starts_with(&prefix) { let command = &msg.content[prefix.len()..]; // log @@ -117,10 +130,7 @@ impl EventHandler for Handler { } impl Handler { - fn process_message(&self, msg: &Message) -> Result<(), Error> { - let cfg = Config::generate()?; - let prefix = cfg.prefix; - + fn process_message(&self, msg: &Message, prefix: &str) -> Result<(), Error> { if msg.content == "<@392763365409292298> has big gay" { let _ = msg.channel_id.broadcast_typing(); thread::sleep_ms(5000); @@ -165,6 +175,8 @@ impl Handler { } else { "<@210743594061922306>".parse::().unwrap().dm(|f| f.content(format!("{} tried to shout {:?}", msg.author, &command[6..]))).unwrap(); } + } else if command.starts_with("prefix ") { + wc_prefix(command, msg)?; } else { bail!(ResponseError::UnknownCommand { name: command.to_owned() }); } diff --git a/src/models.rs b/src/models.rs index a8bbe39..e10f102 100644 --- a/src/models.rs +++ b/src/models.rs @@ -10,4 +10,11 @@ pub struct LivefeedListener { #[table_name="warfeedlisteners"] pub struct WarfeedListener { pub id: i64 +} + +#[derive(Queryable, Insertable)] +#[table_name="prefixes"] +pub struct PrefixConfig { + pub id: i64, + pub prefix: String } \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs index 710f178..a28e9f1 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,6 +4,13 @@ table! { } } +table! { + prefixes (id) { + id -> Int8, + prefix -> Varchar, + } +} + table! { warfeedlisteners (id) { id -> Int8, @@ -12,5 +19,6 @@ table! { allow_tables_to_appear_in_same_query!( livefeedlisteners, + prefixes, warfeedlisteners, );