-
Notifications
You must be signed in to change notification settings - Fork 927
/
streaming.go
101 lines (81 loc) · 3.06 KB
/
streaming.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
package streaming
import (
"encoding/json"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix.v2/redis"
"strconv"
)
type Plugin struct{}
func (p *Plugin) Name() string {
return "Streaming"
}
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
}
type Config struct {
Enabled bool `json:"enabled" schema:"enabled"` // Wether streaming notifications is enabled or not
// Give a role to people streaming
GiveRole int64 `json:"give_role,string" schema:"give_role" valid:"role,true"`
// Ignores people with this role, requirerole is ignored if this is set
IgnoreRole int64 `json:"ban_role,string" schema:"ignore_role" valid:"role,true"`
// Requires people to have this role
RequireRole int64 `json:"require_role,string" schema:"require_role" valid:"role,true"`
// Channel to send streaming announcements in
AnnounceChannel int64 `json:"announce_channel,string" schema:"announce_channel" valid:"channel,true"`
// The message
AnnounceMessage string `json:"announce_message" schema:"announce_message" valid:"template,2000"`
}
type LegacyConfig struct {
Enabled bool `json:"enabled" schema:"enabled"` // Wether streaming notifications is enabled or not
// Give a role to people streaming
GiveRole string `json:"give_role" schema:"give_role" valid:"role,true"`
// Ignores people with this role, requirerole is ignored if this is set
IgnoreRole string `json:"ban_role" schema:"ignore_role" valid:"role,true"`
// Requires people to have this role
RequireRole string `json:"require_role" schema:"require_role" valid:"role,true"`
// Channel to send streaming announcements in
AnnounceChannel string `json:"announce_channel" schema:"announce_channel" valid:"channel,true"`
// The message
AnnounceMessage string `json:"announce_message" schema:"announce_message" valid:"template,2000"`
}
func (c *Config) UnmarshalJSON(b []byte) error {
var tmp LegacyConfig
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
c.GiveRole, _ = strconv.ParseInt(tmp.GiveRole, 10, 64)
c.IgnoreRole, _ = strconv.ParseInt(tmp.IgnoreRole, 10, 64)
c.RequireRole, _ = strconv.ParseInt(tmp.RequireRole, 10, 64)
c.AnnounceChannel, _ = strconv.ParseInt(tmp.AnnounceChannel, 10, 64)
c.Enabled = tmp.Enabled
c.AnnounceMessage = tmp.AnnounceMessage
return nil
}
func (c *Config) Save(client *redis.Client, guildID int64) error {
encoded, err := json.Marshal(c)
if err != nil {
return err
}
return client.Cmd("SET", "streaming_config:"+discordgo.StrID(guildID), encoded).Err
}
var DefaultConfig = &Config{
Enabled: false,
AnnounceMessage: "OH WOWIE! **{{.User.Username}}** is currently streaming! Check it out: {{.URL}}",
}
// Returns he guild's conifg, or the defaul one if not set
func GetConfig(client *redis.Client, guildID int64) (*Config, error) {
reply := client.Cmd("GET", "streaming_config:"+discordgo.StrID(guildID))
if reply.IsType(redis.Nil) {
return DefaultConfig, nil
}
b, err := reply.Bytes()
if err != nil {
return nil, err
}
var config *Config
err = json.Unmarshal(b, &config)
return config, err
}