summary refs log tree commit diff
path: root/src/system/plugin.rs
blob: 379606dc9db3407a1d827f5fcf8c94bc8fb212f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
mod autoproxy;
mod edit;
// mod ghost;
mod prefixes;
// mod reproxy;

use std::{collections::HashMap, sync::Arc};

use async_trait::async_trait;

use edit::Edit;
use twilight_model::{channel::{Channel, Message}, id::{marker::ChannelMarker, Id}};

use super::log::Logger;
use crate::system::types::{System, Response};

pub use prefixes::ProxyPrefixes;
pub use autoproxy::Autoproxy;

#[derive(Copy, Clone, Debug)]
pub enum PluginCommand {
    Long(&'static str),
    Short(&'static str),
}

#[async_trait]
pub trait SeancePlugin<'system> {
    fn get_commands(&self) -> Vec<PluginCommand>;

    async fn handle_command<'message>(&self, logger: &'system Logger, system: &'system System, message: &'message Message, command: PluginCommand, args: Vec<&'message str>);

    async fn handle_message<'message>(&self, logger: &'system Logger, system: &'system System, message: &'message Message, response: &'message mut Response);

    async fn post_response<'message>(&self, logger: &'system Logger, system: &'system System, message: &'message Message, channel: Id<ChannelMarker>, response: &'message Response);
}

pub fn get_plugins<'system>() -> (Vec<Arc<Box<dyn SeancePlugin<'system>>>>, HashMap<&'static str, (PluginCommand, Arc<Box<dyn SeancePlugin<'system>>>)>) {
    let all_plugins : Vec<Arc<Box<dyn SeancePlugin<'system>>>> = vec![
        Arc::new(Box::new(ProxyPrefixes::new())),
        Arc::new(Box::new(Autoproxy::new())),
        Arc::new(Box::new(Edit::new())),
    ];

    let by_commands = all_plugins.iter()
        .map(|plugin| {
            let commands = plugin.get_commands();
            commands.into_iter().map(|command| {
                match command {
                    PluginCommand::Long(command_word) => (command_word, (command, plugin.clone())),
                    PluginCommand::Short(command_char) => (command_char, (command, plugin.clone())),
                }
            })
        })
        .flatten()
        .collect();

    (all_plugins, by_commands)
}