rceaadb/src/commands.rs

37 lines
1.1 KiB
Rust
Raw Normal View History

2020-01-02 23:10:28 +01:00
use super::config::CONFIG;
use cmd_lib::{CmdResult, Process};
2020-01-02 17:51:02 +01:00
use serde::Deserialize;
use serenity::model::channel::Message;
#[derive(Deserialize, Debug)]
2020-01-02 23:10:28 +01:00
pub struct Command {
trigger: String,
command: String,
2020-01-02 17:51:02 +01:00
users: Vec<u64>,
}
pub fn print_commands() {
2020-01-02 23:10:28 +01:00
CONFIG.commands.iter().for_each(|c| println!("{:?}", c));
2020-01-02 17:51:02 +01:00
}
pub fn find_matching(message: &str) -> Option<&Command> {
2020-01-02 23:10:28 +01:00
CONFIG.commands.iter().find(|&c| message[1..] == c.trigger)
2020-01-02 17:51:02 +01:00
}
2020-01-02 23:10:28 +01:00
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
);
2020-01-02 17:51:02 +01:00
if !self.users.contains(&msg.author.id.0) {
2020-01-02 23:10:28 +01:00
return Err("You don’t have the permissions to execute this command.".to_owned());
2020-01-02 17:51:02 +01:00
}
2020-01-02 23:10:28 +01:00
Process::new(self.command.clone())
.wait::<CmdResult>()
.map_err(|e| format!("{:?}", e))
2020-01-02 17:51:02 +01:00
}
}