use super::config::CONFIG; use cmd_lib::{CmdResult, Process}; use serde::Deserialize; use serenity::model::channel::Message; #[derive(Deserialize, Debug)] pub struct Command { trigger: String, command: String, users: Vec, } pub fn print_commands() { CONFIG.commands.iter().for_each(|c| println!("{:?}", c)); } pub fn find_matching(message: &str) -> Option<&Command> { CONFIG.commands.iter().find(|&c| message[1..] == c.trigger) } impl Command { /// Check permissions for a command and execute it. /// Returns an error for insufficient permissions or non-zero return codes. pub fn execute(&self, msg: &Message) -> Result<(), String> { println!( "User {} tried to execute command {}", &msg.author, &msg.content ); if !self.users.contains(&msg.author.id.0) { return Err("You don’t have the permissions to execute this command.".to_owned()); } Process::new(self.command.clone()) .wait::() .map_err(|e| format!("{:?}", e)) } }