rceaadb/src/commands.rs
2020-01-03 11:19:53 +01:00

47 lines
1.3 KiB
Rust

use super::config::*;
use cmd_lib::{CmdResult, Process};
use serde::Deserialize;
use serenity::model::channel::Message;
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Command {
trigger: String,
command: String,
users: Vec<u64>,
}
pub fn print_commands() {
CONFIG.commands.iter().for_each(|c| println!("{:?}", c));
}
pub fn find_matching<'a>(message: &str, cfg: &'a Config) -> Option<&'a Command> {
cfg.commands
.iter()
.find(|&c| message[cfg.prefix.len()..] == 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::<CmdResult>()
.map_err(|e| format!("{:?}", e))
}
pub fn new(command: &str, trigger: &str, users: Vec<u64>) -> Self {
Self {
command: String::from(command),
trigger: String::from(trigger),
users,
}
}
}