-
Notifications
You must be signed in to change notification settings - Fork 927
/
bot.go
180 lines (141 loc) · 4.4 KB
/
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
package automod_legacy
import (
"time"
"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/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/moderation"
"github.com/karlseguin/ccache"
)
var _ bot.BotInitHandler = (*Plugin)(nil)
var (
// cache configs because they are used often
confCache *ccache.Cache
)
func (p *Plugin) BotInit() {
commands.MessageFilterFuncs = append(commands.MessageFilterFuncs, CommandsMessageFilterFunc)
eventsystem.AddHandlerAsyncLastLegacy(p, HandleMessageUpdate, eventsystem.EventMessageUpdate)
pubsub.AddHandler("update_automod_legacy_rules", HandleUpdateAutomodRules, nil)
confCache = ccache.New(ccache.Configure().MaxSize(1000))
}
// Invalidate the cache when the rules have changed
func HandleUpdateAutomodRules(event *pubsub.Event) {
confCache.Delete(KeyConfig(event.TargetGuildInt))
}
// CachedGetConfig either retrieves from local application cache or redis
func CachedGetConfig(gID int64) (*Config, error) {
confItem, err := confCache.Fetch(KeyConfig(gID), time.Minute*5, func() (interface{}, error) {
c, err := GetConfig(gID)
if err != nil {
return nil, err
}
// Compile sites and words
c.Sites.GetCompiled()
c.Words.GetCompiled()
return c, nil
})
if err != nil {
return nil, err
}
return confItem.Value().(*Config), nil
}
func CommandsMessageFilterFunc(msg *discordgo.Message) bool {
return !CheckMessage(msg)
}
func HandleMessageUpdate(evt *eventsystem.EventData) {
CheckMessage(evt.MessageUpdate().Message)
}
func CheckMessage(m *discordgo.Message) bool {
if m.Author == nil || m.Author.ID == common.BotUser.ID {
return false // Pls no panicerinos or banerinos self
}
if m.Author.Bot || m.GuildID == 0 {
return false
}
cs := bot.State.Channel(true, m.ChannelID)
if cs == nil {
logger.WithField("channel", m.ChannelID).Error("Channel not found in state")
return false
}
if cs.IsPrivate {
return false
}
config, err := CachedGetConfig(cs.Guild.ID)
if err != nil {
logger.WithError(err).Error("Failed retrieving config")
return false
}
if !config.Enabled {
return false
}
member, err := bot.GetMember(cs.Guild.ID, m.Author.ID)
if err != nil {
logger.WithError(err).WithField("guild", cs.Guild.ID).Warn("Member not found in state, automod ignoring")
return false
}
locked := true
cs.Owner.RLock()
defer func() {
if locked {
cs.Owner.RUnlock()
}
}()
del := false // Set if a rule triggered a message delete
punishMsg := ""
highestPunish := PunishNone
muteDuration := 0
rules := []Rule{config.Spam, config.Invite, config.Mention, config.Links, config.Words, config.Sites}
// We gonna need to have this locked while we check
for _, r := range rules {
if r.ShouldIgnore(m, member) {
continue
}
d, punishment, msg, err := r.Check(m, cs)
if d {
del = true
}
if err != nil {
logger.WithError(err).WithField("guild", cs.Guild.ID).Error("Failed checking aumod rule:", err)
continue
}
// If the rule did not trigger a deletion there wasn't any violation
if !d {
continue
}
punishMsg += msg + "\n"
if punishment > highestPunish {
highestPunish = punishment
muteDuration = r.GetMuteDuration()
}
}
if !del {
return false
}
if punishMsg != "" {
// Strip last newline
punishMsg = punishMsg[:len(punishMsg)-1]
}
cs.Owner.RUnlock()
locked = false
go func() {
switch highestPunish {
case PunishNone:
err = moderation.WarnUser(nil, cs.Guild.ID, cs.ID, common.BotUser, member.DGoUser(), "Automoderator: "+punishMsg)
case PunishMute:
err = moderation.MuteUnmuteUser(nil, true, cs.Guild.ID, cs.ID, common.BotUser, "Automoderator: "+punishMsg, member, muteDuration)
case PunishKick:
err = moderation.KickUser(nil, cs.Guild.ID, cs.ID, common.BotUser, "Automoderator: "+punishMsg, member.DGoUser())
case PunishBan:
err = moderation.BanUser(nil, cs.Guild.ID, cs.ID, common.BotUser, "Automoderator: "+punishMsg, member.DGoUser())
}
// Execute the punishment before removing the message to make sure it's included in logs
common.BotSession.ChannelMessageDelete(m.ChannelID, m.ID)
if err != nil && err != moderation.ErrNoMuteRole && !common.IsDiscordErr(err, discordgo.ErrCodeMissingPermissions, discordgo.ErrCodeMissingAccess) {
logger.WithError(err).Error("Error carrying out punishment")
}
}()
return true
}