-
Notifications
You must be signed in to change notification settings - Fork 921
/
bot.go
372 lines (301 loc) · 10.5 KB
/
bot.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package autorole
import (
"fmt"
"strconv"
"sync"
"time"
"emperror.dev/errors"
"github.com/jonas747/dcmd/v4"
"github.com/jonas747/discordgo/v2"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/common/scheduledevents2"
scheduledEventsModels "github.com/jonas747/yagpdb/common/scheduledevents2/models"
"github.com/mediocregopher/radix/v3"
)
var _ bot.BotInitHandler = (*Plugin)(nil)
var _ bot.BotStopperHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)
func (p *Plugin) AddCommands() {
commands.AddRootCommands(p, roleCommands...)
}
type assignRoleEventdata struct {
UserID int64
RoleID int64 // currently unused
}
func (p *Plugin) BotInit() {
eventsystem.AddHandlerAsyncLast(p, onMemberJoin, eventsystem.EventGuildMemberAdd)
// eventsystem.AddHandlerAsyncLast(p, HandlePresenceUpdate, eventsystem.EventPresenceUpdate)
eventsystem.AddHandlerAsyncLastLegacy(p, handleGuildChunk, eventsystem.EventGuildMembersChunk)
eventsystem.AddHandlerAsyncLast(p, handleGuildMemberUpdate, eventsystem.EventGuildMemberUpdate)
scheduledevents2.RegisterHandler("autorole_assign_role", assignRoleEventdata{}, handleAssignRole)
// go runDurationChecker()
}
func (p *Plugin) StopBot(wg *sync.WaitGroup) {
wg.Done()
}
var roleCommands = []*commands.YAGCommand{
{
CmdCategory: commands.CategoryDebug,
Name: "Roledbg",
Description: "Debug debug debug autorole assignment",
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
var processing int
err := common.RedisPool.Do(radix.Cmd(&processing, "GET", KeyProcessing(parsed.GuildData.GS.ID)))
return fmt.Sprintf("Processing %d users.", processing), err
},
},
}
// HandlePresenceUpdate makes sure the member with joined_at is available for the relevant guilds
// TODO: Figure out a solution that scales better
// func HandlePresenceUpdate(evt *eventsystem.EventData) (retry bool, err error) {
// p := evt.PresenceUpdate()
// if p.Status == discordgo.StatusOffline {
// return
// }
// gs := evt.GS
// gs.RLock()
// m := gs.Member(false, p.User.ID)
// if m != nil && m.MemberSet {
// gs.RUnlock()
// return false, nil
// }
// gs.RUnlock()
// config, err := GuildCacheGetGeneralConfig(gs)
// if err != nil {
// return true, errors.WithStackIf(err)
// }
// if !config.OnlyOnJoin && config.Role != 0 {
// go bot.GetMember(gs.ID, p.User.ID)
// }
// return false, nil
// }
func saveGeneral(guildID int64, config *GeneralConfig) {
err := common.SetRedisJson(KeyGeneral(guildID), config)
if err != nil {
logger.WithError(err).Error("Failed saving autorole config")
} else {
pubsub.Publish("autorole_stop_processing", guildID, nil)
}
}
func onMemberJoin(evt *eventsystem.EventData) (retry bool, err error) {
addEvt := evt.GuildMemberAdd()
config, err := GuildCacheGetGeneralConfig(addEvt.GuildID)
if err != nil {
return true, errors.WithStackIf(err)
}
if config.Role == 0 || evt.GS.GetRole(config.Role) == nil {
return
}
// ms := evt.GS.MemberCopy(true, addEvt.User.ID)
// if ms == nil {
// logger.Error("Member not found in add event")
// return
// }
if config.RequiredDuration < 1 && config.CanAssignTo(addEvt.Roles, time.Now()) {
_, retry, err = assignRole(config, addEvt.GuildID, addEvt.User.ID)
return retry, err
}
if config.RequiredDuration > 0 && !config.OnlyOnJoin {
err = scheduledevents2.ScheduleEvent("autorole_assign_role", addEvt.GuildID,
time.Now().Add(time.Minute*time.Duration(config.RequiredDuration)), &assignRoleEventdata{UserID: addEvt.User.ID})
return bot.CheckDiscordErrRetry(err), err
}
return false, nil
}
func assignRole(config *GeneralConfig, guildID int64, targetID int64) (disabled bool, retry bool, err error) {
analytics.RecordActiveUnit(guildID, &Plugin{}, "assigned_role")
err = common.BotSession.GuildMemberRoleAdd(guildID, targetID, config.Role)
if err != nil {
switch code, _ := common.DiscordError(err); code {
case discordgo.ErrCodeUnknownMember:
logger.WithError(err).Error("Unknown member when trying to assign role")
case discordgo.ErrCodeMissingPermissions, discordgo.ErrCodeMissingAccess, discordgo.ErrCodeUnknownRole:
logger.WithError(err).Warn("disabling autorole from error")
cop := *config
cop.Role = 0
saveGeneral(guildID, &cop)
return true, false, nil
default:
return false, bot.CheckDiscordErrRetry(err), err
}
}
return false, false, nil
}
func (conf *GeneralConfig) CanAssignTo(currentRoles []int64, joinedAt time.Time) bool {
if time.Since(joinedAt) < time.Duration(conf.RequiredDuration)*time.Minute {
return false
}
if len(conf.IgnoreRoles) < 1 && len(conf.RequiredRoles) < 1 {
return true
}
for _, ignoreRole := range conf.IgnoreRoles {
if common.ContainsInt64Slice(currentRoles, ignoreRole) {
return false
}
}
// If require roles are set up, make sure the member has one of them
if len(conf.RequiredRoles) > 0 {
for _, reqRole := range conf.RequiredRoles {
if common.ContainsInt64Slice(currentRoles, reqRole) {
return true
}
}
return false
}
return true
}
func RedisKeyGuildChunkProecssing(gID int64) string {
return "autorole_guild_chunk_processing:" + strconv.FormatInt(gID, 10)
}
func handleGuildChunk(evt *eventsystem.EventData) {
chunk := evt.GuildMembersChunk()
err := common.RedisPool.Do(radix.Cmd(nil, "SETEX", RedisKeyGuildChunkProecssing(chunk.GuildID), "100", "1"))
if err != nil {
logger.WithError(err).Error("failed marking autorole chunk processing")
}
config, err := GetGeneralConfig(chunk.GuildID)
if err != nil {
return
}
if config.Role == 0 || config.OnlyOnJoin {
return
}
go assignFromGuildChunk(chunk.GuildID, config, chunk.Members)
}
func assignFromGuildChunk(guildID int64, config *GeneralConfig, members []*discordgo.Member) {
lastTimeUpdatedBlockingKey := time.Now()
lastTimeUpdatedConfig := time.Now()
for _, m := range members {
joinedAt, err := m.JoinedAt.Parse()
if err != nil {
logger.WithError(err).WithField("ts", m.JoinedAt).WithField("user", m.User.ID).WithField("guild", guildID).Error("failed parsing join timestamp")
if config.RequiredDuration > 0 {
continue // Need the joined_at field for this
}
}
if !config.CanAssignTo(m.Roles, joinedAt) {
continue
}
// already has role
if common.ContainsInt64Slice(m.Roles, config.Role) {
continue
}
time.Sleep(time.Second * 2)
logger.Println("assigning to ", m.User.ID, " from guild chunk event")
disabled, _, err := assignRole(config, guildID, m.User.ID)
if err != nil {
logger.WithError(err).WithField("user", m.User.ID).WithField("guild", guildID).Error("failed adding autorole role")
}
if disabled {
break
}
if time.Since(lastTimeUpdatedConfig) > time.Second*10 {
// Refresh the config occasionally to make sure it dosen't go stale
newConf, err := GetGeneralConfig(guildID)
if err == nil {
config = newConf
} else {
return
}
lastTimeUpdatedConfig = time.Now()
config = newConf
if config.Role == 0 {
logger.WithField("guild", guildID).Info("autorole role was set to none in the middle of full retroactive assignment, cancelling")
return
}
}
if time.Since(lastTimeUpdatedBlockingKey) > time.Second*10 {
lastTimeUpdatedBlockingKey = time.Now()
err := common.RedisPool.Do(radix.Cmd(nil, "SETEX", RedisKeyGuildChunkProecssing(guildID), "100", "1"))
if err != nil {
logger.WithError(err).Error("failed marking autorole chunk processing")
}
}
}
}
func WorkingOnFullScan(guildID int64) bool {
var b bool
err := common.RedisPool.Do(radix.Cmd(&b, "EXISTS", RedisKeyGuildChunkProecssing(guildID)))
if err != nil {
logger.WithError(err).WithField("guild", guildID).Error("failed checking WorkingOnFullScan")
return false
}
return b
}
func GuildCacheGetGeneralConfig(guildID int64) (*GeneralConfig, error) {
v, err := configCache.Get(guildID)
if err != nil {
return nil, err
}
return v.(*GeneralConfig), nil
}
func handleAssignRole(evt *scheduledEventsModels.ScheduledEvent, data interface{}) (retry bool, err error) {
config, err := GetGeneralConfig(evt.GuildID)
if err != nil {
return true, nil
}
if config.Role == 0 || config.OnlyOnJoin {
// settings changed after they joined
return false, nil
}
dataCast := data.(*assignRoleEventdata)
member, err := bot.GetMember(evt.GuildID, dataCast.UserID)
if err != nil {
if common.IsDiscordErr(err, discordgo.ErrCodeUnknownMember) {
return false, nil
}
return bot.CheckDiscordErrRetry(err), err
}
parsedT, _ := member.Member.JoinedAt.Parse()
memberDuration := time.Now().Sub(parsedT)
if memberDuration < time.Duration(config.RequiredDuration)*time.Minute {
// settings may have been changed, re-schedule
err = scheduledevents2.ScheduleEvent("autorole_assign_role", evt.GuildID,
time.Now().Add(time.Minute*time.Duration(config.RequiredDuration)), &assignRoleEventdata{UserID: dataCast.UserID})
return bot.CheckDiscordErrRetry(err), err
}
if !config.CanAssignTo(member.Member.Roles, parsedT) {
// some other reason they can't get the role, such as whitelist or ignore roles
return false, nil
}
go analytics.RecordActiveUnit(evt.GuildID, &Plugin{}, "assigned_role")
_, retry, err = assignRole(config, evt.GuildID, dataCast.UserID)
return retry, err
}
func handleGuildMemberUpdate(evt *eventsystem.EventData) (retry bool, err error) {
update := evt.GuildMemberUpdate()
config, err := GuildCacheGetGeneralConfig(update.GuildID)
if err != nil {
return true, errors.WithStackIf(err)
}
if config.Role == 0 || config.OnlyOnJoin || evt.GS.GetRole(config.Role) == nil {
return false, nil
}
if common.ContainsInt64Slice(update.Member.Roles, config.Role) {
return false, nil
}
if !config.CanAssignTo(update.Member.Roles, time.Time{}) {
return false, nil
}
if config.RequiredDuration > 0 {
// check the autorole duration
ms, err := bot.GetMember(update.GuildID, update.User.ID)
if err != nil {
return bot.CheckDiscordErrRetry(err), errors.WithStackIf(err)
}
parsedT, _ := ms.Member.JoinedAt.Parse()
if time.Since(parsedT) < time.Duration(config.RequiredDuration)*time.Minute {
// haven't been a member long enough
return false, nil
}
}
go analytics.RecordActiveUnit(update.GuildID, &Plugin{}, "assigned_role")
// if we branched here then all the checks passed and they should be assigned the role
_, retry, err = assignRole(config, update.GuildID, update.User.ID)
return retry, err
}