41 lines
583 B
Go
41 lines
583 B
Go
package discord
|
|
|
|
import (
|
|
r "math/rand"
|
|
"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
|
|
}
|
|
|
|
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]
|
|
}
|