tulpje_framework/module/
builder.rs

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::collections::{HashMap, HashSet};

use async_cron_scheduler::cron::Schedule;
use twilight_gateway::EventType;
use twilight_model::application::command::Command;

use super::Module;
use crate::handler::{
    command_handler::{CommandFunc, CommandHandler},
    component_interaction_handler::{ComponentInteractionFunc, ComponentInteractionHandler},
    event_handler::{EventFunc, EventHandler},
    task_handler::{TaskFunc, TaskHandler},
};

pub struct ModuleBuilder<T: Clone + Send + Sync> {
    name: String,
    guild_scoped: bool,

    commands: HashMap<String, CommandHandler<T>>,
    components: HashMap<String, ComponentInteractionHandler<T>>,
    events: HashMap<EventType, HashSet<EventHandler<T>>>,
    tasks: HashMap<String, TaskHandler<T>>,
}

impl<T: Clone + Send + Sync> ModuleBuilder<T> {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.into(),
            guild_scoped: false,

            commands: HashMap::new(),
            components: HashMap::new(),
            events: HashMap::new(),
            tasks: HashMap::new(),
        }
    }

    #[must_use]
    pub fn build(self) -> Module<T> {
        Module {
            name: self.name,
            guild_scoped: self.guild_scoped,

            commands: self.commands,
            components: self.components,
            events: self.events,
            tasks: self.tasks,
        }
    }

    #[must_use]
    pub fn guild(mut self) -> Self {
        self.guild_scoped = true;
        self
    }

    #[must_use]
    pub fn command(mut self, definition: Command, func: CommandFunc<T>) -> Self {
        self.commands.insert(
            definition.name.clone(),
            CommandHandler {
                module: self.name.clone(),
                definition,
                func,
            },
        );
        self
    }

    #[must_use]
    pub fn component(mut self, custom_id: &str, func: ComponentInteractionFunc<T>) -> Self {
        self.components.insert(
            custom_id.to_string(),
            ComponentInteractionHandler {
                module: self.name.clone(),
                custom_id: custom_id.to_string(),
                func,
            },
        );
        self
    }

    #[must_use]
    pub fn event(mut self, event: EventType, func: EventFunc<T>) -> Self {
        self.events.entry(event).or_default().insert(EventHandler {
            module: self.name.clone(),
            uuid: uuid::Uuid::now_v7().to_string(),
            event,
            func,
        });
        self
    }

    #[must_use]
    pub fn task(mut self, name: &str, schedule: &str, func: TaskFunc<T>) -> Self {
        self.tasks.insert(
            name.to_string(),
            TaskHandler {
                module: self.name.clone(),
                name: name.to_string(),
                cron: Schedule::try_from(schedule).expect("failed to parse cron expression"),
                func,
            },
        );
        self
    }
}