-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.go
More file actions
87 lines (79 loc) · 2.63 KB
/
Copy pathcomponents.go
File metadata and controls
87 lines (79 loc) · 2.63 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
package dctl
import (
"context"
"net/http"
"github.com/Herrscherd/dctl/internal/transport"
)
// SelectOption is one entry in a select menu: Label is shown in the dropdown,
// Value is what the interaction submits when the entry is chosen.
type SelectOption struct {
Label string
Value string
Description string
}
// Components sends message components (select menus) and acks their interactions.
type Components struct {
rt transport.Doer
def *defaults
}
// choiceMenuComponents builds the Discord component tree for a single-select
// dropdown: one ACTION_ROW (type 1) wrapping one STRING_SELECT (type 3).
func choiceMenuComponents(customID string, options []SelectOption) []map[string]any {
opts := make([]map[string]any, 0, len(options))
for _, o := range options {
m := map[string]any{"label": clamp(o.Label, 100), "value": clamp(o.Value, 100)}
if o.Description != "" {
m["description"] = clamp(o.Description, 100)
}
opts = append(opts, m)
}
return []map[string]any{{
"type": 1,
"components": []map[string]any{{
"type": 3,
"custom_id": customID,
"options": opts,
}},
}}
}
// SendSelectMenu posts content with a single-select dropdown. When replyTo is
// set the message threads under it; customID routes the click back to the caller.
func (c *Components) SendSelectMenu(ctx context.Context, channelID, replyTo, content, customID string, options []SelectOption) (*Message, error) {
ch, err := c.def.resolveChannel(channelID)
if err != nil {
return nil, err
}
body := map[string]any{
"content": content,
"components": choiceMenuComponents(customID, options),
}
if replyTo != "" {
body["message_reference"] = map[string]any{"message_id": replyTo, "fail_if_not_exists": false}
}
var msg Message
if err := c.rt.Do(ctx, http.MethodPost, "/channels/"+seg(ch)+"/messages", body, &msg); err != nil {
return nil, err
}
return &msg, nil
}
// Ack acknowledges a component interaction with an UPDATE_MESSAGE (type 7):
// rewrites the message content and drops the menu so the click is confirmed
// and the dropdown can't be used twice. Must be sent within Discord's 3s deadline.
func (c *Components) Ack(ctx context.Context, id, token, content string) error {
body := map[string]any{
"type": 7,
"data": map[string]any{"content": content, "components": []any{}},
}
return c.rt.Do(ctx, http.MethodPost, "/interactions/"+seg(id)+"/"+seg(token)+"/callback", body, nil)
}
// clamp truncates s to at most limit runes without splitting a multibyte rune.
func clamp(s string, limit int) string {
if len(s) <= limit {
return s
}
r := []rune(s)
if len(r) <= limit {
return s
}
return string(r[:limit])
}