forked from ImpactDevelopment/ImpactBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rekt.go
587 lines (518 loc) · 15.7 KB
/
rekt.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package main
import (
"database/sql"
"errors"
"fmt"
"regexp"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"github.com/google/uuid"
)
const (
RateLimit = 5 * time.Minute
TempmuteDuration = 3 * time.Hour
UnmuteInterval = 10 * time.Second
)
var ratelimit = make(map[string]int64)
var ratelimitLock sync.Mutex
var mentionRegex = regexp.MustCompile(`^<(?P<Type>[#@])!?(?P<ID>\d+)>$`)
func evalRatelimit(author string) bool {
ratelimitLock.Lock()
defer ratelimitLock.Unlock()
until := ratelimit[author]
if until < time.Now().UnixNano() { // defaults to 0 so this works properly
ratelimit[author] = time.Now().Add(RateLimit).UnixNano()
return true
}
return false
}
func resp(ch string, text string) error {
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: prettyembedcolor,
Description: text,
Footer: &discordgo.MessageEmbedFooter{
Text: "♿ Impact Client ♿",
},
Timestamp: time.Now().Format(time.RFC3339),
}
_, err := discord.ChannelMessageSendEmbed(ch, embed)
return err
}
// Turns the first one or two args into users and/or channels and also returns whatever args weren't consumed
func getUserAndChannelAndArgs(args []string) (user *discordgo.User, channel *discordgo.Channel, remainingArgs []string) {
remainingArgs = args
if len(args) < 1 {
return
}
user, channel = getUserOrChannelForArg(args[0])
if user != nil || channel != nil {
// Consume an arg
remainingArgs = remainingArgs[1:]
// Don't return since we want to try the second arg too
} else {
// No match on first arg so don't try to match second arg
return
}
// getUserOrChannelForArg always has one nil arg, so if-else instead of if-elseif is fine
if len(args) < 2 {
return
}
if user == nil {
user, _ = getUserOrChannelForArg(args[1])
if user != nil {
// Consume an arg
remainingArgs = remainingArgs[1:]
}
} else {
_, channel = getUserOrChannelForArg(args[1])
if channel != nil {
// Consume an arg
remainingArgs = remainingArgs[1:]
}
}
return
}
// Send a blocking api request if a match is found
func getUserOrChannelForArg(arg string) (*discordgo.User, *discordgo.Channel) {
match := findNamedMatches(mentionRegex, arg)
id := match["ID"]
if id == "" {
return nil, nil
}
// Sends a blocking API request
switch match["Type"] {
case "#":
{
// Try to get from the "State" cache before falling back to the API
channel, err := discord.State.Channel(id)
if err != nil || channel == nil {
channel, err = discord.Channel(id)
}
if err == nil {
return nil, channel
}
}
case "@":
{
// discordgo doesn't cache users, only guilds, channels & members
user, err := discord.User(id)
if err == nil {
return user, nil
}
}
}
return nil, nil
}
// Gets the mute role associated with the given channel
// Returns an error if no matching role exists
func getMuteRoleForChannel(channel *discordgo.Channel) (role string, err error) {
if channel == nil {
role = muteRoles[""]
return
}
var ok bool // Avoid shadowing role with :=
if role, ok = muteRoles[channel.ID]; !ok {
err = fmt.Errorf("unable to find mute role for channel %s", channel.Mention())
}
return
}
func muteHandler(caller *discordgo.Member, msg *discordgo.Message, args []string) error {
user, channel, remainingArgs := getUserAndChannelAndArgs(args[1:])
if user == nil {
return errors.New("First argument should mention user or channel")
}
if len(remainingArgs) < 1 {
return errors.New("Give a reason")
}
target, err := GetMember(user.ID)
if err != nil {
return err
}
if !outranks(caller, target) {
return fmt.Errorf("You don't outrank %s", target.User.Username)
}
// Support can tempmute, but only on users without roles
if strings.ToLower(args[0]) == "tempmute" {
if IsUserLowerThan(caller, Moderator) && !evalRatelimit(msg.Author.ID) {
return errors.New("Too soon")
}
if IsUserLowerThan(caller, Moderator) {
trustedRoles := append(RolesToIDs(staffRoles)) // TODO calculate this only once?
for _, role := range target.Roles {
if includes(trustedRoles, role) {
return errors.New("They have trusted role(s)")
}
}
}
}
muteRole, err := getMuteRoleForChannel(channel)
if err != nil {
return fmt.Errorf("Can't mute from %s yet", channel.Mention())
}
// Reasons are important
var reason strings.Builder
reason.WriteString(args[0])
reason.WriteString(" has been issued to " + user.Username)
if channel != nil {
reason.WriteString(" from channel " + channel.Mention())
}
reason.WriteString(" by @" + msg.Author.Username + "#" + msg.Author.Discriminator)
reason.WriteString(" for reason: " + strings.Join(remainingArgs, " "))
providedReason := reason.String()
// Direct message the user being muted
DM, err := discord.UserChannelCreate(user.ID) // only creates it if it doesn"t already exist
if err == nil {
// if there is an error DMing them, we still want to ban them, they just won't know why
err = resp(DM.ID, providedReason)
if err != nil {
fmt.Printf("Error direct messaging %s#%s: %s\n", user.Username, user.Discriminator, err.Error())
}
}
if DB == nil {
return errors.New("I have no database, so I cannot ")
}
// Row values
var (
id uuid.UUID
userId = user.ID
channelId sql.NullString
)
if channel != nil {
channelId = sql.NullString{
String: channel.ID,
Valid: true,
}
}
var expiration *time.Time
if strings.ToLower(args[0]) == "tempmute" {
exp := time.Now().Add(TempmuteDuration) // go doesn't let you do this in one line
expiration = &exp
}
// Check if we have a matching mute already
err = DB.QueryRow("SELECT id from mutes WHERE discord_id=$1 AND (channel_id=$2 OR ($2 IS NULL AND channel_id IS NULL))", userId, channelId).Scan(&id)
if err == nil {
// Update existing entry, but only if it was already a tempmute. Don't downgrade a mute to a tempmute.
_, err = DB.Exec("UPDATE mutes SET expiration=$2 WHERE id=$1 AND expiration IS NOT NULL", id, expiration)
if err != nil {
return err
}
} else if errors.Is(err, sql.ErrNoRows) {
// Insert new entry
_, err = DB.Exec("INSERT INTO mutes (discord_id, channel_id, expiration) VALUES ($1, $2, $3)", userId, channelId, expiration)
if err != nil {
return err
}
} else {
// Unknown error
return err
}
err = discord.GuildMemberRoleAdd(msg.GuildID, user.ID, muteRole)
if err != nil {
return err
}
_ = resp(impactBotLog, providedReason)
_ = resp(msg.ChannelID, providedReason)
return nil
}
func unmuteHandler(caller *discordgo.Member, msg *discordgo.Message, args []string) (err error) {
var unmuteAll bool
user, channel, remainingArgs := getUserAndChannelAndArgs(args[1:])
if user == nil {
return errors.New("First argument should mention user")
}
if channel == nil && len(remainingArgs) == 1 && "all" == strings.ToLower(remainingArgs[0]) {
unmuteAll = true
} else if len(remainingArgs) > 0 {
return fmt.Errorf("Unexpected arguments \"%s\"", strings.Join(remainingArgs, " "))
}
var target *discordgo.Member
target, err = GetMember(user.ID)
if err != nil {
return
}
if outranks(target, caller) {
return fmt.Errorf("You must be at least the same rank as %s", target.User.Username)
}
// produce a list of muted channels for the command output
var fullMute bool
var channels []string
if unmuteAll {
// Remove all mutes from DB
_, err = DB.Exec("DELETE FROM mutes WHERE discord_id = $1", user.ID)
if err != nil {
return err
}
// Remove all mute roles
var count uint
for mutedChannel, muteRole := range muteRoles {
if hasRole(target, Role{ID: muteRole}) {
// Keep track of what was unmuted for the reply
count++
if mutedChannel == "" {
fullMute = true
} else {
channels = append(channels, mutedChannel)
}
// Remove the role
err = discord.GuildMemberRoleRemove(msg.GuildID, user.ID, muteRole)
}
}
// We didn't actually unmute anything!
if count < 1 {
return fmt.Errorf("No mutes found for @%s#%s", user.Username, user.Discriminator)
}
} else {
// unmute specified channel (or server-wide for nil)
muteRole, err := getMuteRoleForChannel(channel)
if err != nil {
if channel != nil {
return fmt.Errorf("Can't unmute from %s yet", channel.Mention())
} else {
return err // Unknown error
}
}
// Check the user is actually muted...
if !hasRole(target, Role{ID: muteRole}) {
if channel == nil {
return fmt.Errorf("%s isn't muted serverwide", user.Username)
} else {
return fmt.Errorf("%s isn't muted in %s", user.Username, channel.Mention())
}
}
// Update the database and keep track of what was unmuted (for the reply)
if channel == nil {
fullMute = true
_, err = DB.Exec("DELETE FROM mutes WHERE discord_id = $1 AND channel_id IS NULL", user.ID)
if err != nil {
return err
}
} else {
channels = append(channels, channel.ID)
_, err = DB.Exec("DELETE FROM mutes WHERE discord_id = $1 AND channel_id = $2", user.ID, channel.ID)
if err != nil {
return err
}
}
// Unmute them
err = discord.GuildMemberRoleRemove(msg.GuildID, user.ID, muteRole)
}
// Construct a reply out of the unmuted channels slice
var reply strings.Builder
reply.WriteString("User " + user.Username + " has been ")
if fullMute {
reply.WriteString("unmuted serverwide")
if len(channels) > 0 {
reply.WriteString(" and ")
}
}
if len(channels) > 0 {
reply.WriteString("unmuted from ")
for index, id := range channels {
if index > 0 {
reply.WriteString(", ")
if index == len(channels)-1 {
reply.WriteString("& ")
}
}
reply.WriteString(fmt.Sprintf("<#%s>", id))
}
}
reply.WriteString(" by @" + msg.Author.Username + "#" + msg.Author.Discriminator)
reply.WriteString("\n")
// Direct message the user being unmuted
DM, err := discord.UserChannelCreate(user.ID)
if err == nil {
err = resp(DM.ID, reply.String())
if err != nil {
s := fmt.Sprintf("Error direct messaging %s#%s: %s", user.Username, user.Discriminator, err.Error())
fmt.Println(s)
reply.WriteString(s + "\n")
}
}
// Respond in chat & #impactbot-log
_ = resp(impactBotLog, reply.String())
_ = resp(msg.ChannelID, reply.String())
return nil
}
// tbh should this be separate handlers??
// or maybe multiple handlers here is stupid, this is mostly a copy of mute handler :\
func rektHandler(caller *discordgo.Member, msg *discordgo.Message, args []string) error {
user, channel, remainingArgs := getUserAndChannelAndArgs(args[1:])
if user == nil {
return errors.New("First argument should mention user")
}
if channel != nil {
return errors.New(args[0] + " does not support channel mentions")
}
if len(remainingArgs) < 1 {
return errors.New("Give a reason")
}
target, err := GetMember(user.ID)
if err != nil {
return err
}
if !outranks(caller, target) {
return fmt.Errorf("You don't outrank %s", target.User.Username)
}
// Reasons are important
providedReason := args[0] + " has been issued to " + user.Username + " by @" + msg.Author.Username + "#" + msg.Author.Discriminator + " for reason: " + strings.Join(remainingArgs, " ")
// Direct message the user being rekt
DM, err := discord.UserChannelCreate(user.ID) // only creates it if it doesn"t already exist
if err == nil {
// if there is an error DMing them, we still want to ban them, they just won't know why
err = resp(DM.ID, providedReason)
if err != nil {
fmt.Printf("Error direct messaging %s#%s: %s\n", user.Username, user.Discriminator, err.Error())
}
}
switch args[0] {
case "ban":
err = discord.GuildBanCreateWithReason(msg.GuildID, user.ID, providedReason, 0)
case "kick":
err = discord.GuildMemberDeleteWithReason(msg.GuildID, user.ID, providedReason)
}
if err != nil {
return err
}
_ = resp(impactBotLog, providedReason)
_ = resp(msg.ChannelID, providedReason)
return nil
}
// unmuteCallback is called every UNMUTE_INTERVAL to unmute any expired temp mutes
func unmuteCallback() {
if DB == nil {
return
}
// Get all expired rows
now := time.Now()
rows, err := DB.Query("SELECT id, discord_id, channel_id FROM mutes WHERE expiration < $1 AND expiration IS NOT NULL", now)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
fmt.Println("Error querying expired tempmutes", err)
}
return
}
for rows.Next() {
var (
id uuid.UUID
discordId string
channelId sql.NullString
)
err := rows.Scan(&id, &discordId, &channelId)
if err != nil {
fmt.Println("Error scanning tempmute entry", err)
continue
}
// We're handling the user, so delete from db.
// If we fail to unmute, at least we won't end up handling them forever
_, err = DB.Exec("DELETE FROM mutes WHERE id = $1", id)
if err != nil {
fmt.Println("Error deleting tempmute entry", err)
continue
}
// Get channel and mute role
var channel *discordgo.Channel
if channelId.Valid {
channel, err = discord.Channel(channelId.String)
if err != nil {
fmt.Println("Error getting tempmute channel from channel id "+channelId.String, err)
continue
}
}
muteRole, err := getMuteRoleForChannel(channel)
if err != nil {
fmt.Println(fmt.Sprintf("Invalid mute role for channel id \"%s\"\n", channelId.String), err)
continue
}
// Construct a message to the unmuted user
var message strings.Builder
message.WriteString(fmt.Sprintf("Your temp mute"))
if channel != nil {
message.WriteString(fmt.Sprintf(" from %s", channel.Mention()))
}
message.WriteString(" has expired!\n")
{ // Log the unmute
var username = "user"
if user, _ := discord.User(discordId); user != nil {
username = fmt.Sprintf("@%s#%s", user.Username, user.Discriminator)
}
if channel == nil {
fmt.Printf("Processing unmute for %s (%s) serverwide\n", username, discordId)
} else {
fmt.Printf("Processing unmute for %s (%s) from channel #%s (%s)\n", username, discordId, channel.Name, channel.ID)
}
}
// Do the unmute
err = discord.GuildMemberRoleRemove(impactServer, discordId, muteRole)
if err != nil {
fmt.Println("Could not remove mute role \""+muteRole+"\" from user \""+discordId+"\"", err)
message.WriteString("But the bot failed to unmute you! Please show this message to a moderator.\n")
}
// DM message to user
dm, err := discord.UserChannelCreate(discordId)
if err != nil {
continue // guess we can't let em know
}
_ = resp(dm.ID, message.String())
}
}
func init() {
if DB == nil {
fmt.Println("WARNING: No DB when initialising tempmutes callback, either rekt.go was initialised before db.go or there is no DB")
}
go func() {
ticker := time.NewTicker(UnmuteInterval)
for range ticker.C {
unmuteCallback()
}
}()
}
func onUserJoin2(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
if m.GuildID != impactServer || m.User == nil {
return
}
if DB == nil {
return
}
// Get all nonexpired rows
now := time.Now()
rows, err := DB.Query("SELECT channel_id FROM mutes WHERE (expiration IS NULL OR expiration > $1) AND discord_id = $2", now, m.User.ID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
fmt.Println("Error querying expired tempmutes", err)
}
return
}
for rows.Next() {
var (
channelId sql.NullString
)
err := rows.Scan(&channelId)
if err != nil {
fmt.Println("Error scanning tempmute entry", err)
continue
}
// Get channel and mute role
var channel *discordgo.Channel
if channelId.Valid {
channel, err = discord.Channel(channelId.String)
if err != nil {
fmt.Println("Error getting tempmute channel from channel id "+channelId.String, err)
continue
}
}
muteRole, err := getMuteRoleForChannel(channel)
if err != nil {
fmt.Println(fmt.Sprintf("Invalid mute role for channel id \"%s\"\n", channelId.String), err)
continue
}
// Do the unmute
err = discord.GuildMemberRoleAdd(impactServer, m.User.ID, muteRole)
if err != nil {
fmt.Println("Could not remove mute role \""+muteRole+"\" from user \""+m.User.ID+"\"", err)
}
}
}