-
Notifications
You must be signed in to change notification settings - Fork 35
/
event_manager_config.go
79 lines (65 loc) · 2.46 KB
/
event_manager_config.go
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
package bot
import (
"log/slog"
"github.com/disgoorg/disgo/gateway"
)
// DefaultEventManagerConfig returns a new EventManagerConfig with all default values.
func DefaultEventManagerConfig() *EventManagerConfig {
return &EventManagerConfig{
Logger: slog.Default(),
}
}
// EventManagerConfig can be used to configure the EventManager.
type EventManagerConfig struct {
Logger *slog.Logger
EventListeners []EventListener
AsyncEventsEnabled bool
GatewayHandlers map[gateway.EventType]GatewayEventHandler
HTTPServerHandler HTTPServerEventHandler
}
// EventManagerConfigOpt is a functional option for configuring an EventManager.
type EventManagerConfigOpt func(config *EventManagerConfig)
// Apply applies the given EventManagerConfigOpt(s) to the EventManagerConfig.
func (c *EventManagerConfig) Apply(opts []EventManagerConfigOpt) {
for _, opt := range opts {
opt(c)
}
}
// WithEventManagerLogger overrides the default logger in the EventManagerConfig.
func WithEventManagerLogger(logger *slog.Logger) EventManagerConfigOpt {
return func(config *EventManagerConfig) {
config.Logger = logger
}
}
// WithListeners adds the given EventListener(s) to the EventManagerConfig.
func WithListeners(listeners ...EventListener) EventManagerConfigOpt {
return func(config *EventManagerConfig) {
config.EventListeners = append(config.EventListeners, listeners...)
}
}
// WithListenerFunc adds the given func(e E) to the EventManagerConfig.
func WithListenerFunc[E Event](f func(e E)) EventManagerConfigOpt {
return WithListeners(NewListenerFunc(f))
}
// WithListenerChan adds the given chan<- E to the EventManagerConfig.
func WithListenerChan[E Event](c chan<- E) EventManagerConfigOpt {
return WithListeners(NewListenerChan(c))
}
// WithAsyncEventsEnabled enables/disables the async events.
func WithAsyncEventsEnabled() EventManagerConfigOpt {
return func(config *EventManagerConfig) {
config.AsyncEventsEnabled = true
}
}
// WithGatewayHandlers overrides the default GatewayEventHandler(s) in the EventManagerConfig.
func WithGatewayHandlers(handlers map[gateway.EventType]GatewayEventHandler) EventManagerConfigOpt {
return func(config *EventManagerConfig) {
config.GatewayHandlers = handlers
}
}
// WithHTTPServerHandler overrides the given HTTPServerEventHandler in the EventManagerConfig.
func WithHTTPServerHandler(handler HTTPServerEventHandler) EventManagerConfigOpt {
return func(config *EventManagerConfig) {
config.HTTPServerHandler = handler
}
}