File size: 1,276 Bytes
891b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// (c) Jisin0

package utils

import (
	"math/rand"
	"time"

	"github.com/Jisin0/Go-Filter-Bot/utils/config"
)

// Character set to generate a random string
const charset = "abcdefghijklmnopqrstuvwxyz" +
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func RandString(length int) string {
	var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))

	b := make([]byte, length)
	for i := range b {
		b[i] = charset[seededRand.Intn(len(charset))]
	}

	return string(b)
}

// IsAdmin returns wether the user is a global admin(from env or dynamic DB list).
func IsAdmin(u int64) bool {
	for _, n := range config.Admins {
		if u == n {
			return true
		}
	}
	// Check dynamic admins if getter is set
	if dynamicAdminGetter != nil {
		for _, n := range dynamicAdminGetter() {
			if u == n {
				return true
			}
		}
	}
	return false
}

// dynamicAdminGetter is set by the database layer to avoid import cycles.
var dynamicAdminGetter func() []int64

// SetDynamicAdminGetter sets the function used to fetch dynamic admins from DB.
func SetDynamicAdminGetter(fn func() []int64) {
	dynamicAdminGetter = fn
}

// Checks if a string slice contains an item.
func Contains(l []string, v string) bool {
	for _, i := range l {
		if i == v {
			return true
		}
	}

	return false
}