Skip to content

Commit

Permalink
fix(router): fix command router (#26)
Browse files Browse the repository at this point in the history
* fix(router): fix command router

* chore: bump version

* fix: code lint
  • Loading branch information
fu050409 authored Oct 27, 2024
1 parent c2e4336 commit 8e4987e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-router.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aionbot-core": patch:fix
---

Fix command router matches for different prefixes.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions crates/aionbot-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ pub trait Event: Any + Send + Sync {
}
fn as_any(&self) -> &dyn Any;
}

impl Event for String {
fn event_type(&self) -> &str {
"string_event"
}

fn as_any(&self) -> &dyn Any {
self
}

fn content(&self) -> Box<dyn Any> {
let str: &str = self.clone().leak();
Box::new(str)
}
}
51 changes: 50 additions & 1 deletion crates/aionbot-core/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,35 @@ pub struct CommandRouter {
pub command: String,
}

impl Default for CommandRouter {
fn default() -> Self {
Self {
prefixes: vec!["/".to_string()],
command: "help".to_string(),
}
}
}

impl CommandRouter {
pub fn new(prefixes: Vec<String>, command: String) -> Self {
Self { prefixes, command }
}

pub fn command(command: impl ToString) -> Self {
Self {
command: command.to_string(),
..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 command == self.command {
if command.starts_with(self.command.as_str()) {
return true;
}
}
Expand All @@ -170,3 +192,30 @@ impl Router for CommandRouter {
}
}
}

#[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");
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()));

let router = CommandRouter::new(vec!["!".to_string()], "cmd".to_string());
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()))
}
}

0 comments on commit 8e4987e

Please sign in to comment.