forked from iopred/bruxism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playingplugin.go
95 lines (76 loc) · 2.13 KB
/
playingplugin.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
package playingplugin
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/iopred/bruxism"
)
type playingPlugin struct {
bruxism.SimplePlugin
Game string
URL string
}
// Name returns the name of the plugin.
func (p *playingPlugin) Name() string {
return "Playing"
}
// Load will load plugin state from a byte array.
func (p *playingPlugin) Load(bot *bruxism.Bot, service bruxism.Service, data []byte) error {
if service.Name() != bruxism.DiscordServiceName {
panic("Playing Plugin only supports Discord.")
}
if data != nil {
if err := json.Unmarshal(data, p); err != nil {
log.Println("Error loading data", err)
}
}
if p.Game != "" {
service.(*bruxism.Discord).Session.UpdateStreamingStatus(0, p.Game, p.URL)
} else {
service.(*bruxism.Discord).Session.UpdateStatus(0, p.Game)
}
return nil
}
// Save will save plugin state to a byte array.
func (p *playingPlugin) Save() ([]byte, error) {
return json.Marshal(p)
}
// Help returns a list of help strings that are printed when the user requests them.
func (p *playingPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
if detailed {
return nil
}
if !service.IsBotOwner(message) {
return nil
}
return bruxism.CommandHelp(service, "playing", "<game>, <url>", fmt.Sprintf("Set which game %s is playing.", service.UserName()))
}
// Message handler.
func (p *playingPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
if service.IsMe(message) {
return
}
if !bruxism.MatchesCommand(service, "playing", message) {
return
}
if !service.IsBotOwner(message) {
return
}
query, _ := bruxism.ParseCommand(service, message)
split := strings.Split(query, ",")
p.Game = strings.Trim(split[0], " ")
if len(split) > 1 {
p.URL = strings.Trim(split[1], " ")
service.(*bruxism.Discord).Session.UpdateStreamingStatus(0, p.Game, p.URL)
} else {
service.(*bruxism.Discord).Session.UpdateStatus(0, p.Game)
}
}
// New will create a new top streamers plugin.
func New() bruxism.Plugin {
p := &playingPlugin{}
p.MessageFunc = p.messageFunc
p.HelpFunc = p.helpFunc
return p
}