-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.go
More file actions
148 lines (123 loc) · 3.68 KB
/
Copy pathlayout.go
File metadata and controls
148 lines (123 loc) · 3.68 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
package strut
import (
"errors"
"github.com/disgoorg/disgo/discord"
)
// Discord's newer layout components. They need the IsComponentsV2 message
// flag, which strut sets whenever a reply uses one.
// Text starts a reply built from layout components.
func Text(content string) Reply { return Reply{}.Text(content) }
// Text adds a block of markdown as its own component.
func (r Reply) Text(content string) Reply {
r.v2 = true
r.Components = append(r.Components, discord.NewTextDisplay(content))
return r
}
// Textf adds formatted markdown.
func (r Reply) Textf(format string, a ...any) Reply {
r.v2 = true
r.Components = append(r.Components, discord.NewTextDisplayf(format, a...))
return r
}
// Separator adds a divider.
func (r Reply) Separator() Reply {
r.v2 = true
r.Components = append(r.Components, discord.NewSeparator(discord.SeparatorSpacingSizeSmall))
return r
}
// Section pairs text with one accessory, such as a button or a thumbnail.
type Section struct {
text []string
accessory discord.SectionAccessoryComponent
err error
}
// NewSection starts a section holding the given markdown blocks.
func NewSection(text ...string) Section { return Section{text: text} }
// WithButton puts a button beside the text.
func (s Section) WithButton(b Component) Section {
if b.err != nil {
s.err = errors.Join(s.err, b.err)
return s
}
if btn, ok := b.c.(discord.ButtonComponent); ok {
s.accessory = btn
}
return s
}
// WithThumbnail puts an image beside the text.
func (s Section) WithThumbnail(url string) Section {
s.accessory = discord.NewThumbnail(url)
return s
}
// Section adds a section to the reply.
func (r Reply) Section(s Section) Reply {
if s.err != nil {
r.err = errors.Join(r.err, s.err)
return r
}
subs := make([]discord.SectionSubComponent, 0, len(s.text))
for _, t := range s.text {
subs = append(subs, discord.NewTextDisplay(t))
}
section := discord.NewSection(subs...)
if s.accessory != nil {
section = section.WithAccessory(s.accessory)
}
r.v2 = true
r.Components = append(r.Components, section)
return r
}
// Container groups components behind an accent colour.
type Container struct {
components []discord.ContainerSubComponent
color int
err error
}
// NewContainer starts a container.
func NewContainer() Container { return Container{} }
// WithColor sets the accent colour.
func (c Container) WithColor(color int) Container { c.color = color; return c }
// Text adds markdown inside the container.
func (c Container) Text(content string) Container {
c.components = append(c.components, discord.NewTextDisplay(content))
return c
}
// Row adds a row of components inside the container.
func (c Container) Row(components ...Component) Container {
if len(components) == 0 {
return c
}
if len(components) > maxRowComponents {
c.err = errors.Join(c.err, errTooManyInRow(len(components)))
return c
}
out := make([]discord.InteractiveComponent, 0, len(components))
for _, comp := range components {
if comp.err != nil {
c.err = errors.Join(c.err, comp.err)
return c
}
out = append(out, comp.c)
}
c.components = append(c.components, discord.NewActionRow(out...))
return c
}
// Separator adds a divider inside the container.
func (c Container) Separator() Container {
c.components = append(c.components, discord.NewSeparator(discord.SeparatorSpacingSizeSmall))
return c
}
// Container adds a container to the reply.
func (r Reply) Container(c Container) Reply {
if c.err != nil {
r.err = errors.Join(r.err, c.err)
return r
}
container := discord.NewContainer(c.components...)
if c.color != 0 {
container = container.WithAccentColor(c.color)
}
r.v2 = true
r.Components = append(r.Components, container)
return r
}