From 749abf78982d2cb99ca00ad08dce4e94803ee3d8 Mon Sep 17 00:00:00 2001 From: jude Date: Thu, 22 Oct 2020 10:31:47 +0100 Subject: [PATCH] added case insensitivity to commands --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 + src/framework.rs | 43 ++++++++++++++++++++++++++++--------------- src/main.rs | 1 + 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4fcbc52..13d08ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,7 +1165,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "0.1.4" +version = "0.1.5" dependencies = [ "Inflector", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index a0e353f..047bc5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "0.1.4" +version = "0.1.5" authors = ["jellywx "] edition = "2018" diff --git a/README.md b/README.md index 0852fbf..36ccb98 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,4 @@ __Other Variables__ * `LOCAL_LANGUAGE` - default `EN`. Specifies the string set to fall back to if a string cannot be found (and to be used with new users) * `THEME_COLOR` - default `8fb677`. Specifies the hex value of the color to use on info message embeds * `WEBHOOK_AVATAR` - default `None`, accepts the path to an image file to be used as the avatar when creating webhooks. **IMPORTANT: image file must be 128x128 or smaller in size** +* `CASE_INSENSITIVE` - default `1`, if `1`, commands will be treated with case insensitivity (so both `$help` and `$HELP` will work) diff --git a/src/framework.rs b/src/framework.rs index c42feac..8437cc0 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -16,7 +16,7 @@ use serenity::{ use log::{error, info, warn}; -use regex::{Match, Regex}; +use regex::{Match, Regex, RegexBuilder}; use std::{collections::HashMap, fmt}; @@ -152,16 +152,6 @@ impl fmt::Debug for Command { } } -// create event handler for bot -pub struct RegexFramework { - commands: HashMap, - command_matcher: Regex, - dm_regex_matcher: Regex, - default_prefix: String, - client_id: u64, - ignore_bots: bool, -} - #[async_trait] pub trait SendIterator { async fn say_lines( @@ -205,6 +195,16 @@ impl SendIterator for ChannelId { } } +pub struct RegexFramework { + commands: HashMap, + command_matcher: Regex, + dm_regex_matcher: Regex, + default_prefix: String, + client_id: u64, + ignore_bots: bool, + case_insensitive: bool, +} + impl RegexFramework { pub fn new>(client_id: T) -> Self { Self { @@ -214,9 +214,16 @@ impl RegexFramework { default_prefix: "".to_string(), client_id: client_id.into(), ignore_bots: true, + case_insensitive: true, } } + pub fn case_insensitive(mut self, case_insensitive: bool) -> Self { + self.case_insensitive = case_insensitive; + + self + } + pub fn default_prefix(mut self, new_prefix: T) -> Self { self.default_prefix = new_prefix.to_string(); @@ -255,7 +262,10 @@ impl RegexFramework { .replace("COMMANDS", command_names.as_str()) .replace("ID", self.client_id.to_string().as_str()); - self.command_matcher = Regex::new(match_string.as_str()).unwrap(); + self.command_matcher = RegexBuilder::new(match_string.as_str()) + .case_insensitive(self.case_insensitive) + .build() + .unwrap(); } } @@ -285,7 +295,10 @@ impl RegexFramework { .replace("COMMANDS", dm_command_names.as_str()) .replace("ID", self.client_id.to_string().as_str()); - self.dm_regex_matcher = Regex::new(match_string.as_str()).unwrap(); + self.dm_regex_matcher = RegexBuilder::new(match_string.as_str()) + .case_insensitive(self.case_insensitive) + .build() + .unwrap(); } } @@ -405,7 +418,7 @@ impl Framework for RegexFramework { let command = self .commands - .get(full_match.name("cmd").unwrap().as_str()) + .get(&full_match.name("cmd").unwrap().as_str().to_lowercase()) .unwrap(); let channel_data = ChannelData::from_channel( msg.channel(&ctx).await.unwrap(), @@ -456,7 +469,7 @@ impl Framework for RegexFramework { else if let Some(full_match) = self.dm_regex_matcher.captures(&msg.content[..]) { let command = self .commands - .get(full_match.name("cmd").unwrap().as_str()) + .get(&full_match.name("cmd").unwrap().as_str().to_lowercase()) .unwrap(); let args = full_match .name("args") diff --git a/src/main.rs b/src/main.rs index 32465e8..74a8658 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,6 +157,7 @@ async fn main() -> Result<(), Box> { let framework = RegexFramework::new(logged_in_id) .default_prefix(DEFAULT_PREFIX.clone()) + .case_insensitive(env::var("CASE_INSENSITIVE").map_or(true, |var| var == "1")) .ignore_bots(env::var("IGNORE_BOTS").map_or(true, |var| var == "1")) // info commands .add_command("ping", &info_cmds::PING_COMMAND)