-
Notifications
You must be signed in to change notification settings - Fork 915
/
response.go
178 lines (147 loc) · 4.51 KB
/
response.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
166
167
168
169
170
171
172
173
174
175
176
177
178
package dcmd
import (
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
)
type Response interface {
// Channel, session, command etc can all be found in this context
Send(data *Data) ([]*discordgo.Message, error)
}
func SendResponseInterface(data *Data, reply interface{}, escapeEveryoneMention bool) ([]*discordgo.Message, error) {
allowedMentions := discordgo.AllowedMentions{}
if !escapeEveryoneMention {
// Legacy behaviour
allowedMentions.Parse = []discordgo.AllowedMentionType{discordgo.AllowedMentionTypeRoles, discordgo.AllowedMentionTypeUsers, discordgo.AllowedMentionTypeUsers}
}
return data.SendFollowupMessage(reply, allowedMentions)
}
// Temporary response deletes the inner response after Duration
type TemporaryResponse struct {
Response interface{}
Duration time.Duration
EscapeEveryone bool
}
func NewTemporaryResponse(d time.Duration, inner interface{}, escapeEveryoneMention bool) *TemporaryResponse {
return &TemporaryResponse{
Duration: d, Response: inner,
EscapeEveryone: escapeEveryoneMention,
}
}
func (t *TemporaryResponse) Send(data *Data) ([]*discordgo.Message, error) {
msgs, err := SendResponseInterface(data, t.Response, t.EscapeEveryone)
if err != nil {
return nil, err
}
time.AfterFunc(t.Duration, func() {
// do a bulk if 2 or more
if len(msgs) > 1 {
ids := make([]int64, len(msgs))
for i, m := range msgs {
ids[i] = m.ID
}
data.Session.ChannelMessagesBulkDelete(data.ChannelID, ids)
} else {
data.Session.ChannelMessageDelete(data.ChannelID, msgs[0].ID)
}
})
return msgs, nil
}
// SplitSendMessage uses SplitString to make sure each message is within 2k characters and splits at last newline before that (if possible)
func SplitSendMessage(data *Data, contents string, allowedMentions discordgo.AllowedMentions) ([]*discordgo.Message, error) {
result := make([]*discordgo.Message, 0, 1)
split := SplitString(contents, 2000)
for _, v := range split {
var err error
var m *discordgo.Message
switch data.TriggerType {
case TriggerTypeSlashCommands:
m, err = data.Session.CreateFollowupMessage(data.SlashCommandTriggerData.Interaction.ApplicationID, data.SlashCommandTriggerData.Interaction.Token, &discordgo.WebhookParams{
Content: v,
AllowedMentions: &allowedMentions,
})
default:
m, err = data.Session.ChannelMessageSendComplex(data.ChannelID, &discordgo.MessageSend{
Content: v,
AllowedMentions: allowedMentions,
})
}
if err != nil {
return result, err
}
result = append(result, m)
}
return result, nil
}
// SplitString uses StrSplitNext to split a string at the last newline before maxLen, throwing away leading and ending whitespaces in the process
func SplitString(s string, maxLen int) []string {
result := make([]string, 0, 1)
rest := s
for {
if strings.TrimSpace(rest) == "" {
break
}
var split string
split, rest = StrSplitNext(rest, maxLen)
split = strings.TrimSpace(split)
if split == "" {
continue
}
result = append(result, split)
}
return result
}
// StrSplitNext Will split "s" before runecount at last possible newline, whitespace or just at "runecount" if there is no whitespace
// If the runecount in "s" is less than "runeCount" then "last" will be zero
func StrSplitNext(s string, runeCount int) (split, rest string) {
if utf8.RuneCountInString(s) <= runeCount {
return s, ""
}
_, beforeIndex := RuneByIndex(s, runeCount)
firstPart := s[:beforeIndex]
// Split at newline if possible
foundWhiteSpace := false
lastIndex := strings.LastIndex(firstPart, "\n")
if lastIndex == -1 {
// No newline, check for any possible whitespace then
lastIndex = strings.LastIndexFunc(firstPart, func(r rune) bool {
return unicode.In(r, unicode.White_Space)
})
if lastIndex == -1 {
lastIndex = beforeIndex
} else {
foundWhiteSpace = true
}
} else {
foundWhiteSpace = true
}
// Remove the whitespace we split at if any
if foundWhiteSpace {
_, rLen := utf8.DecodeRuneInString(s[lastIndex:])
rest = s[lastIndex+rLen:]
} else {
rest = s[lastIndex:]
}
split = s[:lastIndex]
return
}
// RuneByIndex Returns the string index from the rune position
// Panics if utf8.RuneCountInString(s) <= runeIndex or runePos < 0
func RuneByIndex(s string, runePos int) (rune, int) {
sLen := utf8.RuneCountInString(s)
if sLen <= runePos || runePos < 0 {
panic("runePos is out of bounds")
}
i := 0
last := rune(0)
for k, r := range s {
if i == runePos {
return r, k
}
i++
last = r
}
return last, i
}