Cache guild prefixes and optimize logging

This commit is contained in:
Sakuhl 2018-02-07 13:06:23 +01:00
parent de8605daf1
commit 4d9dbf9899
4 changed files with 24 additions and 8 deletions

3
Cargo.lock generated
View File

@ -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)",

View File

@ -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"

View File

@ -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() });

View File

@ -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<HashMap<String, String>> = 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::<User>() {
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::<User>() {
let _ = user.dm(|f| f.content(format!("{}: {}", msg.author, command)));
}
if command.starts_with("guild ") {
wc_guild(command, msg)?;
} else if command == "topguilds" {