2020-07-02 20:00:22 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-07-02 21:52:20 +00:00
|
|
|
r "math/rand"
|
2020-07-02 20:00:22 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rateLimit struct {
|
|
|
|
rate time.Duration
|
|
|
|
next time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRateLimit(rate time.Duration) *rateLimit {
|
|
|
|
return &rateLimit{
|
|
|
|
rate: rate,
|
|
|
|
next: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rateLimit) Try() bool {
|
|
|
|
now := time.Now()
|
|
|
|
if now.After(r.next) {
|
|
|
|
r.next = now.Add(r.rate)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-07-02 21:52:20 +00:00
|
|
|
|
|
|
|
var rand = r.New(r.NewSource(time.Now().Unix()))
|
|
|
|
|
|
|
|
func random(list []string) string {
|
|
|
|
size := len(list)
|
|
|
|
if size == 0 {
|
|
|
|
return ""
|
|
|
|
} else if size == 1 {
|
|
|
|
return list[0]
|
|
|
|
}
|
|
|
|
idx := rand.Intn(size)
|
|
|
|
return list[idx]
|
|
|
|
}
|