-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwebhook_config.go
76 lines (65 loc) · 2.09 KB
/
webhook_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
package webhook
import (
"log/slog"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest"
)
// DefaultConfig is the default configuration for the webhook client
func DefaultConfig() *Config {
return &Config{
Logger: slog.Default(),
DefaultAllowedMentions: &discord.DefaultAllowedMentions,
}
}
// Config is the configuration for the webhook client
type Config struct {
Logger *slog.Logger
RestClient rest.Client
RestClientConfigOpts []rest.ConfigOpt
Webhooks rest.Webhooks
DefaultAllowedMentions *discord.AllowedMentions
}
// ConfigOpt is used to provide optional parameters to the webhook client
type ConfigOpt func(config *Config)
// Apply applies all options to the config
func (c *Config) Apply(opts []ConfigOpt) {
for _, opt := range opts {
opt(c)
}
if c.RestClient == nil {
c.RestClient = rest.NewClient("", append([]rest.ConfigOpt{rest.WithLogger(c.Logger)}, c.RestClientConfigOpts...)...)
}
if c.Webhooks == nil {
c.Webhooks = rest.NewWebhooks(c.RestClient)
}
}
// WithLogger sets the logger for the webhook client
func WithLogger(logger *slog.Logger) ConfigOpt {
return func(config *Config) {
config.Logger = logger
}
}
// WithRestClient sets the rest client for the webhook client
func WithRestClient(restClient rest.Client) ConfigOpt {
return func(config *Config) {
config.RestClient = restClient
}
}
// WithRestClientConfigOpts sets the rest client configuration for the webhook client
func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt {
return func(config *Config) {
config.RestClientConfigOpts = append(config.RestClientConfigOpts, opts...)
}
}
// WithWebhooks sets the webhook service for the webhook client
func WithWebhooks(webhooks rest.Webhooks) ConfigOpt {
return func(config *Config) {
config.Webhooks = webhooks
}
}
// WithDefaultAllowedMentions sets the default allowed mentions for the webhook client
func WithDefaultAllowedMentions(allowedMentions discord.AllowedMentions) ConfigOpt {
return func(config *Config) {
config.DefaultAllowedMentions = &allowedMentions
}
}