-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
282 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::event::Event; | ||
|
||
use super::Router; | ||
|
||
|
||
pub struct CommandRouter { | ||
pub prefixes: Vec<String>, | ||
pub command: Vec<String>, | ||
} | ||
|
||
impl Default for CommandRouter { | ||
fn default() -> Self { | ||
Self { | ||
prefixes: vec!["/".into()], | ||
command: ["help".into()].to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl CommandRouter { | ||
pub fn new<S: Into<String>, C: IntoIterator<Item = S>>( | ||
prefixes: Vec<String>, | ||
command: C, | ||
) -> Self { | ||
Self { | ||
prefixes, | ||
command: command.into_iter().map(Into::into).collect(), | ||
} | ||
} | ||
|
||
pub fn command<S: Into<String>, C: IntoIterator<Item = S>>(command: C) -> Self { | ||
Self { | ||
command: command.into_iter().map(Into::into).collect(), | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl Router for CommandRouter { | ||
fn matches(&self, event: &dyn Event) -> bool { | ||
if let Ok(val) = event.content().downcast::<&str>() { | ||
for prefix in &self.prefixes { | ||
if val.starts_with(prefix) { | ||
let command = val.strip_prefix(prefix).unwrap(); | ||
if self.command.iter().any(|c| command.starts_with(c)) { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_command_router() { | ||
let router = CommandRouter::default(); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"/help".to_string())); | ||
assert!(router.matches(&"/help@bot".to_string())); | ||
assert!(!router.matches(&"/not help".to_string())); | ||
|
||
let router = CommandRouter::command(["cmd", "command"]); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"/cmd".to_string())); | ||
assert!(router.matches(&"/cmd@bot".to_string())); | ||
assert!(!router.matches(&"/not cmd".to_string())); | ||
assert!(router.matches(&"/command".to_string())); | ||
|
||
let router = CommandRouter::new(vec!["!".to_string()], ["cmd"]); | ||
assert!(!router.matches(&"help".to_string())); | ||
assert!(router.matches(&"!cmd".to_string())); | ||
assert!(router.matches(&"!cmd@bot".to_string())); | ||
assert!(!router.matches(&"!not cmd".to_string())); | ||
assert!(!router.matches(&"/cmd arg1 arg2".to_string())) | ||
} | ||
} |
Oops, something went wrong.