-
Notifications
You must be signed in to change notification settings - Fork 927
/
automod.go
176 lines (137 loc) · 3.59 KB
/
automod.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
package automod
import (
"encoding/json"
"strconv"
"strings"
"unicode"
"github.com/jonas747/yagpdb/automod/models"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/premium"
"github.com/karlseguin/ccache"
)
//go:generate sqlboiler --no-hooks psql
var (
RegexCache *ccache.Cache
logger = common.GetPluginLogger(&Plugin{})
)
type Plugin struct {
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Automod v2",
SysName: "automod_v2",
Category: common.PluginCategoryModeration,
}
}
func RegisterPlugin() {
RegexCache = ccache.New(ccache.Configure())
common.InitSchemas("automod_v2", DBSchemas...)
p := &Plugin{}
common.RegisterPlugin(p)
}
type ErrUnknownTypeID struct {
TypeID int
}
func (e *ErrUnknownTypeID) Error() string {
return "Unknown TypeID: " + strconv.Itoa(e.TypeID)
}
func ParseRulePartData(model *models.AutomodRuleDatum) (interface{}, error) {
part, ok := RulePartMap[model.TypeID]
if !ok {
return nil, &ErrUnknownTypeID{model.TypeID}
}
settingsDestination := part.DataType()
if settingsDestination == nil {
// No user settings for this part
return nil, nil
}
err := json.Unmarshal(model.Settings, settingsDestination)
return settingsDestination, err
}
func ParseAllRulePartData(dataModels []*models.AutomodRuleDatum) ([]interface{}, error) {
dst := make([]interface{}, len(dataModels))
for i, v := range dataModels {
parsed, err := ParseRulePartData(v)
if err != nil {
return nil, err
}
dst[i] = parsed
}
return dst, nil
}
const (
MaxMessageTriggers = 20
MaxMessageTriggersPremium = 100
MaxViolationTriggers = 20
MaxViolationTriggersPremium = 100
MaxTotalRules = 25
MaxTotalRulesPremium = 150
MaxLists = 5
MaxListsPremium = 25
MaxRuleParts = 25
MaxRulesets = 10
MaxRulesetsPremium = 25
)
func GuildMaxMessageTriggers(guildID int64) int {
if isPremium, _ := premium.IsGuildPremium(guildID); isPremium {
return MaxMessageTriggersPremium
}
return MaxMessageTriggers
}
func GuildMaxViolationTriggers(guildID int64) int {
if isPremium, _ := premium.IsGuildPremium(guildID); isPremium {
return MaxViolationTriggersPremium
}
return MaxViolationTriggers
}
func GuildMaxTotalRules(guildID int64) int {
if isPremium, _ := premium.IsGuildPremium(guildID); isPremium {
return MaxTotalRulesPremium
}
return MaxTotalRules
}
func GuildMaxLists(guildID int64) int {
if isPremium, _ := premium.IsGuildPremium(guildID); isPremium {
return MaxListsPremium
}
return MaxLists
}
func GuildMaxRulesets(guildID int64) int {
if isPremium, _ := premium.IsGuildPremium(guildID); isPremium {
return MaxRulesetsPremium
}
return MaxRulesets
}
func PrepareMessageForWordCheck(input string) string {
var out strings.Builder
split := strings.Fields(input)
for i, w := range split {
if i != 0 {
out.WriteRune(' ')
}
// make 2 variants, 1 with all occurences replaced with space and 1 with all the occurences just removed
// this i imagine will solve a low of cases
w1 := ""
w2 := ""
for _, r := range w {
// we replace them with spaces instead to make for a more accurate version
// e.g "word1*word2" will become "word1 word2" instead of "word1word2"
if unicode.IsPunct(r) || unicode.IsSymbol(r) {
// replace with spaces for w1, and just remove for w2
w1 += " "
} else {
w1 += string(r)
w2 += string(r)
}
}
out.WriteString(w1)
if w1 != w2 && w1 != w {
out.WriteString(" " + w2 + " " + w)
} else if w1 != w2 {
out.WriteString(" " + w2)
} else if w1 != w {
out.WriteString(" " + w)
}
}
return out.String()
}