37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
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<u64>,
|
|
}
|
|
|
|
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::<CmdResult>()
|
|
.map_err(|e| format!("{:?}", e))
|
|
}
|
|
}
|