-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoderation.go
248 lines (215 loc) · 7.12 KB
/
moderation.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
package moderation
import (
"fmt"
"github.com/Clinet/clinet_cmds"
"github.com/Clinet/clinet_features"
"github.com/Clinet/clinet_services"
"github.com/Clinet/clinet_storage"
"github.com/JoshuaDoes/logger"
)
var Log *logger.Logger
var Feature = features.Feature{
Name: "moderation",
Desc: "Allows easy-going and simple control over a community.",
Cmds: []*cmds.Cmd{
cmds.NewCmd("ban", "Bans a given user", handleBan).AddArgs(
cmds.NewCmdArg("user", "Who to actually ban", cmds.ArgTypeUser).SetRequired(),
cmds.NewCmdArg("reason", "Reason for the ban", "No reason provided."),
cmds.NewCmdArg("rule", "Rule broken that led to ban", 0),
),
cmds.NewCmd("hackban", "Hackbans a given user ID", handleBan).AddArgs(
cmds.NewCmdArg("user", "ID of the user to ban", "").SetRequired(),
cmds.NewCmdArg("reason", "Reason for the ban", "No reason provided."),
cmds.NewCmdArg("rule", "Rule broken that led to ban", 0),
),
cmds.NewCmd("kick", "Kicks a given user", handleKick).AddArgs(
cmds.NewCmdArg("user", "Who to actually kick", cmds.ArgTypeUser).SetRequired(),
cmds.NewCmdArg("reason", "Reason for the kick", "No reason provided."),
cmds.NewCmdArg("rule", "Rule broken that led to kick", 0),
),
cmds.NewCmd("warn", "Warns a given user", handleWarn).AddArgs(
cmds.NewCmdArg("user", "Who to actually warn", cmds.ArgTypeUser).SetRequired(),
cmds.NewCmdArg("reason", "Reason for the warning", "No reason provided."),
cmds.NewCmdArg("rule", "Rule broken that led to warning", 0),
),
},
Init: Init,
}
func init() {
Log = logger.NewLogger("moderation", 2)
}
//Needed for the cmds framework
var Cmds []*cmds.Cmd
var Storage *storage.Storage
func Init() error {
Storage = &storage.Storage{}
if err := Storage.LoadFrom("moderation"); err != nil {
Log.Error(err)
return err
}
return nil
}
func handleBan(ctx *cmds.CmdCtx) *cmds.CmdResp {
ctxPerms, err := ctx.Service.GetUserPerms(ctx.Server.ServerID, ctx.Channel.ChannelID, ctx.User.UserID)
if err != nil || !ctxPerms.CanBan() {
msgErr := services.NewMessage().
SetContent("You're not allowed to ban anyone!").
SetColor(0xFF0000)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
user := ctx.GetArg("user").GetUser()
user.ServerID = ctx.Server.ServerID //Fill it in for hackbans
reason := ctx.GetArg("reason").GetString()
rule := ctx.GetArg("rule").GetInt()
ban := "You've been banned"
if rule > 0 {
ban += " for breaking rule " + fmt.Sprintf("%d", rule)
}
ban += "."
if reason != "" {
ban += " The following reason was given: " + reason
}
msgBan := services.NewMessage().
SetContent(ban).
SetColor(0xFF0000)
server, err := ctx.Service.GetServer(ctx.Server.ServerID)
if err != nil {
Log.Error(err)
} else {
msgBan.SetTitle(server.Name)
}
//Ship off the DM
msgBan.ChannelID = user.UserID
if _, err = ctx.Service.MsgSend(msgBan, nil); err != nil {
Log.Error(err)
}
if err := ctx.Service.UserBan(user, reason, rule); err != nil {
Log.Error(err)
msgErr := services.NewMessage().
SetContent("Something went wrong while trying to ban that user...").
SetColor(0xFF0000)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
msg := services.NewMessage().
SetContent("You banished them to the shadow realm!").
SetColor(0x1C1C1C)
return cmds.CmdRespFromMsg(msg).SetReady(true)
}
func handleKick(ctx *cmds.CmdCtx) *cmds.CmdResp {
ctxPerms, err := ctx.Service.GetUserPerms(ctx.Server.ServerID, ctx.Channel.ChannelID, ctx.User.UserID)
if err != nil || !ctxPerms.CanKick() {
msgErr := services.NewMessage().
SetContent("You're not allowed to kick anyone!").
SetColor(0xFF0000)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
user := ctx.GetArg("user").GetUser()
reason := ctx.GetArg("reason").GetString()
rule := ctx.GetArg("rule").GetInt()
kick := "You've been kicked"
if rule > 0 {
kick += " for breaking rule " + fmt.Sprintf("%d", rule)
}
kick += "."
if reason != "" {
kick += " The following reason was given: " + reason
}
msgKick := services.NewMessage().
SetContent(kick).
SetColor(0xCCCC09) //Dirty yellow?
server, err := ctx.Service.GetServer(ctx.Server.ServerID)
if err != nil {
Log.Error(err)
} else {
msgKick.SetTitle(server.Name)
}
//Ship off the DM
msgKick.ChannelID = user.UserID
if _, err = ctx.Service.MsgSend(msgKick, nil); err != nil {
Log.Error(err)
}
if err := ctx.Service.UserKick(user, reason, rule); err != nil {
Log.Error(err)
msgErr := services.NewMessage().
SetContent("Something went wrong while trying to kick that user...").
SetColor(0xFF0000)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
msg := services.NewMessage().
SetContent("You kicked them over the gates!").
SetColor(0x1C1C1C)
return cmds.CmdRespFromMsg(msg).SetReady(true)
}
type Warning struct {
Reason string
Rule int
}
func handleWarn(ctx *cmds.CmdCtx) *cmds.CmdResp {
ctxPerms, err := ctx.Service.GetUserPerms(ctx.Server.ServerID, ctx.Channel.ChannelID, ctx.User.UserID)
//Only users with kick or ban perms can warn someone, with the side effect of automatic ban/kick failing if the warner can't do so
if err != nil || !ctxPerms.CanKick() || !ctxPerms.CanBan() {
msgErr := services.NewMessage().
SetContent("You're not allowed to warn anyone!").
SetColor(0xFF0000)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
user := ctx.GetArg("user").GetUser()
reason := ctx.GetArg("reason").GetString()
rule := ctx.GetArg("rule").GetInt()
warnings := make([]*Warning, 0)
rawWarnings, err := Storage.UserGet(user.UserID, "warnings")
if err == nil {
warnings = rawWarnings.([]*Warning)
}
warnings = append(warnings, &Warning{Reason: reason, Rule: rule})
Storage.UserSet(user.UserID, "warnings", warnings)
warnLimit := 3
rawWarnLimit, err := Storage.ServerGet(ctx.Server.ServerID, "warnLimit")
if err != nil {
Storage.ServerSet(ctx.Server.ServerID, "warnLimit", warnLimit)
} else {
warnLimit = rawWarnLimit.(int)
}
if len(warnings) >= warnLimit {
shouldBan := false
rawShouldBan, err := Storage.ServerGet(ctx.Server.ServerID, "warnLimitShouldBan")
if err != nil {
Storage.ServerSet(ctx.Server.ServerID, "warnLimitShouldBan", shouldBan)
} else {
shouldBan = rawShouldBan.(bool)
}
//Conveniently, commands may simply call each other now
if shouldBan {
return handleBan(ctx)
}
return handleKick(ctx)
} else {
warning := "You've been warned"
if rule > 0 {
warning += " for breaking rule " + fmt.Sprintf("%d", rule)
}
warning += "."
if reason != "" {
warning += " The following reason was given: " + reason
}
msgWarning := services.NewMessage().
SetContent(warning).
SetColor(0xCCCC09) //Dirty yellow?
server, err := ctx.Service.GetServer(ctx.Server.ServerID)
if err != nil {
Log.Error(err)
} else {
msgWarning.SetTitle(server.Name)
}
//Ship off the DM
msgWarning.ChannelID = user.UserID
if _, err = ctx.Service.MsgSend(msgWarning, nil); err != nil {
Log.Error(err)
msgErr := services.NewMessage().
SetContent("Something went wrong while trying to DM the warning, but it applied anyway.").
SetColor(0xCCCC09)
return cmds.CmdRespFromMsg(msgErr).SetReady(true)
}
}
return cmds.NewCmdRespMsg("The user has been warned!")
}