-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.go
More file actions
243 lines (195 loc) · 7.61 KB
/
Copy pathsource.go
File metadata and controls
243 lines (195 loc) · 7.61 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package strut
import (
"errors"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/snowflake/v2"
)
// An invocation reaches strut through one of several surfaces, each with its
// own way of reporting who ran it and its own way of answering. Those two
// concerns are the seams below, so Event does not branch on the surface and
// the test harness can substitute its own.
// source supplies invocation identity.
type Source interface {
Author() discord.User
Member() *discord.ResolvedMember
GuildID() *snowflake.ID
ChannelID() snowflake.ID
Locale() discord.Locale
NSFW() bool
AppPerms() *discord.Permissions
}
// responder performs the raw output operations.
type Responder interface {
Create(Reply) error
DeferResponse(ephemeral bool) error
Update(Reply) error
Followup(Reply) (*discord.Message, error)
Typing() error
}
// modalOpener shows a modal. Commands and components can open one; a prefix
// invocation has no interaction, and Discord does not allow a modal to be
// opened from another modal's submission.
type ModalOpener interface {
OpenModal(discord.ModalCreate) error
}
// interactionSource reads identity from any interaction. Commands,
// components, modals and autocomplete all report it the same way, so one
// implementation serves them all.
type interactionSource[I discord.Interaction] struct{ i I }
func (s interactionSource[I]) Author() discord.User { return s.i.User() }
func (s interactionSource[I]) Member() *discord.ResolvedMember { return s.i.Member() }
func (s interactionSource[I]) GuildID() *snowflake.ID { return s.i.GuildID() }
func (s interactionSource[I]) ChannelID() snowflake.ID { return s.i.Channel().ID() }
func (s interactionSource[I]) Locale() discord.Locale { return s.i.Locale() }
func (s interactionSource[I]) AppPerms() *discord.Permissions { return s.i.AppPermissions() }
// nsfw reports the channel flag, which only guild message channels carry.
func (s interactionSource[I]) NSFW() bool {
ch, ok := s.i.Channel().MessageChannel.(discord.GuildMessageChannel)
return ok && ch.NSFW()
}
// commandResponder answers a command interaction.
type commandResponder struct{ ce *handler.CommandEvent }
func (s commandResponder) Create(r Reply) error {
return s.ce.CreateMessage(r.toCreate())
}
func (s commandResponder) DeferResponse(ephemeral bool) error {
return s.ce.DeferCreateMessage(ephemeral)
}
func (s commandResponder) Update(r Reply) error {
_, err := s.ce.UpdateInteractionResponse(r.toUpdate())
return err
}
func (s commandResponder) Followup(r Reply) (*discord.Message, error) {
return s.ce.CreateFollowupMessage(r.toCreate())
}
func (s commandResponder) OpenModal(m discord.ModalCreate) error {
return s.ce.Modal(m)
}
func (s commandResponder) Typing() error {
return s.ce.Client().Rest.SendTyping(s.ce.Channel().ID())
}
// componentResponder answers a component interaction. Update edits the
// message the component is attached to, which is what Edit means there.
type componentResponder struct{ ce *handler.ComponentEvent }
func (s componentResponder) Create(r Reply) error {
return s.ce.CreateMessage(r.toCreate())
}
func (s componentResponder) DeferResponse(ephemeral bool) error {
return s.ce.DeferCreateMessage(ephemeral)
}
func (s componentResponder) Update(r Reply) error {
return s.ce.UpdateMessage(r.toUpdate())
}
func (s componentResponder) Followup(r Reply) (*discord.Message, error) {
return s.ce.CreateFollowupMessage(r.toCreate())
}
func (s componentResponder) OpenModal(m discord.ModalCreate) error {
return s.ce.Modal(m)
}
func (s componentResponder) Typing() error {
return s.ce.Client().Rest.SendTyping(s.ce.Channel().ID())
}
// modalResponder answers a modal submission.
type modalResponder struct{ me *handler.ModalEvent }
func (s modalResponder) Create(r Reply) error {
return s.me.CreateMessage(r.toCreate())
}
func (s modalResponder) DeferResponse(ephemeral bool) error {
return s.me.DeferCreateMessage(ephemeral)
}
func (s modalResponder) Update(r Reply) error {
_, err := s.me.UpdateInteractionResponse(r.toUpdate())
return err
}
func (s modalResponder) Followup(r Reply) (*discord.Message, error) {
return s.me.CreateFollowupMessage(r.toCreate())
}
func (s modalResponder) Typing() error {
return s.me.Client().Rest.SendTyping(s.me.Channel().ID())
}
// noResponder rejects output from a context that cannot send messages.
type noResponder struct{}
var errNoResponse = errors.New("strut: an autocomplete handler cannot send messages")
func (noResponder) Create(Reply) error { return errNoResponse }
func (noResponder) DeferResponse(bool) error { return errNoResponse }
func (noResponder) Update(Reply) error { return errNoResponse }
func (noResponder) Followup(Reply) (*discord.Message, error) { return nil, errNoResponse }
func (noResponder) Typing() error { return errNoResponse }
// messageSource reads identity from a prefix invocation, which is a plain
// message rather than an interaction.
type messageSource[D any] struct {
msg *events.MessageCreate
fw *Framework[D]
}
func (s messageSource[D]) Author() discord.User { return s.msg.Message.Author }
func (s messageSource[D]) Member() *discord.ResolvedMember {
if s.msg.Message.Member == nil {
return nil
}
return &discord.ResolvedMember{Member: *s.msg.Message.Member}
}
func (s messageSource[D]) GuildID() *snowflake.ID { return s.msg.GuildID }
func (s messageSource[D]) ChannelID() snowflake.ID { return s.msg.ChannelID }
// locale is empty: a message carries no locale the way an interaction does.
func (s messageSource[D]) Locale() discord.Locale { return "" }
// nsfw and appPerms come from the cache, since a message does not carry them
// inline the way an interaction does.
func (s messageSource[D]) NSFW() bool {
if s.fw.client == nil || s.msg.GuildID == nil {
return false
}
ch, ok := s.fw.client.Caches.GuildMessageChannel(s.msg.ChannelID)
return ok && ch.NSFW()
}
func (s messageSource[D]) AppPerms() *discord.Permissions {
if s.fw.client == nil || s.msg.GuildID == nil {
return nil
}
self, ok := s.fw.client.Caches.SelfMember(*s.msg.GuildID)
if !ok {
return nil
}
p := s.fw.client.Caches.MemberPermissions(self)
return &p
}
// messageResponder answers with ordinary messages.
type messageResponder[D any] struct {
msg *events.MessageCreate
fw *Framework[D]
// sent is the reply already produced, so re-running an edited invocation
// updates it rather than posting again.
sent *discord.Message
}
func (s *messageResponder[D]) Create(r Reply) error {
if s.sent != nil {
return s.Update(r)
}
create := r.toCreate()
// Ephemeral has no meaning outside an interaction.
create.Flags = create.Flags.Remove(discord.MessageFlagEphemeral)
if r.Reference {
create.MessageReference = &discord.MessageReference{MessageID: &s.msg.Message.ID}
}
sent, err := s.fw.client.Rest.CreateMessage(s.msg.ChannelID, create)
s.sent = sent
return err
}
// deferResponse has no equivalent for a message, so it shows typing.
func (s *messageResponder[D]) DeferResponse(bool) error { return s.Typing() }
func (s *messageResponder[D]) Update(r Reply) error {
if s.sent == nil {
return s.Create(r)
}
_, err := s.fw.client.Rest.UpdateMessage(s.msg.ChannelID, s.sent.ID, r.toUpdate())
return err
}
func (s *messageResponder[D]) Followup(r Reply) (*discord.Message, error) {
create := r.toCreate()
create.Flags = create.Flags.Remove(discord.MessageFlagEphemeral)
return s.fw.client.Rest.CreateMessage(s.msg.ChannelID, create)
}
func (s *messageResponder[D]) Typing() error {
return s.fw.client.Rest.SendTyping(s.msg.ChannelID)
}