use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use async_trait::async_trait;
use reqwest::Client;
use std::error::Error;
#[async_trait]
pub trait Command: Send + Sync {
async fn execute(&self, client: &Client, token: &str, channel_id: &str, args: &str) -> Result<(), Box<dyn Error>>;
}
pub struct CommandRouter {
commands: HashMap<String, Arc<dyn Command>>,
}
impl CommandRouter {
pub fn new() -> Self {
Self {
commands: HashMap::new(),
}
}
pub fn register_command(&mut self, name: &str, command: Arc<dyn Command>) {
self.commands.insert(name.to_string(), command);
}
pub async fn dispatch(&self, client: &Client, token: &str, channel_id: &str, content: &str) -> Result<(), Box<dyn Error>> {
let parts: Vec<&str> = content.splitn(2, ' ').collect();
let command_name = parts[0];
let args = if parts.len() > 1 { parts[1] } else { "" };
if let Some(command) = self.commands.get(command_name) {
command.execute(client, token, channel_id, args).await?;
} else {
println!("Command not found: {}", command_name);
}
Ok(())
}
}