-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctximpl.go
165 lines (144 loc) · 3.85 KB
/
ctximpl.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
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
package sevcord
import (
"strconv"
"github.com/bwmarrin/discordgo"
)
// MessageCtx represents a message context
type MessageCtx struct {
m *discordgo.Message
d *discordgo.Session
acknowledged bool
}
func (m *MessageCtx) Dg() *discordgo.Session {
return m.d
}
func (m *MessageCtx) Acknowledge() error {
if m.acknowledged {
return nil
}
m.acknowledged = true
return m.d.ChannelTyping(m.m.ChannelID)
}
func (m *MessageCtx) Respond(msg MessageSend) error {
v := msg.Dg()
v.Reference = &discordgo.MessageReference{
MessageID: m.m.ID,
ChannelID: m.m.ChannelID,
GuildID: m.m.GuildID,
}
_, err := m.d.ChannelMessageSendComplex(m.m.ChannelID, v)
return err
}
func (m *MessageCtx) Author() *discordgo.Member {
v := m.m.Member
v.User = m.m.Author
return v
}
func (m *MessageCtx) Channel() string {
return m.m.ChannelID
}
func (m *MessageCtx) Guild() string {
return m.m.GuildID
}
func (m *MessageCtx) Message() *discordgo.Message {
return m.m
}
// InteractionCtx represents context for an interaction
type InteractionCtx struct {
dg *discordgo.Session
i *discordgo.Interaction
s *Sevcord
acknowledged bool
component bool // If component, then update
modal bool
}
func (i *InteractionCtx) Dg() *discordgo.Session {
return i.dg
}
func (i *InteractionCtx) Acknowledge() error {
if i.acknowledged {
return nil
}
i.acknowledged = true
if i.component { // if component, then make it so that response will be ephemeral instead of update
return nil
}
return i.dg.InteractionRespond(i.i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
}
func (i *InteractionCtx) Respond(msg MessageSend) error {
b := msg.Dg()
if i.acknowledged && !i.component {
_, err := i.dg.FollowupMessageCreate(i.i, true, &discordgo.WebhookParams{
Content: b.Content,
Files: b.Files,
Embeds: b.Embeds,
Components: b.Components,
})
return err
}
if i.component && !i.acknowledged { // if not acknowledged, then update instead of ephemeral (no non-ephemeral response allowed on components since no one needs that)
typ := discordgo.InteractionResponseUpdateMessage
if i.modal {
typ = discordgo.InteractionResponseChannelMessageWithSource
}
return i.dg.InteractionRespond(i.i, &discordgo.InteractionResponse{
Type: typ,
Data: &discordgo.InteractionResponseData{
Content: b.Content,
Files: b.Files,
Embeds: b.Embeds,
Components: b.Components,
},
})
}
return i.dg.InteractionRespond(i.i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: b.Content,
Files: b.Files,
Embeds: b.Embeds,
Components: b.Components,
Flags: 1 << 6, // Ephemeral
},
})
}
func (i *InteractionCtx) Author() *discordgo.Member {
return i.i.Member
}
func (i *InteractionCtx) Modal(m Modal) error {
comps := make([]discordgo.MessageComponent, len(m.Inputs))
for ind, inp := range m.Inputs {
comps[ind] = &discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: strconv.Itoa(ind),
Label: inp.Label,
Style: discordgo.TextInputStyle(inp.Style),
Placeholder: inp.Placeholder,
Required: inp.Required,
MinLength: inp.MinLength,
MaxLength: inp.MaxLength,
},
},
}
}
i.s.lock.Lock()
i.s.modalHandlers[i.i.ID] = m.Handler
i.s.lock.Unlock()
return i.dg.InteractionRespond(i.i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
Title: m.Title,
Components: comps,
CustomID: i.i.ID,
},
})
}
func (i *InteractionCtx) Channel() string {
return i.i.ChannelID
}
func (i *InteractionCtx) Guild() string {
return i.i.GuildID
}