forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
295 lines (234 loc) · 7.34 KB
/
util.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package bot
import (
"context"
"errors"
"github.com/bwmarrin/snowflake"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix.v3"
"github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
Cache = cache.New(time.Minute, time.Minute)
currentStatus = "v" + common.VERSION + " :)"
currentStreamingURL = ""
currentPresenceLock sync.Mutex
)
func init() {
// Discord epoch
snowflake.Epoch = 1420070400000
}
func ContextSession(ctx context.Context) *discordgo.Session {
return ctx.Value(common.ContextKeyDiscordSession).(*discordgo.Session)
}
func SendDM(user int64, msg string) error {
if strings.TrimSpace(msg) == "" {
return nil
}
channel, err := common.BotSession.UserChannelCreate(user)
if err != nil {
return err
}
_, err = common.BotSession.ChannelMessageSend(channel.ID, msg)
return err
}
func SendDMEmbed(user int64, embed *discordgo.MessageEmbed) error {
channel, err := common.BotSession.UserChannelCreate(user)
if err != nil {
return err
}
_, err = common.BotSession.ChannelMessageSendEmbed(channel.ID, embed)
return err
}
var (
ErrStartingUp = errors.New("Starting up, caches are being filled...")
ErrGuildNotFound = errors.New("Guild not found")
)
func AdminOrPerm(needed int, userID, channelID int64) (bool, error) {
channel := State.Channel(true, channelID)
if channel == nil {
return false, errors.New("Channel not found")
}
// Ensure the member is in state
GetMember(channel.Guild.ID, userID)
perms, err := channel.Guild.MemberPermissions(true, channelID, userID)
if err != nil {
return false, err
}
if perms&needed != 0 {
return true, nil
}
if perms&discordgo.PermissionManageServer != 0 || perms&discordgo.PermissionAdministrator != 0 {
return true, nil
}
return false, nil
}
// GuildName is a convenience function for getting the name of a guild
func GuildName(gID int64) (name string) {
g := State.Guild(true, gID)
g.RLock()
name = g.Guild.Name
g.RUnlock()
return
}
func SnowflakeToTime(i int64) time.Time {
flake := snowflake.ID(i)
t := time.Unix(flake.Time()/1000, 0)
return t
}
func SetStatus(status string) {
if status == "" {
status = "v" + common.VERSION + " :)"
}
currentPresenceLock.Lock()
currentStatus = status
currentPresenceLock.Unlock()
updateAllShardStatuses()
}
func SetStreaming(streaming, status string) {
if status == "" {
status = "v" + common.VERSION + " :)"
}
currentPresenceLock.Lock()
currentStatus = status
currentStreamingURL = streaming
currentPresenceLock.Unlock()
updateAllShardStatuses()
}
func updateAllShardStatuses() {
currentPresenceLock.Lock()
stremaing := currentStreamingURL
status := currentStatus
currentPresenceLock.Unlock()
for _, v := range ShardManager.Sessions {
if stremaing == "" {
v.UpdateStatus(0, status)
} else {
v.UpdateStreamingStatus(0, status, stremaing)
}
}
}
// BotProbablyHasPermission returns true if its possible that the bot has the following permission,
// it also returns true if the bot member could not be found or if the guild is not in state (hence, probably)
func BotProbablyHasPermission(guildID int64, channelID int64, permission int) bool {
gs := State.Guild(true, guildID)
if gs == nil {
return true
}
return BotProbablyHasPermissionGS(true, gs, channelID, permission)
}
// BotProbablyHasPermissionGS is the same as BotProbablyHasPermission but with a guildstate instead of guildid
func BotProbablyHasPermissionGS(lock bool, gs *dstate.GuildState, channelID int64, permission int) bool {
perms, err := gs.MemberPermissions(lock, channelID, common.BotUser.ID)
if err != nil && err != dstate.ErrChannelNotFound {
logrus.WithError(err).WithField("guild", gs.ID).Error("Failed checking perms")
return true
}
if perms&permission == permission {
return true
}
if perms&discordgo.PermissionAdministrator != 0 {
return true
}
return false
}
func SendMessage(guildID int64, channelID int64, msg string) (permsOK bool, resp *discordgo.Message, err error) {
if !BotProbablyHasPermission(guildID, channelID, discordgo.PermissionSendMessages) {
return false, nil, nil
}
resp, err = common.BotSession.ChannelMessageSend(channelID, msg)
permsOK = true
return
}
func SendMessageGS(gs *dstate.GuildState, channelID int64, msg string) (permsOK bool, resp *discordgo.Message, err error) {
if !BotProbablyHasPermissionGS(true, gs, channelID, discordgo.PermissionSendMessages|discordgo.PermissionReadMessages) {
return false, nil, nil
}
resp, err = common.BotSession.ChannelMessageSend(channelID, msg)
permsOK = true
return
}
func SendMessageEmbed(guildID int64, channelID int64, msg *discordgo.MessageEmbed) (permsOK bool, resp *discordgo.Message, err error) {
if !BotProbablyHasPermission(guildID, channelID, discordgo.PermissionSendMessages|discordgo.PermissionReadMessages|discordgo.PermissionEmbedLinks) {
return false, nil, nil
}
resp, err = common.BotSession.ChannelMessageSendEmbed(channelID, msg)
permsOK = true
return
}
func SendMessageEmbedGS(gs *dstate.GuildState, channelID int64, msg *discordgo.MessageEmbed) (permsOK bool, resp *discordgo.Message, err error) {
if !BotProbablyHasPermissionGS(true, gs, channelID, discordgo.PermissionSendMessages|discordgo.PermissionReadMessages|discordgo.PermissionEmbedLinks) {
return false, nil, nil
}
resp, err = common.BotSession.ChannelMessageSendEmbed(channelID, msg)
permsOK = true
return
}
// IsGuildOnCurrentProcess returns whether the guild is on one of the shards for this process
func IsGuildOnCurrentProcess(guildID int64) bool {
processShardsLock.RLock()
shardID := int((guildID >> 22) % int64(totalShardCount))
onProcess := common.ContainsIntSlice(processShards, shardID)
processShardsLock.RUnlock()
return onProcess
}
// GuildShardID returns the shard id for the provided guild id
func GuildShardID(guildID int64) int {
totShards := GetTotalShards()
shardID := int((guildID >> 22) % totShards)
return shardID
}
var runShardPollerOnce sync.Once
// GetTotalShards either retrieves the total shards from passed command line if the bot is set to run in the same process
// or it starts a background poller to poll redis for it every second
func GetTotalShards() int64 {
// if the bot is running on this process, then we know the number of total shards
if Enabled && totalShardCount != 0 {
return int64(totalShardCount)
}
// otherwise we poll it from redis every second
runShardPollerOnce.Do(func() {
err := fetchTotalShardsFromRedis()
if err != nil {
panic("failed retrieving shards")
}
go runNumShardsUpdater()
})
return atomic.LoadInt64(redisSetTotalShards)
}
var redisSetTotalShards = new(int64)
func runNumShardsUpdater() {
t := time.NewTicker(time.Second)
for {
err := fetchTotalShardsFromRedis()
if err != nil {
logrus.WithError(err).Error("[botrest] failed retrieving total shards")
}
<-t.C
}
}
func fetchTotalShardsFromRedis() error {
var result int64
err := common.RedisPool.Do(radix.Cmd(&result, "GET", "yagpdb_total_shards"))
if err != nil {
return err
}
old := atomic.SwapInt64(redisSetTotalShards, result)
if old != result {
logrus.Info("[botrest] new shard count received: ", old, " -> ", result)
}
return nil
}
func GetProcessShards() []int {
processShardsLock.RLock()
cop := make([]int, len(processShards))
copy(cop, processShards)
processShardsLock.RUnlock()
return cop
}