forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reputation.go
372 lines (304 loc) · 9.09 KB
/
reputation.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 reputation
//go:generate sqlboiler --no-hooks psql
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"
"emperror.dev/errors"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/botrest"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/reputation/models"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
)
var logger = common.GetPluginLogger(&Plugin{})
func RegisterPlugin() {
plugin := &Plugin{}
common.InitSchemas("reputation", DBSchemas...)
common.RegisterPlugin(plugin)
}
type Plugin struct{}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Reputation",
SysName: "reputation",
Category: common.PluginCategoryMisc,
}
}
func DefaultConfig(guildID int64) *models.ReputationConfig {
return &models.ReputationConfig{
GuildID: guildID,
PointsName: "Rep",
Enabled: false,
Cooldown: 120,
MaxGiveAmount: 1,
}
}
func KeyCooldown(guildID, userID int64) string {
return "reputation_cooldown:" + discordgo.StrID(guildID) + ":" + discordgo.StrID(userID)
}
var (
ErrUserNotFound = errors.New("User not found")
)
func GetUserStats(guildID, userID int64) (score int64, rank int, err error) {
const query = `SELECT points, position FROM
(
SELECT user_id, points,
RANK() OVER(ORDER BY points DESC) AS position
FROM reputation_users WHERE guild_id = $1
) AS w
WHERE user_id = $2`
row := common.PQ.QueryRow(query, guildID, userID)
err = row.Scan(&score, &rank)
if err != nil {
if err == sql.ErrNoRows {
err = ErrUserNotFound
}
}
return
}
type RankEntry struct {
Rank int `json:"rank"`
UserID int64 `json:"user_id"`
Points int64 `json:"points"`
}
func TopUsers(guildID int64, offset, limit int) ([]*RankEntry, error) {
const query = `SELECT points, position, user_id FROM
(
SELECT user_id, points,
RANK() OVER(ORDER BY points DESC) AS position
FROM reputation_users WHERE guild_id = $1
) AS w
ORDER BY points desc
LIMIT $2 OFFSET $3`
rows, err := common.PQ.Query(query, guildID, limit, offset)
if err != nil {
if err == sql.ErrNoRows {
return []*RankEntry{}, nil
}
return nil, err
}
defer rows.Close()
result := make([]*RankEntry, 0, limit)
for rows.Next() {
var rank int
var userID int64
var points int64
err = rows.Scan(&points, &rank, &userID)
if err != nil {
return nil, err
}
result = append(result, &RankEntry{
Rank: rank,
UserID: userID,
Points: points,
})
}
return result, nil
}
type UserError string
func (b UserError) Error() string {
return string(b)
}
var (
ErrMissingRequiredGiveRole = UserError("You don't have any of the required roles to give points")
ErrMissingRequiredReceiveRole = UserError("Target don't have any of the required roles to receive points")
ErrBlacklistedGive = UserError("Blacklisted from giving points")
ErrBlacklistedReceive = UserError("Blacklisted from receiving points")
ErrCooldown = UserError("You're still on cooldown")
)
func ModifyRep(ctx context.Context, conf *models.ReputationConfig, guildID int64, sender, receiver *dstate.MemberState, amount int64) (err error) {
if conf == nil {
conf, err = GetConfig(ctx, guildID)
if err != nil {
return
}
}
if err = CanModifyRep(conf, sender, receiver); err != nil {
return
}
if amount > 0 && amount > conf.MaxGiveAmount {
err = UserError(fmt.Sprintf("Can't give that much (max %d)", conf.MaxGiveAmount))
return
} else if amount < 0 && -amount > conf.MaxRemoveAmount {
err = UserError(fmt.Sprintf("Can't remove that much (max %d)", conf.MaxRemoveAmount))
return
} else if amount == 0 {
return nil
}
ok, err := CheckSetCooldown(conf, sender.ID)
if err != nil || !ok {
if err == nil {
err = ErrCooldown
}
return
}
err = insertUpdateUserRep(ctx, guildID, receiver.ID, amount)
if err != nil {
// Clear the cooldown since it failed updating the rep
ClearCooldown(guildID, sender.ID)
return
}
receiver.Guild.RLock()
defer receiver.Guild.RUnlock()
receiverUsername := receiver.Username + "#" + receiver.StrDiscriminator()
senderUsername := sender.Username + "#" + sender.StrDiscriminator()
// Add the log element
entry := &models.ReputationLog{
GuildID: guildID,
SenderID: sender.ID,
SenderUsername: senderUsername,
ReceiverID: receiver.ID,
ReceiverUsername: receiverUsername,
SetFixedAmount: false,
Amount: amount,
}
err = entry.InsertG(ctx, boil.Infer())
if err != nil {
err = errors.WithMessage(err, "ModifyRep log entry.Insert")
}
return
}
func insertUpdateUserRep(ctx context.Context, guildID, userID int64, amount int64) (err error) {
// upsert query which is too advanced for orms
const query = `
INSERT INTO reputation_users (created_at, guild_id, user_id, points)
VALUES ($1, $2, $3, $4)
ON CONFLICT (guild_id, user_id)
DO UPDATE SET points = reputation_users.points + $4;
`
_, err = common.PQ.ExecContext(ctx, query, time.Now(), guildID, userID, amount)
return
}
// Returns a user error if the sender can not modify the rep of receiver
// Admins are always able to modify the rep of everyone
func CanModifyRep(conf *models.ReputationConfig, sender, receiver *dstate.MemberState) error {
if common.ContainsInt64SliceOneOf(sender.Roles, conf.AdminRoles) {
return nil
}
if len(conf.RequiredGiveRoles) > 0 && !common.ContainsInt64SliceOneOf(sender.Roles, conf.RequiredGiveRoles) {
return ErrMissingRequiredGiveRole
}
if len(conf.RequiredReceiveRoles) > 0 && !common.ContainsInt64SliceOneOf(receiver.Roles, conf.RequiredReceiveRoles) {
return ErrMissingRequiredReceiveRole
}
if common.ContainsInt64SliceOneOf(sender.Roles, conf.BlacklistedGiveRoles) {
return ErrBlacklistedGive
}
if common.ContainsInt64SliceOneOf(receiver.Roles, conf.BlacklistedReceiveRoles) {
return ErrBlacklistedReceive
}
return nil
}
func IsAdmin(gs *dstate.GuildState, member *dstate.MemberState, config *models.ReputationConfig) bool {
memberPerms, _ := gs.MemberPermissions(false, gs.ID, member.ID)
if memberPerms&discordgo.PermissionManageServer != 0 {
return true
}
if common.ContainsInt64SliceOneOf(member.Roles, config.AdminRoles) {
return true
}
return false
}
func SetRep(ctx context.Context, gid int64, senderID, userID int64, points int64) error {
user := &models.ReputationUser{
GuildID: gid,
UserID: userID,
Points: points,
}
err := user.UpsertG(ctx, true, []string{"guild_id", "user_id"}, boil.Whitelist("points"), boil.Infer())
if err != nil {
return err
}
// Insert log entry
entry := &models.ReputationLog{
GuildID: gid,
SenderID: senderID,
ReceiverID: userID,
SetFixedAmount: true,
Amount: points,
}
err = entry.InsertG(ctx, boil.Infer())
return errors.WithMessage(err, "SetRep log entry.Insert")
}
func DelRep(ctx context.Context, gid int64, userID int64) error {
_, err := models.ReputationUsers(qm.Where("guild_id = ? AND user_id = ?", gid, userID)).DeleteAll(ctx, common.PQ)
return err
}
// CheckSetCooldown checks and updates the reputation cooldown of a user,
// it returns true if the user was not on cooldown
func CheckSetCooldown(conf *models.ReputationConfig, senderID int64) (bool, error) {
if conf.Cooldown < 1 {
return true, nil
}
var resp string
err := common.RedisPool.Do(retryableredis.FlatCmd(&resp, "SET", KeyCooldown(conf.GuildID, senderID), true, "EX", conf.Cooldown, "NX"))
if resp != "OK" {
return false, err
}
return true, err
}
func ClearCooldown(guildID, senderID int64) error {
return common.RedisPool.Do(retryableredis.Cmd(nil, "DEL", KeyCooldown(guildID, senderID)))
}
func GetConfig(ctx context.Context, guildID int64) (*models.ReputationConfig, error) {
conf, err := models.FindReputationConfigG(ctx, guildID)
if err != nil {
if err == sql.ErrNoRows {
return DefaultConfig(guildID), nil
}
return nil, errors.WrapIf(err, "Reputation.GetConfig")
}
return conf, nil
}
type LeaderboardEntry struct {
*RankEntry
Username string `json:"username"`
Bot bool `json:"bot"`
Avatar string `json:"avatar"`
}
func DetailedLeaderboardEntries(guildID int64, ranks []*RankEntry) ([]*LeaderboardEntry, error) {
var members []*discordgo.Member
var err error
userIDs := make([]int64, len(ranks))
for i := 0; i < len(ranks); i++ {
userIDs[i] = ranks[i].UserID
}
if bot.Running {
var tmp []*dstate.MemberState
tmp, err = bot.GetMembers(guildID, userIDs...)
if tmp != nil {
for _, v := range tmp {
members = append(members, v.DGoCopy())
}
}
} else {
members, err = botrest.GetMembers(guildID, userIDs...)
}
if err != nil {
return nil, err
}
var resultEntries = make([]*LeaderboardEntry, len(ranks))
for i := 0; i < len(ranks); i++ {
lEntry := &LeaderboardEntry{
RankEntry: ranks[i],
Username: strconv.FormatInt(userIDs[i], 10),
}
for _, m := range members {
if m.User.ID == userIDs[i] {
lEntry.Username = m.User.Username + "#" + m.User.Discriminator
lEntry.Avatar = m.User.AvatarURL("256")
lEntry.Bot = m.User.Bot
break
}
}
resultEntries[i] = lEntry
}
return resultEntries, nil
}