48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
use serenity::model::channel::Message;
|
|
use serenity::prelude::*;
|
|
mod pacman;
|
|
|
|
pub struct Handler;
|
|
pub type CommandHandler = dyn Fn(Context, Message);
|
|
struct Command {
|
|
trigger: String,
|
|
handler: Box<CommandHandler>,
|
|
}
|
|
|
|
unsafe impl Sync for Command {}
|
|
|
|
const PREFIX: char = '!';
|
|
|
|
impl EventHandler for Handler {
|
|
fn message(&self, ctx: Context, msg: Message) {
|
|
if msg.content.starts_with(PREFIX) {
|
|
if let Some(command) = COMMANDS
|
|
.iter()
|
|
.filter(|&c| msg.content.starts_with(&c.trigger))
|
|
.next()
|
|
{
|
|
(*command.handler)(ctx, msg);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
lazy_static! {
|
|
static ref COMMANDS: Vec<Command> = {
|
|
let mut command_list = Vec::new();
|
|
command_list.push(Command::new("pacman", pacman::query_pacman));
|
|
command_list
|
|
};
|
|
}
|
|
|
|
impl Command {
|
|
pub fn new(trigger: &str, handler: fn(Context, Message)) -> Self {
|
|
let mut trigger_with_prefix = trigger.to_string();
|
|
trigger_with_prefix.insert(0, PREFIX);
|
|
Self {
|
|
trigger: trigger_with_prefix,
|
|
handler: Box::new(handler),
|
|
}
|
|
}
|
|
}
|