diff options
| author | Nathan Perry <np@nathanperry.dev> | 2019-07-20 15:48:35 -0400 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2019-07-20 15:48:35 -0400 |
| commit | 479bb8d584b138054acc6567b72cb3076832e79c (patch) | |
| tree | e2bc0b58282d0627e307d5203ae8a5c0c4798a37 | |
| parent | ca46177a5df0eac30b3ca2bc33c5015e03f18688 (diff) | |
time-based prefix restrictions
| -rw-r--r-- | src/main.rs | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 90e6e6e..aac7587 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ use std::{ }, }; +use chrono::Datelike; use dotenv::dotenv; use failure::Error; use fnv::{FnvHashMap, FnvHashSet}; @@ -175,20 +176,41 @@ fn run() -> Result<()> { return false; } - if RESTRICTED_PREFIXES.iter().any(|prefix| message.content.starts_with(prefix)) - && restrict_ids.contains(&message.author.id.0) { - info!("rejecting command '{}' from user '{}': restricted prefix", cmd, message.author.name); - match crate::commands::send_result(message.channel_id, "no", message.tts) { - Err(e) => error!("sending restricted prefix response: {}", e), - Ok(msg_id) => { - let mut mp = MESSAGE_WATCH.lock(); - mp.insert(message.id, msg_id); - } + if message.author.id.0 == owner_id { + return true; + } + + let restricted_prefix = RESTRICTED_PREFIXES.iter().any(|prefix| message.content.starts_with(prefix)); + if !restricted_prefix { + return true; + } + + const PERMITTED_WEEKDAY: chrono::Weekday = chrono::Weekday::Tue; + + let restricted_user = restrict_ids.contains(&message.author.id.0); + let flip_restriction_day = chrono::Local::now().weekday() == PERMITTED_WEEKDAY; + + if restricted_user == flip_restriction_day { + return true; + } + + let reason = if !flip_restriction_day { + "restricted prefix".to_owned() + } else { + format!("it is {:?}", PERMITTED_WEEKDAY) + }; + + info!("rejecting command '{}' from user '{}': {}", cmd, message.author.name, reason); + + match crate::commands::send_result(message.channel_id, "no", message.tts) { + Err(e) => error!("sending restricted prefix response: {}", e), + Ok(msg_id) => { + let mut mp = MESSAGE_WATCH.lock(); + mp.insert(message.id, msg_id); } - return false; } - true + return false; }) .after(|_ctx, msg, cmd, err| { match err { |
