-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntiSpam.go
93 lines (71 loc) · 1.91 KB
/
AntiSpam.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
package main
import (
"fmt"
"time"
discord "github.com/bwmarrin/discordgo"
)
// handler which handles SPAM and others problem
type AntiSpam struct{}
const (
// time that handler reset the count of message
timeToReset = time.Millisecond * 3000
/*
the max message that the user can send at {TimeToRest},
if the user send more than the max message,
then the user get warned and can't send message
*/
maxMessageCount = 5
)
// DangerHandler constructor
func NewAntiSpam() *AntiSpam {
return &AntiSpam{}
}
// handling problem
func (_ AntiSpam) Handle(msg *discord.MessageCreate) {
var (
warningHandler *UserMap = GetUserMap()
bot *discord.Session = GetBot()
author *discord.User = msg.Author
id string = IdForUserMapCalculation(author.ID, msg.GuildID)
user *User
ok bool
)
user, ok = warningHandler.GetUserById(id)
if !ok {
var user *User = NewUser(author)
warningHandler.StoreUser(id, user)
// fmt.Printf("user storaged in the map %v", user)
return
}
user.AddMessageCount()
if user.IsWarned() {
// fmt.Printf("%v is warned\n", msg.Author.ID)
GetBot().ChannelMessageDelete(msg.ChannelID, msg.ID)
return
}
if user.GetMessageCount() == 1 {
go func() {
time.Sleep(timeToReset)
var count int32 = user.GetMessageCount()
// fmt.Printf("reseted with %v\n", count)
if count > maxMessageCount-1 {
user.Warn()
var messages_id []string
var messages []*discord.Message
var err error
messages, err = bot.ChannelMessages(msg.ChannelID, int(count), "", "", "")
if err != nil {
return
}
for i := range messages {
messages_id = append(messages_id, messages[i].ID)
}
bot.ChannelMessagesBulkDelete(msg.ChannelID, messages_id)
bot.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("%v you are warned", author.Mention()))
}
user.ResetMessageCount()
}()
return
}
// fmt.Println("user exist in data")
}