-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_bot.go
303 lines (257 loc) · 8.37 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
package reputation
import (
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix.v2/redis"
"github.com/sirupsen/logrus"
"regexp"
"strconv"
)
func (p *Plugin) InitBot() {
commands.AddRootCommands(cmds...)
eventsystem.AddHandler(bot.RedisWrapper(handleMessageCreate), eventsystem.EventMessageCreate)
}
var thanksRegex = regexp.MustCompile(`(?i)( |\n|^)(thanks?|danks|ty|thx|\+rep|\+ ?\<\@[0-9]*\>)( |\n|$)`)
func handleMessageCreate(evt *eventsystem.EventData) {
msg := evt.MessageCreate
client := bot.ContextRedis(evt.Context())
if len(msg.Mentions) < 1 {
return
}
if !thanksRegex.MatchString(evt.MessageCreate.Content) {
return
}
who := msg.Mentions[0]
if who.ID == msg.Author.ID {
return
}
cs := bot.State.Channel(true, msg.ChannelID)
if cs.IsPrivate() {
return
}
conf, err := GetConfig(cs.Guild.ID())
if err != nil || !conf.Enabled {
return
}
target, err := bot.GetMember(cs.Guild.ID(), who.ID)
sender, err2 := bot.GetMember(cs.Guild.ID(), msg.Author.ID)
if err != nil || err2 != nil {
if err2 != nil {
err = err2
}
logrus.WithError(err).Error("Failed retrieving bot member")
return
}
if err = CanModifyRep(conf, sender, target); err != nil {
return
}
err = ModifyRep(conf, client, cs.Guild.ID(), sender, target, 1)
if err != nil {
if err == ErrCooldown {
// Ignore this error silently
return
}
logrus.WithError(err).Error("Failed giving rep")
return
}
content := fmt.Sprintf("Gave +1 %s to **%s**", conf.PointsName, who.Username)
common.BotSession.ChannelMessageSend(msg.ChannelID, common.EscapeSpecialMentions(content))
}
var cmds = []*commands.YAGCommand{
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "TakeRep",
Aliases: []string{"-", "tr", "trep"},
Description: "Takes away rep from someone",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.User},
&dcmd.ArgDef{Name: "Num", Type: dcmd.Int, Default: 1},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
parsed.Args[1].Value = -parsed.Args[1].Int()
return CmdGiveRep(parsed)
},
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "GiveRep",
Aliases: []string{"+", "gr", "grep"},
Description: "Gives rep to someone",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.User},
&dcmd.ArgDef{Name: "Num", Type: dcmd.Int, Default: 1},
},
RunFunc: CmdGiveRep,
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "SetRep",
Description: "Sets someones rep, this is an admin command and bypasses cooldowns and other restrictions.",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.User},
&dcmd.ArgDef{Name: "Num", Type: dcmd.Int},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
conf, err := GetConfig(parsed.GS.ID())
if err != nil {
return "An error occured while finding the server config", err
}
member, _ := bot.GetMember(parsed.GS.ID(), parsed.Msg.Author.ID)
parsed.GS.RLock()
if !IsAdmin(parsed.GS, member, conf) {
parsed.GS.RUnlock()
return "You're not an reputation admin. (no manage servers perms and no rep admin role)", nil
}
parsed.GS.RUnlock()
target := parsed.Args[0].Value.(*discordgo.User)
err = SetRep(parsed.GS.ID(), member.User.ID, target.ID, int64(parsed.Args[1].Int()))
if err != nil {
return "Failed setting rep, contact bot owner", err
}
return fmt.Sprintf("Set **%s** %s to `%d`", target.Username, conf.PointsName, parsed.Args[1].Int()), nil
},
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "SetRepID",
Description: "Sets someones rep, by userid, this is an admin command and bypasses cooldowns and other restrictions.",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.Int},
&dcmd.ArgDef{Name: "Num", Type: dcmd.Int},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
conf, err := GetConfig(parsed.GS.ID())
if err != nil {
return "An error occured while finding the server config", err
}
member, _ := bot.GetMember(parsed.GS.ID(), parsed.Msg.Author.ID)
parsed.GS.RLock()
if !IsAdmin(parsed.GS, member, conf) {
parsed.GS.RUnlock()
return "You're not an reputation admin. (no manage servers perms and no rep admin role)", nil
}
parsed.GS.RUnlock()
target := parsed.Args[0].Int64()
err = SetRep(parsed.GS.ID(), member.User.ID, target, int64(parsed.Args[1].Int()))
if err != nil {
return "Failed setting rep, contact bot owner", err
}
return fmt.Sprintf("Set **%d** %s to `%d`", target, conf.PointsName, parsed.Args[1].Int()), nil
},
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "Rep",
Description: "Shows yours or the specified users current rep and rank",
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.User},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
target := parsed.Msg.Author
if parsed.Args[0].Value != nil {
target = parsed.Args[0].Value.(*discordgo.User)
}
conf, err := GetConfig(parsed.GS.ID())
if err != nil {
return "An error occured finding the server config", err
}
score, rank, err := GetUserStats(parsed.GS.ID(), target.ID)
if err != nil {
if err == ErrUserNotFound {
rank = -1
} else {
return "Error retrieving stats", 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
},
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "TopRep",
Description: "Shows top 15 rep on the server",
Arguments: []*dcmd.ArgDef{
{Name: "Offset", Type: dcmd.Int, Default: 0},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
offset := parsed.Args[0].Int()
entries, err := TopUsers(parsed.GS.ID(), offset, 15)
if err != nil {
return "Something went wrong... i may hae had one too many alcohol", err
}
detailed, err := DetailedLeaderboardEntries(parsed.GS.ID(), entries)
if err != nil {
return "Failed filling in the detalis of the leaderboard entries", err
}
leaderboardURL := "https://" + common.Conf.Host + "/public/" + discordgo.StrID(parsed.GS.ID()) + "/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 + ">"
return out, nil
},
},
}
func CmdGiveRep(parsed *dcmd.Data) (interface{}, error) {
target := parsed.Args[0].Value.(*discordgo.User)
conf, err := GetConfig(parsed.GS.ID())
if err != nil {
return "An error occured finding the server config", err
}
pointsName := conf.PointsName
if target.ID == parsed.Msg.Author.ID {
return fmt.Sprintf("You can't modify your own %s... **Silly**", pointsName), nil
}
client := parsed.Context().Value(commands.CtxKeyRedisClient).(*redis.Client)
sender, err := bot.GetMember(parsed.GS.ID(), parsed.Msg.Author.ID)
receiver, err2 := bot.GetMember(parsed.GS.ID(), target.ID)
if err != nil || err2 != nil {
if err2 != nil {
err = err2
}
return "Failed retreiving members", err
}
amount := parsed.Args[1].Int()
err = ModifyRep(conf, client, parsed.GS.ID(), sender, receiver, int64(amount))
if err != nil {
if cast, ok := err.(UserError); ok {
return cast, nil
}
return "Failed modifying your " + pointsName, err
}
newScore, newRank, err := GetUserStats(parsed.GS.ID(), target.ID)
if err != nil {
newScore = -1
newRank = -1
logrus.WithError(err).WithField("guild", parsed.GS.ID()).Error("Failed getting user reputation stats")
}
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
}