forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autorole.go
68 lines (55 loc) · 1.54 KB
/
autorole.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
package autorole
import (
"encoding/json"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/sirupsen/logrus"
"strconv"
)
func KeyGeneral(guildID int64) string { return "autorole:" + discordgo.StrID(guildID) + ":general" }
func KeyProcessing(guildID int64) string {
return "autorole:" + discordgo.StrID(guildID) + ":processing"
}
type Plugin struct{}
func (p *Plugin) Name() string {
return "Autorole"
}
func RegisterPlugin() {
p := &Plugin{}
common.RegisterPlugin(p)
}
type GeneralConfig struct {
Role int64 `json:",string" valid:"role,true"`
RequiredDuration int
RequiredRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
OnlyOnJoin bool
}
type LegacyGeneralConfig struct {
Role string `valid:"role,true"`
RequiredDuration int
RequiredRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
OnlyOnJoin bool
}
func (l *GeneralConfig) UnmarshalJSON(b []byte) error {
var tmp LegacyGeneralConfig
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
l.Role, _ = strconv.ParseInt(tmp.Role, 10, 64)
l.RequiredDuration = tmp.RequiredDuration
l.RequiredRoles = tmp.RequiredRoles
l.IgnoreRoles = tmp.IgnoreRoles
l.OnlyOnJoin = tmp.OnlyOnJoin
return nil
}
func GetGeneralConfig(guildID int64) (*GeneralConfig, error) {
conf := &GeneralConfig{}
err := common.GetRedisJson(KeyGeneral(guildID), conf)
if err != nil {
logrus.WithError(err).WithField("guild", guildID).Error("failed retreiving autorole config")
}
return conf, err
}