-
Notifications
You must be signed in to change notification settings - Fork 926
/
admin_updater.go
107 lines (87 loc) · 3.04 KB
/
admin_updater.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
package bot
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix"
"github.com/sirupsen/logrus"
"os"
"strconv"
"time"
)
var (
mainServer int64
adminRole int64
readOnlyAccessRole int64
// Set of redis admins
RedisKeyAdmins = "yagpdb_admins"
tmpRedisKeyAdmins = "yagpdb_admins_tmp"
// Set of users with read only access
RedisKeyReadOnlyAccess = "yagpdb_ro_access"
tmpRedisKeyReadOnlyAccess = "yagpdb_ro_access_tmp"
)
func IsBotAdmin(userID int64) (isAdmin bool, err error) {
if userID == common.Conf.Owner {
return true, nil
}
err = common.RedisPool.Do(radix.FlatCmd(&isAdmin, "SISMEMBER", RedisKeyAdmins, userID))
return
}
func HasReadOnlyAccess(userID int64) (hasAccess bool, err error) {
err = common.RedisPool.Do(radix.FlatCmd(&hasAccess, "SISMEMBER", RedisKeyReadOnlyAccess, userID))
return
}
var stopRunCheckAdmins = make(chan bool)
func loopCheckAdmins() {
mainServerStr := os.Getenv("YAGPDB_MAIN_SERVER")
adminRoleStr := os.Getenv("YAGPDB_ADMIN_ROLE")
readOnlyAccessRoleStr := os.Getenv("YAGPDB_READONLY_ACCESS_ROLE")
mainServer, _ = strconv.ParseInt(mainServerStr, 10, 64)
adminRole, _ = strconv.ParseInt(adminRoleStr, 10, 64)
readOnlyAccessRole, _ = strconv.ParseInt(readOnlyAccessRoleStr, 10, 64)
if mainServer == 0 || (adminRole == 0 && readOnlyAccessRole == 0) {
return
}
ticker := time.NewTicker(time.Second * 60)
for {
select {
case <-ticker.C:
if IsGuildOnCurrentProcess(mainServer) {
requestCheckBotAdmins(mainServer, adminRole, readOnlyAccessRole)
}
case <-stopRunCheckAdmins:
return
}
}
}
func requestCheckBotAdmins(mainServer, adminRole, readOnlyRole int64) {
relevantSession := ShardManager.SessionForGuild(mainServer)
if relevantSession == nil || relevantSession.GatewayManager.Status() != discordgo.GatewayStatusReady {
logrus.WithField("shard", relevantSession.ShardID).Error("shard not ready, not updating bot admins")
return
}
// Swap the keys updated last round, assuming thats done
common.RedisPool.Do(radix.Cmd(nil, "RENAME", tmpRedisKeyAdmins, RedisKeyAdmins))
common.RedisPool.Do(radix.Cmd(nil, "RENAME", tmpRedisKeyReadOnlyAccess, RedisKeyReadOnlyAccess))
relevantSession.GatewayManager.RequestGuildMembers(mainServer, "", 0)
}
func HandleGuildMembersChunk(data *eventsystem.EventData) {
evt := data.GuildMembersChunk()
if evt.GuildID != mainServer {
return
}
for _, member := range evt.Members {
if adminRole != 0 && common.ContainsInt64Slice(member.Roles, adminRole) {
err := common.RedisPool.Do(radix.FlatCmd(nil, "SADD", tmpRedisKeyAdmins, member.User.ID))
if err != nil {
logrus.WithError(err).Error("failed adding user to admins")
}
}
if readOnlyAccessRole != 0 && common.ContainsInt64Slice(member.Roles, readOnlyAccessRole) {
err := common.RedisPool.Do(radix.FlatCmd(nil, "SADD", tmpRedisKeyReadOnlyAccess, member.User.ID))
if err != nil {
logrus.WithError(err).Error("failed adding user to read only access users")
}
}
}
}