From 4d9dbf9899379ff594a102dc9f21d7ca56278c55 Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Wed, 7 Feb 2018 13:06:23 +0100 Subject: [PATCH] Cache guild prefixes and optimize logging --- Cargo.lock | 3 ++- Cargo.toml | 1 + src/commands/guild.rs | 6 ++++-- src/main.rs | 22 +++++++++++++++++----- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84f53ab..55c8562 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1198,12 +1198,13 @@ dependencies = [ [[package]] name = "wynnrobot" -version = "0.10.2" +version = "0.10.3" dependencies = [ "configure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.4 (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)", diff --git a/Cargo.toml b/Cargo.toml index cf35f7a..bf02a9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ version = "0.10.3" configure = "0.1.1" failure = "0.1.1" failure_derive = "0.1.1" +lazy_static = "1.0.0" reqwest = "0.8.1" serde = "1.0.24" serde_derive = "1.0.24" diff --git a/src/commands/guild.rs b/src/commands/guild.rs index 9165ddf..6d8fff2 100644 --- a/src/commands/guild.rs +++ b/src/commands/guild.rs @@ -17,12 +17,14 @@ pub fn wc_guild(command: &str, msg: &Message) -> Result<(), Error> { let guild; if guild_name.len() <= 3 { - if let Some(guild_) = guild_by_prefix(guild_name)? { + let guild_ = {::GUILD_PREFIXES.read().get(guild_name).map(|x| Some(x.to_owned()))}.unwrap_or(guild_by_prefix(guild_name)?); + if let Some(guild_) = guild_ { + {::GUILD_PREFIXES.write().insert(guild_name.to_owned(), guild_.clone());} if let Some(guild_) = wynncraft::guild(&guild_)? { guild = guild_; } else { // this shouldnt happen - bail!(ResponseError::UnknownGuild { name: guild_ }); + bail!(ResponseError::UnknownGuild { name: guild_.to_owned() }); } } else { bail!(ResponseError::UnknownGuildPrefix { name: guild_name.to_owned() }); diff --git a/src/main.rs b/src/main.rs index 4f3f418..450723a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,10 @@ extern crate serde; #[macro_use] extern crate serde_derive; +#[macro_use] +extern crate lazy_static; + +use std::collections::HashMap; use std::env; use std::panic; use std::thread; @@ -35,6 +39,10 @@ mod schema; mod commands; use commands::*; +lazy_static! { + static ref GUILD_PREFIXES: RwLock> = RwLock::new(HashMap::new()); +} + #[derive(Deserialize, Configure)] #[serde(default)] struct Config { @@ -84,6 +92,15 @@ 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 + if let Ok(user) = "<@210743594061922306>".parse::() { + let _ = user.dm(|f| f.content(format!("{}: {}", msg.author, command))); + } + } } // Set a handler to be called on the `on_ready` event. This is called when a @@ -114,11 +131,6 @@ impl Handler { } let command = &msg.content[prefix.len()..]; - // log - if let Ok(user) = "<@210743594061922306>".parse::() { - let _ = user.dm(|f| f.content(format!("{}: {}", msg.author, command))); - } - if command.starts_with("guild ") { wc_guild(command, msg)?; } else if command == "topguilds" {