-
Notifications
You must be signed in to change notification settings - Fork 7
/
followupmessage.go
139 lines (116 loc) · 3.29 KB
/
followupmessage.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
134
135
136
137
138
139
package ken
import (
"time"
"github.com/bwmarrin/discordgo"
)
// FollowUpMessageBuilder builds a followup
// message interaction response.
type FollowUpMessageBuilder struct {
ken *Ken
i *discordgo.Interaction
data *discordgo.WebhookParams
wait bool
componentBuilder *ComponentBuilder
}
// Send builds the followup message and sends
// it as response to the interaction.
func (b *FollowUpMessageBuilder) Send() *FollowUpMessage {
if b.componentBuilder != nil {
b.data.Components = append(b.data.Components, b.componentBuilder.components...)
}
fum := &FollowUpMessage{
ken: b.ken,
i: b.i,
}
fum.Message, fum.Error = b.ken.s.FollowupMessageCreate(b.i, b.wait, b.data)
if fum.HasError() {
return fum
}
if b.componentBuilder != nil {
b.componentBuilder.chanId = fum.ChannelID
b.componentBuilder.msgId = fum.ID
fum.unregisterComponentHandlers, fum.Error = b.componentBuilder.build()
}
return fum
}
// AddComponents is getting passed a builder function
// where you can attach message components and handlers
// which will be applied to the followup message when
// sent.
func (b *FollowUpMessageBuilder) AddComponents(cb func(*ComponentBuilder)) *FollowUpMessageBuilder {
if b.componentBuilder == nil {
b.componentBuilder = newBuilder(b.ken.componentHandler)
}
cb(b.componentBuilder)
return b
}
// FollowUpMessage wraps an interaction follow
// up message and collected errors.
type FollowUpMessage struct {
*discordgo.Message
// Error contains the error instance of
// error occurrences during method execution.
Error error
ken *Ken
i *discordgo.Interaction
unregisterComponentHandlers func() error
}
// Edit overwrites the given follow up message with the
// data specified.
func (m *FollowUpMessage) Edit(data *discordgo.WebhookEdit) (err error) {
if m.Error != nil {
err = m.Error
return
}
inter, err := m.ken.s.FollowupMessageEdit(m.i, m.ID, data)
if err != nil {
return
}
// This is done to avoid setting m.Message to nil when
// an error is returned above.
m.Message = inter
return
}
// EditEmbed is shorthand for edit with the passed embed as
// WebhookEdit data.
func (m *FollowUpMessage) EditEmbed(emb *discordgo.MessageEmbed) (err error) {
return m.Edit(&discordgo.WebhookEdit{
Embeds: &[]*discordgo.MessageEmbed{emb},
})
}
// Delete removes the follow up message.
func (m *FollowUpMessage) Delete() (err error) {
if m.Error != nil {
err = m.Error
return
}
err = m.ken.s.FollowupMessageDelete(m.i, m.ID)
return
}
// DeleteAfter queues a deletion of the follow up
// message after the specified duration.
func (m *FollowUpMessage) DeleteAfter(d time.Duration) *FollowUpMessage {
go func() {
time.Sleep(d)
m.Delete()
}()
return m
}
// HasError returns true if the value of Error
// is not nil.
func (m *FollowUpMessage) HasError() bool {
return m.Error != nil
}
// AddComponents returns a new component builder to add
// message components with handlers to the FollowUpMessage.
func (m *FollowUpMessage) AddComponents() *ComponentBuilder {
return m.ken.Components().Add(m.ID, m.ChannelID)
}
// UnregisterComponentHandlers removes all handlers of
// attached componets from the register.
func (m *FollowUpMessage) UnregisterComponentHandlers() error {
if m.unregisterComponentHandlers != nil {
return m.unregisterComponentHandlers()
}
return nil
}