-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (69 loc) · 2.14 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
package main
import (
"flag"
"github.com/iotku/mumzic/database"
"log"
"github.com/iotku/mumzic/commands"
"github.com/iotku/mumzic/config"
"github.com/iotku/mumzic/playback"
_ "github.com/mattn/go-sqlite3"
"layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleutil"
)
func main() {
var channelPlayer *playback.Player
var bConfig *config.Config
var hostname, username string
gumbleutil.Main(gumbleutil.AutoBitrate, gumbleutil.Listener{
Connect: func(e *gumble.ConnectEvent) {
hostname = getValueFromFlag(flag.Lookup("server"))
username = getValueFromFlag(flag.Lookup("username"))
bConfig = config.NewConfig(hostname)
if e.Client.Channels.Find(bConfig.Channel) != nil {
e.Client.Self.Move(e.Client.Channels.Find(bConfig.Channel))
}
channelPlayer = playback.NewPlayer(e.Client, bConfig)
channelPlayer.Playlist.Load(bConfig.Hostname)
log.Printf("audio player loaded! (%d files)\n", database.GetMaxID())
},
TextMessage: func(e *gumble.TextMessageEvent) {
if e.Sender == nil {
return
}
isPrivate := len(e.TextMessage.Channels) == 0 // If no channels, is private message
logMessage(e, isPrivate)
if commands.IsCommand(e.Message, isPrivate, username, bConfig) {
go commands.CommandDispatch(channelPlayer, e.Message, isPrivate, e.Sender.Name)
}
},
ChannelChange: func(e *gumble.ChannelChangeEvent) {
if bConfig != nil && !e.Channel.IsRoot() {
bConfig.Channel = e.Channel.Name
}
if channelPlayer != nil {
channelPlayer.TargetUsers()
}
},
Disconnect: func(e *gumble.DisconnectEvent) {
log.Println("Disconnecting: ", e.Type)
bConfig.Channel = channelPlayer.Client.Self.Channel.Name
bConfig.Save()
channelPlayer.Playlist.Save(bConfig.Hostname)
config.CloseDatabase()
database.Close()
},
})
}
func logMessage(e *gumble.TextMessageEvent, isPrivate bool) {
if isPrivate {
log.Printf("DMSG (%s): %s", e.Sender.Name, e.Message)
} else {
log.Printf("CMSG (%s) %s: %s", e.Sender.Channel.Name, e.Sender.Name, e.Message)
}
}
func getValueFromFlag(lookup *flag.Flag) string {
if lookup == nil {
panic("getValueFromFlag: flagNotFound: nil")
}
return lookup.Value.String()
}