-
Notifications
You must be signed in to change notification settings - Fork 13
/
gomumblesoundboard.go
90 lines (82 loc) · 2.29 KB
/
gomumblesoundboard.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/go-martini/martini"
"github.com/layeh/gumble/gumble"
"github.com/layeh/gumble/gumble_ffmpeg"
"github.com/layeh/gumble/gumbleutil"
)
func main() {
files := make(map[string]string)
var stream *gumble_ffmpeg.Stream
targetChannel := flag.String("channel", "Root", "channel the bot will join")
gumbleutil.Main(func(_ *gumble.Config, client *gumble.Client) {
var err error
stream, err = gumble_ffmpeg.New(client)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
for _, file := range flag.Args() {
key := filepath.Base(file)
files[key] = file
}
}, gumbleutil.Listener{
// Connect event
Connect: func(e *gumble.ConnectEvent) {
fmt.Printf("GoMumbleSoundboard loaded (%d files)\n", len(files))
fmt.Printf("Connected to %s\n", e.Client.Conn().RemoteAddr())
if e.WelcomeMessage != "" {
fmt.Printf("Welcome message: %s\n", e.WelcomeMessage)
}
fmt.Printf("Current Channel: %s\n", e.Client.Self().Channel().Name())
if *targetChannel != "" && e.Client.Self().Channel().Name() != *targetChannel {
channelPath := strings.Split(*targetChannel, "/")
target := e.Client.Self().Channel().Find(channelPath...)
if target == nil {
fmt.Printf("Cannot find channel named %s\n", *targetChannel)
os.Exit(1)
}
e.Client.Self().Move(target)
fmt.Printf("Moved to: %s\n", target.Name())
}
// Start webserver
m := martini.Classic()
// martini.Static() is used, so public/index.html gets automagically served
m.Get("/files.json", func() string {
keys := make([]string, 0, len(files))
for k := range files {
keys = append(keys, k)
}
// Sort keys into alphabetical order. Sick of things moving around
ss := sort.StringSlice(keys)
ss.Sort()
js, _ := json.Marshal(ss)
return string(js)
})
m.Get("/play/:file", func(params martini.Params) (int, string) {
file, ok := files[params["file"]]
if !ok {
return 404, "not found"
}
stream.Stop()
if err := stream.Play(file); err != nil {
return 400, fmt.Sprintf("%s\n", err)
} else {
return 200, fmt.Sprintf("Playing %s\n", file)
}
})
m.Get("/stop", func() string {
stream.Stop()
return "ok"
})
m.Run()
},
})
}