-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwebhook_client_impl.go
133 lines (105 loc) · 4.31 KB
/
webhook_client_impl.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package webhook
import (
"context"
"log/slog"
"net/url"
"strings"
"github.com/disgoorg/snowflake/v2"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest"
)
// NewWithURL creates a new Client by parsing the given webhookURL for the ID and token.
func NewWithURL(webhookURL string, opts ...ConfigOpt) (Client, error) {
u, err := url.Parse(webhookURL)
if err != nil {
return nil, err
}
parts := strings.FieldsFunc(u.Path, func(r rune) bool { return r == '/' })
if len(parts) != 4 {
return nil, ErrInvalidWebhookURL
}
token := parts[3]
id, err := snowflake.Parse(parts[2])
if err != nil {
return nil, err
}
return New(id, token, opts...), nil
}
// New creates a new Client with the given ID, token and ConfigOpt(s).
func New(id snowflake.ID, token string, opts ...ConfigOpt) Client {
config := DefaultConfig()
config.Apply(opts)
config.Logger = config.Logger.With(slog.String("name", "webhook"))
return &clientImpl{
id: id,
token: token,
config: *config,
}
}
type clientImpl struct {
id snowflake.ID
token string
config Config
}
func (c *clientImpl) ID() snowflake.ID {
return c.id
}
func (c *clientImpl) Token() string {
return c.token
}
func (c *clientImpl) URL() string {
return discord.WebhookURL(c.id, c.token)
}
func (c *clientImpl) Close(ctx context.Context) {
c.config.RestClient.Close(ctx)
}
func (c *clientImpl) Rest() rest.Webhooks {
return c.config.Webhooks
}
func (c *clientImpl) GetWebhook(opts ...rest.RequestOpt) (*discord.IncomingWebhook, error) {
webhook, err := c.Rest().GetWebhookWithToken(c.id, c.token, opts...)
if incomingWebhook, ok := webhook.(discord.IncomingWebhook); ok && err == nil {
return &incomingWebhook, nil
}
return nil, err
}
func (c *clientImpl) UpdateWebhook(webhookUpdate discord.WebhookUpdateWithToken, opts ...rest.RequestOpt) (*discord.IncomingWebhook, error) {
webhook, err := c.Rest().UpdateWebhookWithToken(c.id, c.token, webhookUpdate, opts...)
if incomingWebhook, ok := webhook.(discord.IncomingWebhook); ok && err == nil {
return &incomingWebhook, nil
}
return nil, err
}
func (c *clientImpl) DeleteWebhook(opts ...rest.RequestOpt) error {
return c.Rest().DeleteWebhookWithToken(c.id, c.token, opts...)
}
func (c *clientImpl) CreateMessageInThread(messageCreate discord.WebhookMessageCreate, threadID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.Rest().CreateWebhookMessage(c.id, c.token, messageCreate, true, threadID, opts...)
}
func (c *clientImpl) CreateMessage(messageCreate discord.WebhookMessageCreate, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.CreateMessageInThread(messageCreate, 0, opts...)
}
func (c *clientImpl) CreateContent(content string, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.CreateMessage(discord.WebhookMessageCreate{Content: content}, opts...)
}
func (c *clientImpl) CreateEmbeds(embeds []discord.Embed, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.CreateMessage(discord.WebhookMessageCreate{Embeds: embeds}, opts...)
}
func (c *clientImpl) UpdateMessage(messageID snowflake.ID, messageUpdate discord.WebhookMessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.UpdateMessageInThread(messageID, messageUpdate, 0, opts...)
}
func (c *clientImpl) UpdateMessageInThread(messageID snowflake.ID, messageUpdate discord.WebhookMessageUpdate, threadID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.Rest().UpdateWebhookMessage(c.id, c.token, messageID, messageUpdate, threadID, opts...)
}
func (c *clientImpl) UpdateContent(messageID snowflake.ID, content string, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.UpdateMessage(messageID, discord.WebhookMessageUpdate{Content: &content}, opts...)
}
func (c *clientImpl) UpdateEmbeds(messageID snowflake.ID, embeds []discord.Embed, opts ...rest.RequestOpt) (*discord.Message, error) {
return c.UpdateMessage(messageID, discord.WebhookMessageUpdate{Embeds: &embeds}, opts...)
}
func (c *clientImpl) DeleteMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error {
return c.DeleteMessageInThread(messageID, 0, opts...)
}
func (c *clientImpl) DeleteMessageInThread(messageID snowflake.ID, threadID snowflake.ID, opts ...rest.RequestOpt) error {
return c.Rest().DeleteWebhookMessage(c.id, c.token, messageID, threadID, opts...)
}