forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
174 lines (137 loc) · 4.57 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
package automod_legacy
import (
"time"
"github.com/botlabs-gg/yagpdb/v2/analytics"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/bot/eventsystem"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/pubsub"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
"github.com/botlabs-gg/yagpdb/v2/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(evt *eventsystem.EventData, msg *discordgo.Message) bool {
return !CheckMessage(evt, msg)
}
func HandleMessageUpdate(evt *eventsystem.EventData) {
CheckMessage(evt, evt.MessageUpdate().Message)
}
func CheckMessage(evt *eventsystem.EventData, m *discordgo.Message) bool {
if !bot.IsNormalUserMessage(m) {
return false
}
if m.Author.ID == common.BotUser.ID || m.Author.Bot || m.GuildID == 0 {
return false // Pls no panicerinos or banerinos self
}
if !evt.HasFeatureFlag(featureFlagEnabled) {
return false
}
cs := evt.GS.GetChannelOrThread(m.ChannelID)
if cs == nil {
logger.WithField("channel", m.ChannelID).Error("Channel not found in state")
return false
}
config, err := CachedGetConfig(cs.GuildID)
if err != nil {
logger.WithError(err).Error("Failed retrieving config")
return false
}
if !config.Enabled {
return false
}
member := dstate.MemberStateFromMember(m.Member)
member.GuildID = m.GuildID
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}
didCheck := false
// We gonna need to have this locked while we check
for _, r := range rules {
if r.ShouldIgnore(cs, m, member) {
continue
}
didCheck = true
d, punishment, msg, err := r.Check(m, cs)
if d {
del = true
}
if err != nil {
logger.WithError(err).WithField("guild", cs.GuildID).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 {
if didCheck {
go analytics.RecordActiveUnit(cs.GuildID, &Plugin{}, "checked")
}
return false
}
go analytics.RecordActiveUnit(cs.GuildID, &Plugin{}, "rule_triggered")
if punishMsg != "" {
// Strip last newline
punishMsg = punishMsg[:len(punishMsg)-1]
}
go func() {
switch highestPunish {
case PunishNone:
err = moderation.WarnUser(nil, cs.GuildID, cs, m, common.BotUser, &member.User, "Automoderator: "+punishMsg)
case PunishMute:
err = moderation.MuteUnmuteUser(nil, true, cs.GuildID, cs, m, common.BotUser, "Automoderator: "+punishMsg, member, muteDuration)
case PunishKick:
err = moderation.KickUser(nil, cs.GuildID, cs, m, common.BotUser, "Automoderator: "+punishMsg, &member.User, -1)
case PunishBan:
err = moderation.BanUser(nil, cs.GuildID, cs, m, common.BotUser, "Automoderator: "+punishMsg, &member.User)
}
// 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
}