forked from robbi5/gomumblesoundboard
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
149 lines (123 loc) · 3.18 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync"
_ "github.com/feuerrot/gomumblesoundboard/opus"
"layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleffmpeg"
"layeh.com/gumble/gumbleutil"
)
type Interaction struct {
Stop bool
Volume float32
File *File
}
var (
targetChannel = flag.String("channel", "Root", "channel the bot will join")
maxVolume = flag.String("maxvol", "100", "Set the maximum Volume in %, the volume set in the UI is multiplied with it")
)
func main() {
s := sb{
iChan: make(chan Interaction),
}
gumbleutil.Main(
gumbleutil.AutoBitrate,
// Parse volume parameter
OnStartListener(func() {
maxVolumeF, err := strconv.Atoi(*maxVolume)
if err != nil {
fmt.Printf("Invalid MaxVolume %d", maxVolumeF)
os.Exit(1)
}
s.maxVol = float32(maxVolumeF) / 100
s.currVol = s.maxVol
fmt.Printf("maximum Volume: %.1f%%\n", s.maxVol*100)
}),
// Scan for Files
OnStartListener(func() {
scanDirs(flag.Args())
fmt.Printf("GoMumbleSoundboard loaded (%d files)\n", len(soundfiles))
}),
// Setup HTTP routes and listener
OnStartListener(s.setupHTTP),
// Handle initial channel move
OnConnectListener(s.initialChannelMove),
// The Soundboard itself
OnConnectListener(s.soundBoardListener),
// Exit on disconnect
OnDisconnectListener(func(_ *gumble.DisconnectEvent) {
os.Exit(1)
}),
)
}
type sb struct {
maxVol float32
currVol float32
iChan chan Interaction
mtx sync.Mutex
}
func (s *sb) initialChannelMove(e *gumble.ConnectEvent) {
e.Client.Self.SetSelfDeafened(true)
fmt.Printf("Connected to %s\n", e.Client.Conn.RemoteAddr())
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)
}
}
func (s *sb) soundBoardListener(e *gumble.ConnectEvent) {
stream := gumbleffmpeg.New(e.Client, nil)
stream.Volume = 1
for interaction := range s.iChan {
if interaction.Stop == true {
_ = stream.Stop()
}
if interaction.Volume != 0 {
s.currVol = interaction.Volume
stream.Volume = s.currVol
}
if interaction.File != nil {
e.Client.Self.SetSelfMuted(false)
stream = gumbleffmpeg.New(e.Client, gumbleffmpeg.SourceFile(interaction.File.FullPath))
stream.Volume = s.currVol
if err := stream.Play(); err != nil {
return
}
go func() {
stream.Wait()
s.mtx.Unlock()
e.Client.Self.SetSelfDeafened(true)
}()
}
}
}
func OnStartListener(f func()) gumbleutil.Listener {
var o sync.Once
return OnConnectListener(func(_ *gumble.ConnectEvent) {
o.Do(f)
})
}
func OnConnectListener(f func(event *gumble.ConnectEvent)) gumbleutil.Listener {
return gumbleutil.Listener{
Connect: func(e *gumble.ConnectEvent) {
f(e)
},
}
}
func OnDisconnectListener(f func(event *gumble.DisconnectEvent)) gumbleutil.Listener {
return gumbleutil.Listener{
Disconnect: func(e *gumble.DisconnectEvent) {
f(e)
},
}
}