-
Notifications
You must be signed in to change notification settings - Fork 1
/
ratelimit.go
45 lines (36 loc) · 920 Bytes
/
ratelimit.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
package rest
import (
"errors"
"regexp"
"strings"
)
type Ratelimiter interface {
Request(httpClient HTTPClient, req *request) (*DiscordResponse, error)
}
var userIdRe = regexp.MustCompile(`\d{16,19}`)
var majorParams = [...]string{"channels", "guilds", "webhooks"}
var ErrMaxRetriesExceeded = errors.New("max retries exceeded")
func isBucketedParam(comp string) bool {
for _, param := range majorParams {
if comp == param {
return true
}
}
return false
}
func getBucketID(urlStr string) string {
comps := strings.Split(urlStr, "/")
bucketComps := make([]string, 0)
for i, comp := range comps[5:] {
if comp == "reactions" {
bucketComps = append(bucketComps, comp)
break
}
if userIdRe.MatchString(comp) && !isBucketedParam(comps[5:][i]) {
bucketComps = append(bucketComps, "id")
} else {
bucketComps = append(bucketComps, comp)
}
}
return strings.Join(bucketComps, ":")
}