forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 5
/
moderation.go
138 lines (109 loc) · 3.47 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
package moderation
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/configstore"
"github.com/mediocregopher/radix.v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"strconv"
"strings"
)
const (
ActionMuted = "Muted"
ActionUnMuted = "Unmuted"
ActionKicked = "Kicked"
ActionBanned = "Banned"
ActionUnbanned = "Unbanned"
ActionWarned = "Warned"
)
type Plugin struct{}
func (p *Plugin) Name() string {
return "Moderation"
}
func RedisKeyMutedUser(guildID, userID int64) string {
return "moderation_muted_user:" + discordgo.StrID(guildID) + ":" + discordgo.StrID(userID)
}
func RedisKeyBannedUser(guildID, userID int64) string {
return "moderation_banned_user:" + discordgo.StrID(guildID) + ":" + discordgo.StrID(userID)
}
func RedisKeyUnbannedUser(guildID, userID int64) string {
return "moderation_unbanned_user:" + discordgo.StrID(guildID) + ":" + discordgo.StrID(userID)
}
func RedisKeyLockedMute(guildID, userID int64) string {
return "moderation_updating_mute:" + discordgo.StrID(guildID) + ":" + discordgo.StrID(userID)
}
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
configstore.RegisterConfig(configstore.SQL, &Config{})
common.GORM.AutoMigrate(&Config{}, &WarningModel{}, &MuteModel{})
}
func getConfigIfNotSet(guildID int64, config *Config) (*Config, error) {
if config == nil {
var err error
config, err = GetConfig(guildID)
if err != nil {
return nil, err
}
}
return config, nil
}
func GetConfig(guildID int64) (*Config, error) {
var config Config
err := configstore.Cached.GetGuildConfig(context.Background(), guildID, &config)
if err == configstore.ErrNotFound {
err = nil
}
return &config, err
}
func handleUnMute(data string) error {
split := strings.Split(data, ":")
if len(split) < 2 {
logrus.Error("Invalid unmute event", data)
return nil // Can't re-schedule an invalid event..
}
guildID, _ := strconv.ParseInt(split[0], 10, 64)
userID, _ := strconv.ParseInt(split[1], 10, 64)
member, err := bot.GetMember(guildID, userID)
if err != nil {
err = errors.Cause(err)
if cast, ok := err.(*discordgo.RESTError); ok && cast.Message != nil {
return nil // Discord api ok, something else went wrong (like the user not being on the server). do not reschedule
}
if err == bot.ErrGuildNotFound {
return nil
}
return err
}
err = MuteUnmuteUser(nil, false, guildID, 0, common.BotUser, "Mute Duration Expired", member, 0)
if errors.Cause(err) != ErrNoMuteRole {
if cast, ok := errors.Cause(err).(*discordgo.RESTError); ok && cast.Message != nil {
return nil // Discord api ok, something else went wrong. do not reschedule
}
return err
}
return nil
}
func handleUnban(data string) error {
split := strings.Split(data, ":")
if len(split) < 2 {
logrus.Error("Invalid unban event", data)
return nil // Can't re-schedule an invalid event..
}
guildID, _ := strconv.ParseInt(split[0], 10, 64)
userID, _ := strconv.ParseInt(split[1], 10, 64)
g := bot.State.Guild(true, guildID)
if g == nil {
logrus.WithField("guild", guildID).Error("Unban scheduled for guild now in state")
return nil
}
common.RedisPool.Do(radix.FlatCmd(nil, "SETEX", RedisKeyUnbannedUser(guildID, userID), 30, 1))
err := common.BotSession.GuildBanDelete(guildID, userID)
if err != nil {
logrus.WithField("guild", guildID).WithError(err).Error("Failed unbanning user")
}
return nil
}