added case insensitivity to commands

This commit is contained in:
jude 2020-10-22 10:31:47 +01:00
parent d50390c4a5
commit 749abf7898
5 changed files with 32 additions and 17 deletions

2
Cargo.lock generated
View File

@ -1165,7 +1165,7 @@ dependencies = [
[[package]]
name = "reminder_rs"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"Inflector",
"async-trait",

View File

@ -1,6 +1,6 @@
[package]
name = "reminder_rs"
version = "0.1.4"
version = "0.1.5"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"

View File

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

View File

@ -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<String, &'static Command>,
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<String, &'static Command>,
command_matcher: Regex,
dm_regex_matcher: Regex,
default_prefix: String,
client_id: u64,
ignore_bots: bool,
case_insensitive: bool,
}
impl RegexFramework {
pub fn new<T: Into<u64>>(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<T: ToString>(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")

View File

@ -157,6 +157,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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)