-
Notifications
You must be signed in to change notification settings - Fork 926
/
plugin_bot.go
556 lines (472 loc) · 15.5 KB
/
plugin_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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package reputation
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/botlabs-gg/yagpdb/v2/analytics"
"github.com/botlabs-gg/yagpdb/v2/bot/paginatedmessages"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/bot/eventsystem"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/lib/dcmd"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/reputation/models"
"github.com/botlabs-gg/yagpdb/v2/web"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
var _ bot.BotInitHandler = (*Plugin)(nil)
var _ commands.CommandProvider = (*Plugin)(nil)
func (p *Plugin) AddCommands() {
commands.AddRootCommands(p, cmds...)
}
func (p *Plugin) BotInit() {
eventsystem.AddHandlerAsyncLastLegacy(p, handleMessageCreate, eventsystem.EventMessageCreate)
}
var thanksRegex = regexp.MustCompile(`(?i)( |\n|^)(thanks?\pP*|danks|ty|thx|\+rep|\+ ?\<\@[0-9]*\>)( |\n|$)`)
func createRepDisabledError(guild *dcmd.GuildContextData) string {
return fmt.Sprintf("**The reputation system is disabled for this server.** Enable it at: <%s/reputation>.", web.ManageServerURL(guild))
}
func handleMessageCreate(evt *eventsystem.EventData) {
msg := evt.MessageCreate()
conf, err := GetConfig(evt.Context(), msg.GuildID)
if err != nil || !conf.Enabled || conf.DisableThanksDetection {
return
}
if !bot.IsNormalUserMessage(msg.Message) {
return
}
if len(msg.Mentions) < 1 || msg.GuildID == 0 || msg.Author.Bot {
return
}
if !evt.HasFeatureFlag(featureFlagThanksEnabled) {
return
}
if !thanksRegex.MatchString(msg.Content) {
return
}
cState := evt.CSOrThread()
if cState == nil {
return // No channel state, ignore
}
channelID := msg.ChannelID
// Check if thanks detection is allowed in the parent channel
if cState.Type.IsThread() {
channelID = cState.ParentID
}
if !isThanksDetectionAllowedInChannel(conf, channelID) {
return
}
who := msg.Mentions[0]
if who.ID == msg.Author.ID {
return
}
target, err := bot.GetMember(msg.GuildID, who.ID)
sender := dstate.MemberStateFromMember(msg.Member)
if err != nil {
logger.WithError(err).Error("Failed retrieving target member")
return
}
if err = CanModifyRep(conf, sender, target); err != nil {
return
}
err = ModifyRep(evt.Context(), conf, msg.GuildID, sender, target, 1)
if err != nil {
if err == ErrCooldown {
// Ignore this error silently
return
}
logger.WithError(err).Error("Failed giving rep")
return
}
go analytics.RecordActiveUnit(msg.GuildID, &Plugin{}, "auto_add_rep")
newScore, newRank, err := GetUserStats(msg.GuildID, who.ID)
if err != nil {
newScore = -1
newRank = -1
logger.WithError(err).Error("Failed retrieving target stats")
return
}
content := fmt.Sprintf("Gave +1 %s to **%s** (current: `#%d` - `%d`)", conf.PointsName, who.Mention(), newRank, newScore)
common.BotSession.ChannelMessageSend(msg.ChannelID, content)
}
var cmds = []*commands.YAGCommand{
{
CmdCategory: commands.CategoryFun,
Name: "TakeRep",
Aliases: []string{"-", "tr", "trep", "-rep"},
Description: "Takes away rep from someone",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.User},
{Name: "Num", Type: dcmd.Int, Default: 1},
},
SlashCommandEnabled: true,
DefaultEnabled: false,
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
if parsed.Args[1].Int() < 1 {
return "**rep amount should be greater than or equal to 1**", nil
}
parsed.Args[1].Value = -parsed.Args[1].Int()
return CmdGiveRep(parsed)
},
},
{
CmdCategory: commands.CategoryFun,
Name: "GiveRep",
Aliases: []string{"+", "gr", "grep", "+rep"},
Description: "Gives rep to someone",
RequiredArgs: 1,
SlashCommandEnabled: true,
DefaultEnabled: false,
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.User},
{Name: "Num", Type: dcmd.Int, Default: 1},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
if parsed.Args[1].Int() < 1 {
return "**rep amount should be greater than or equal to 1**", nil
}
return CmdGiveRep(parsed)
},
},
{
CmdCategory: commands.CategoryFun,
Name: "SetRep",
Aliases: []string{"SetRepID"}, // alias for legacy reasons, used to be a standalone command
Description: "Sets someones rep, this is an admin command and bypasses cooldowns and other restrictions.",
RequiredArgs: 2,
SlashCommandEnabled: true,
DefaultEnabled: false,
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.UserID},
{Name: "Num", Type: dcmd.Int},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
conf, err := GetConfig(parsed.Context(), parsed.GuildData.GS.ID)
if err != nil {
return "An error occurred while finding the server config", err
}
if !conf.Enabled {
return createRepDisabledError(parsed.GuildData), nil
}
if !IsAdmin(parsed.GuildData.GS, parsed.GuildData.MS, conf) {
return "You're not a reputation admin. (no manage server perms and no rep admin role)", nil
}
targetID := parsed.Args[0].Int64()
targetUsername := strconv.FormatInt(targetID, 10)
targetMember, _ := bot.GetMember(parsed.GuildData.GS.ID, targetID)
if targetMember != nil {
targetUsername = targetMember.User.Username
} else {
prevMember, err := userPresentInRepLog(targetID, parsed.GuildData.GS.ID, parsed)
if err != nil {
return nil, err
}
if !prevMember {
return "Invalid User. This user never received/gave rep in this server", nil
}
}
err = SetRep(parsed.Context(), parsed.GuildData.GS.ID, parsed.GuildData.MS.User.ID, targetID, int64(parsed.Args[1].Int()))
if err != nil {
return nil, err
}
return fmt.Sprintf("Set **%s** %s to `%d`", targetUsername, conf.PointsName, parsed.Args[1].Int()), nil
},
},
{
CmdCategory: commands.CategoryFun,
Name: "DelRep",
Description: "Deletes someone from the reputation list completely, this cannot be undone.",
RequiredArgs: 1,
SlashCommandEnabled: true,
DefaultEnabled: false,
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.UserID},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
conf, err := GetConfig(parsed.Context(), parsed.GuildData.GS.ID)
if err != nil {
return "An error occurred while finding the server config", err
}
if !conf.Enabled {
return createRepDisabledError(parsed.GuildData), nil
}
if !IsAdmin(parsed.GuildData.GS, parsed.GuildData.MS, conf) {
return "You're not an reputation admin. (no manage servers perms and no rep admin role)", nil
}
target := parsed.Args[0].Int64()
err = DelRep(parsed.Context(), parsed.GuildData.GS.ID, target)
if err != nil {
return nil, err
}
return fmt.Sprintf("Deleted all of %d's %s.", target, conf.PointsName), nil
},
},
{
CmdCategory: commands.CategoryFun,
Name: "RepLog",
Aliases: []string{"replogs"},
Description: "Shows the rep log for the specified user.",
SlashCommandEnabled: true,
DefaultEnabled: false,
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.UserID},
{Name: "Page", Type: dcmd.Int, Default: 1},
},
ArgumentCombos: [][]int{{}, {0}, {1}, {0, 1}},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
conf, err := GetConfig(parsed.Context(), parsed.GuildData.GS.ID)
if err != nil {
return "An error occurred while finding the server config", err
}
if !IsAdmin(parsed.GuildData.GS, parsed.GuildData.MS, conf) {
return "You're not an reputation admin. (no manage servers perms and no rep admin role)", nil
}
targetID := parsed.Args[0].Int64()
if targetID == 0 {
targetID = parsed.Author.ID
}
const entriesPerPage = 20
offset := (parsed.Args[1].Int() - 1) * entriesPerPage
logEntries, err := models.ReputationLogs(qm.Where("guild_id = ? AND (receiver_id = ? OR sender_id = ?)", parsed.GuildData.GS.ID, targetID, targetID), qm.OrderBy("id desc"), qm.Limit(entriesPerPage), qm.Offset(offset)).AllG(parsed.Context())
if err != nil {
return nil, err
}
if len(logEntries) < 1 {
return "No entries", nil
}
// grab the up to date info on as many users as we can
membersToGrab := make([]int64, 1, len(logEntries))
membersToGrab[0] = targetID
OUTER:
for _, entry := range logEntries {
for _, v := range membersToGrab {
if entry.ReceiverID == targetID {
if v == entry.SenderID {
continue OUTER
}
} else {
if v == entry.ReceiverID {
continue OUTER
}
}
}
if entry.ReceiverID == targetID {
membersToGrab = append(membersToGrab, entry.SenderID)
} else {
membersToGrab = append(membersToGrab, entry.ReceiverID)
}
}
members, _ := bot.GetMembers(parsed.GuildData.GS.ID, membersToGrab...)
// finally display the results
var out strings.Builder
out.WriteString("```\n")
for i, entry := range logEntries {
receiver := entry.ReceiverUsername
sender := entry.SenderUsername
for _, v := range members {
if v.User.ID == entry.ReceiverID {
receiver = v.User.String()
}
if v.User.ID == entry.SenderID {
sender = v.User.String()
}
}
if receiver == "" {
receiver = discordgo.StrID(entry.ReceiverID)
}
if sender == "" {
sender = discordgo.StrID(entry.SenderID)
}
f := "#%2d: %-15s: %s gave %s: %d points"
if entry.SetFixedAmount {
f = "#%2d: %-15s: %s set %s points to: %d"
}
out.WriteString(fmt.Sprintf(f, i+offset+1, entry.CreatedAt.UTC().Format("02 Jan 06 15:04"), sender, receiver, entry.Amount))
out.WriteRune('\n')
}
out.WriteString("```\n")
out.WriteString(fmt.Sprint("Page ", parsed.Args[1].Int()))
return out.String(), nil
},
},
{
CmdCategory: commands.CategoryFun,
Name: "Rep",
Description: "Shows yours or the specified users current rep and rank",
Arguments: []*dcmd.ArgDef{
{Name: "User", Type: dcmd.User},
},
SlashCommandEnabled: true,
DefaultEnabled: false,
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
target := parsed.Author
if parsed.Args[0].Value != nil {
target = parsed.Args[0].Value.(*discordgo.User)
}
conf, err := GetConfig(parsed.Context(), parsed.GuildData.GS.ID)
if err != nil {
return "An error occurred finding the server config", err
}
score, rank, err := GetUserStats(parsed.GuildData.GS.ID, target.ID)
if err != nil {
if err == ErrUserNotFound {
rank = -1
} else {
return nil, err
}
}
rankStr := "#ω"
if rank != -1 {
rankStr = strconv.FormatInt(int64(rank), 10)
}
return fmt.Sprintf("**%s**: **%d** %s (#**%s**)", target.Username, score, conf.PointsName, rankStr), nil
},
},
{
CmdCategory: commands.CategoryFun,
Name: "TopRep",
Description: "Shows rep leaderboard on the server",
Arguments: []*dcmd.ArgDef{
{Name: "Page", Type: dcmd.Int, Default: 0},
},
ArgSwitches: []*dcmd.ArgDef{
{Name: "user", Help: "User to search for in the leaderboard", Type: dcmd.UserID},
},
SlashCommandEnabled: true,
DefaultEnabled: true,
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
page := parsed.Args[0].Int()
if id := parsed.Switch("user").Int64(); id != 0 {
const query = `
SELECT pos
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY points DESC) AS pos, user_id
FROM reputation_users
WHERE guild_id = $1
) as ordered_users
WHERE user_id = $2
`
var pos int
err := common.PQ.QueryRow(query, parsed.GuildData.GS.ID, id).Scan(&pos)
if err != nil {
if err == sql.ErrNoRows {
return "Could not find that user on the leaderboard", nil
}
return "Failed finding that user on the leaderboard, try again", err
}
page = (pos-1)/15 + 1 // pos and page are both one-based
}
if page < 1 {
page = 1
}
if parsed.Context().Value(paginatedmessages.CtxKeyNoPagination) != nil {
return topRepPager(parsed.GuildData.GS.ID, nil, page)
}
_, err := paginatedmessages.CreatePaginatedMessage(parsed.GuildData.GS.ID, parsed.ChannelID, page, 0, func(p *paginatedmessages.PaginatedMessage, page int) (*discordgo.MessageEmbed, error) {
return topRepPager(parsed.GuildData.GS.ID, p, page)
})
return nil, err
},
},
}
func topRepPager(guildID int64, p *paginatedmessages.PaginatedMessage, page int) (*discordgo.MessageEmbed, error) {
offset := (page - 1) * 15
entries, err := TopUsers(guildID, offset, 15)
if err != nil {
return nil, err
}
detailed, err := DetailedLeaderboardEntries(guildID, entries)
if err != nil {
return nil, err
}
if len(entries) < 1 && p != nil && p.LastResponse != nil { //Dont send No Results error on first execution
return nil, paginatedmessages.ErrNoResults
}
embed := &discordgo.MessageEmbed{
Title: "Reputation leaderboard",
}
leaderboardURL := web.BaseURL() + "/public/" + discordgo.StrID(guildID) + "/reputation/leaderboard"
out := "```\n# -- Points -- User\n"
for _, v := range detailed {
user := v.Username
if user == "" {
user = "unknown ID:" + strconv.FormatInt(v.UserID, 10)
}
out += fmt.Sprintf("#%02d: %6d - %s\n", v.Rank, v.Points, user)
}
out += "```\n" + "Full leaderboard: <" + leaderboardURL + ">"
embed.Description = out
return embed, nil
}
func CmdGiveRep(parsed *dcmd.Data) (interface{}, error) {
target := parsed.Args[0].Value.(*discordgo.User)
conf, err := GetConfig(parsed.Context(), parsed.GuildData.GS.ID)
if err != nil {
return nil, err
}
if !conf.Enabled {
return createRepDisabledError(parsed.GuildData), nil
}
pointsName := conf.PointsName
if target.ID == parsed.Author.ID {
return fmt.Sprintf("You can't modify your own %s... **Silly**", pointsName), nil
}
sender := parsed.GuildData.MS
receiver, err := bot.GetMember(parsed.GuildData.GS.ID, target.ID)
if err != nil {
return nil, err
}
amount := parsed.Args[1].Int()
err = ModifyRep(parsed.Context(), conf, parsed.GuildData.GS.ID, sender, receiver, int64(amount))
if err != nil {
if cast, ok := err.(UserError); ok {
return cast, nil
}
return nil, err
}
newScore, newRank, err := GetUserStats(parsed.GuildData.GS.ID, target.ID)
if err != nil {
newScore = -1
newRank = -1
return nil, err
}
actionStr := ""
targetStr := "to"
if amount > 0 {
actionStr = "Gave"
} else {
actionStr = "Took away"
amount = -amount
targetStr = "from"
}
msg := fmt.Sprintf("%s `%d` %s %s **%s** (current: `#%d` - `%d`)", actionStr, amount, pointsName, targetStr, target.Username, newRank, newScore)
return msg, nil
}
// Function that checks if the given user has ever received/gave rep in the given server
func userPresentInRepLog(userID int64, guildID int64, parsed *dcmd.Data) (found bool, err error) {
logEntries, err := models.ReputationLogs(qm.Where("guild_id = ? AND (receiver_id = ? OR sender_id = ?)", guildID, userID, userID), qm.OrderBy("id desc"), qm.Limit(1)).AllG(parsed.Context())
if err != nil {
return false, err
}
if len(logEntries) < 1 {
return false, nil
}
return true, nil
}
// Checks if the thanks detection is allowed to be run in the given channel
func isThanksDetectionAllowedInChannel(config *models.ReputationConfig, channelID int64) bool {
if len(config.BlacklistedThanksChannels) > 0 {
if common.ContainsInt64Slice(config.BlacklistedThanksChannels, channelID) {
return false
}
}
if len(config.WhitelistedThanksChannels) > 0 {
return common.ContainsInt64Slice(config.WhitelistedThanksChannels, channelID)
}
return true
}