-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.go
More file actions
117 lines (100 loc) · 3.58 KB
/
Copy pathedit.go
File metadata and controls
117 lines (100 loc) · 3.58 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
package strut
import (
"time"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
)
// defaultEditTTL is how long an invocation stays editable by default.
const defaultEditTTL = 10 * time.Minute
// EditTrackerConfig tunes how message edits are followed.
type EditTrackerConfig struct {
// TTL is how long an invocation stays editable. Zero means ten minutes.
TTL time.Duration
// ExecuteUntrackedEdits runs a command when an edit turns a message into
// a valid invocation, even though the original was not one.
ExecuteUntrackedEdits bool
// IgnoreEditsIfNotResponded skips edits to invocations the bot never
// answered.
IgnoreEditsIfNotResponded bool
// Store keeps the invocation to reply mapping. Nil uses an in-process
// store, which is usually right: a guild always lands on one shard.
Store Store[snowflake.ID, TrackedReply]
}
// TrackedReply is the answer strut gave to one invocation.
type TrackedReply struct {
// Response is nil when the command produced no reply.
Response *discord.Message
// DeleteWithInvocation records Meta.TrackDeletion, so deleting the
// invocation removes the answer with it.
DeleteWithInvocation bool
}
// EditTracker remembers which bot message answered which invocation, so an
// edited command can update its original reply instead of sending a new one.
type EditTracker struct {
cfg EditTrackerConfig
store Store[snowflake.ID, TrackedReply]
}
// NewEditTracker returns a tracker with the given configuration.
func NewEditTracker(cfg EditTrackerConfig) *EditTracker {
if cfg.TTL <= 0 {
cfg.TTL = defaultEditTTL
}
store := cfg.Store
if store == nil {
store = NewMemoryStore[snowflake.ID, TrackedReply]()
}
return &EditTracker{cfg: cfg, store: store}
}
// track records how an invocation was answered.
func (t *EditTracker) track(invocation snowflake.ID, reply TrackedReply) {
t.store.Put(invocation, reply, t.cfg.TTL)
}
// lookup returns the answer to an invocation, and whether it was tracked at
// all. A tracked invocation with no reply returns a zero TrackedReply.
func (t *EditTracker) lookup(invocation snowflake.ID) (TrackedReply, bool) {
return t.store.Get(invocation)
}
// forget drops an invocation, used when its message is deleted.
func (t *EditTracker) forget(invocation snowflake.ID) (TrackedReply, bool) {
return t.store.Remove(invocation)
}
// onMessageUpdate re-runs a command whose invocation was edited.
func (f *Framework[D]) onMessageUpdate(ev *events.MessageUpdate) {
p := f.opts.Prefix
if p == nil || p.EditTracker == nil {
return
}
if ev.Message.Author.Bot && !p.AllowBots {
return
}
tracked, ok := p.EditTracker.lookup(ev.MessageID)
switch {
case ok && tracked.Response == nil && p.EditTracker.cfg.IgnoreEditsIfNotResponded:
return
case !ok && !p.EditTracker.cfg.ExecuteUntrackedEdits:
return
}
msg := &events.MessageCreate{GenericMessage: ev.GenericMessage}
node, lex, found := f.resolvePrefix(msg)
if !found {
return
}
// Only commands that opted in re-run; the rest keep their first answer.
if node.inv != nil && !node.inv.meta.InvokeOnEdit {
return
}
f.dispatchPrefix(node, msg, lex, tracked.Response)
}
// onMessageDelete removes the bot's answer when the invocation is deleted.
func (f *Framework[D]) onMessageDelete(ev *events.MessageDelete) {
p := f.opts.Prefix
if p == nil || p.EditTracker == nil {
return
}
tracked, ok := p.EditTracker.forget(ev.MessageID)
if !ok || tracked.Response == nil || !tracked.DeleteWithInvocation || f.client == nil {
return
}
_ = f.client.Rest.DeleteMessage(tracked.Response.ChannelID, tracked.Response.ID)
}