forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
229 lines (185 loc) · 5.92 KB
/
util.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package commands
import (
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/common"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
)
type DurationArg struct {
Min, Max time.Duration
}
func (d *DurationArg) Matches(def *dcmd.ArgDef, part string) bool {
if len(part) < 1 {
return false
}
// We "need" the first character to be a number
r, _ := utf8.DecodeRuneInString(part)
if !unicode.IsNumber(r) {
return false
}
_, err := ParseDuration(part)
return err == nil
}
func (d *DurationArg) Parse(def *dcmd.ArgDef, part string, data *dcmd.Data) (interface{}, error) {
dur, err := ParseDuration(part)
if err != nil {
return nil, err
}
if d.Min != 0 && d.Min > dur {
return nil, &DurationOutOfRangeError{ArgName: def.Name, Got: dur, Max: d.Max, Min: d.Min}
}
if d.Max != 0 && d.Max < dur {
return nil, &DurationOutOfRangeError{ArgName: def.Name, Got: dur, Max: d.Max, Min: d.Min}
}
return dur, nil
}
func (d *DurationArg) HelpName() string {
return "Duration"
}
// Parses a time string like 1day3h
func ParseDuration(str string) (time.Duration, error) {
var dur time.Duration
currentNumBuf := ""
currentModifierBuf := ""
// Parse the time
for _, v := range str {
// Ignore whitespace
if unicode.Is(unicode.White_Space, v) {
continue
}
if unicode.IsNumber(v) {
// If we reached a number and the modifier was also set, parse the last duration component before starting a new one
if currentModifierBuf != "" {
if currentNumBuf == "" {
currentNumBuf = "1"
}
d, err := parseDurationComponent(currentNumBuf, currentModifierBuf)
if err != nil {
return d, err
}
dur += d
currentNumBuf = ""
currentModifierBuf = ""
}
currentNumBuf += string(v)
} else {
currentModifierBuf += string(v)
}
}
if currentNumBuf != "" {
d, err := parseDurationComponent(currentNumBuf, currentModifierBuf)
if err != nil {
return dur, errors.Wrap(err, "not a duration")
}
dur += d
}
return dur, nil
}
func parseDurationComponent(numStr, modifierStr string) (time.Duration, error) {
parsedNum, err := strconv.ParseInt(numStr, 10, 64)
if err != nil {
return 0, err
}
parsedDur := time.Duration(parsedNum)
if strings.HasPrefix(modifierStr, "s") {
parsedDur = parsedDur * time.Second
} else if modifierStr == "" || (strings.HasPrefix(modifierStr, "m") && (len(modifierStr) < 2 || modifierStr[1] != 'o')) {
parsedDur = parsedDur * time.Minute
} else if strings.HasPrefix(modifierStr, "h") {
parsedDur = parsedDur * time.Hour
} else if strings.HasPrefix(modifierStr, "d") {
parsedDur = parsedDur * time.Hour * 24
} else if strings.HasPrefix(modifierStr, "w") {
parsedDur = parsedDur * time.Hour * 24 * 7
} else if strings.HasPrefix(modifierStr, "mo") {
parsedDur = parsedDur * time.Hour * 24 * 30
} else if strings.HasPrefix(modifierStr, "y") {
parsedDur = parsedDur * time.Hour * 24 * 365
} else {
return parsedDur, errors.New("couldn't figure out what '" + numStr + modifierStr + "` was")
}
return parsedDur, nil
}
type DurationOutOfRangeError struct {
Min, Max time.Duration
Got time.Duration
ArgName string
}
func (o *DurationOutOfRangeError) Error() string {
preStr := "too big"
if o.Got < o.Min {
preStr = "too small"
}
if o.Min == 0 {
return fmt.Sprintf("%s is %s, has to be smaller than %s", o.ArgName, preStr, common.HumanizeDuration(common.DurationPrecisionMinutes, o.Max))
} else if o.Max == 0 {
return fmt.Sprintf("%s is %s, has to be bigger than %s", o.ArgName, preStr, common.HumanizeDuration(common.DurationPrecisionMinutes, o.Min))
} else {
format := "%s is %s (has to be within `%d` and `%d`)"
return fmt.Sprintf(format, o.ArgName, preStr, common.HumanizeDuration(common.DurationPrecisionMinutes, o.Min), common.HumanizeDuration(common.DurationPrecisionMinutes, o.Max))
}
}
type PublicError string
func (p PublicError) Error() string {
return string(p)
}
func NewPublicError(a ...interface{}) PublicError {
return PublicError(fmt.Sprint(a...))
}
func NewPublicErrorF(f string, a ...interface{}) PublicError {
return PublicError(fmt.Sprintf(f, a...))
}
func FilterBadInvites(msg string, guildID int64, replacement string) string {
return common.ReplaceServerInvites(msg, guildID, replacement)
}
// CommonContainerNotFoundHandler is a common "NotFound" handler that should be used with dcmd containers
// it ensures that no messages is sent if none of the commands in te container is enabeld
// if "fixedMessage" is empty, then it shows default generated container help
func CommonContainerNotFoundHandler(container *dcmd.Container, fixedMessage string) func(data *dcmd.Data) (interface{}, error) {
return func(data *dcmd.Data) (interface{}, error) {
// Only show stuff if atleast 1 of the commands in the container is enabled
if data.GS != nil {
data.GS.RLock()
cParentID := data.CS.ParentID
data.GS.RUnlock()
channelOverrides, err := GetOverridesForChannel(data.CS.ID, cParentID, data.GS.ID)
if err != nil {
logrus.WithError(err).WithField("guild", data.Msg.GuildID).Error("failed retrieving command overrides")
return nil, nil
}
chain := []*dcmd.Container{CommandSystem.Root, container}
enabled := false
// make sure that atleast 1 command in the container is enabled
for _, v := range container.Commands {
cast := v.Command.(*YAGCommand)
settings, err := cast.GetSettingsWithLoadedOverrides(chain, data.GS.ID, channelOverrides)
if err != nil {
logrus.WithError(err).WithField("guild", data.Msg.GuildID).Error("failed checking if command was enabled")
continue
}
if settings.Enabled {
enabled = true
break
}
}
// no commands enabled, do nothing
if !enabled {
return nil, nil
}
}
if fixedMessage != "" {
return fixedMessage, nil
}
resp := dcmd.GenerateHelp(data, container, &dcmd.StdHelpFormatter{})
if len(resp) > 0 {
return resp[0], nil
}
return nil, nil
}
}