diff --git a/bridge.go b/bridge.go index 3b1b4d0..bc2cc69 100644 --- a/bridge.go +++ b/bridge.go @@ -14,10 +14,11 @@ import ( "layeh.com/gumble/gumble" ) +//BridgeState manages dynamic information about the bridge during runtime type BridgeState struct { ActiveConn chan bool Connected bool - Mode BridgeMode + Mode bridgeMode Client *gumble.Client DiscordUsers map[string]bool MumbleUsers map[string]bool @@ -157,6 +158,9 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string, l *Listener) } } +//AutoBridge starts a goroutine to check the number of users in discord and mumble +//when there is at least one user on both, starts up the bridge +//when there are no users on either side, kills the bridge func AutoBridge(s *discordgo.Session, l *Listener) { log.Println("beginning auto mode") for { @@ -177,8 +181,7 @@ func AutoBridge(s *discordgo.Session, l *Listener) { if l.Bridge.Connected && l.Bridge.MumbleUserCount == 0 && l.Bridge.DiscordUserCount <= 1 { log.Println("no one online, killing bridge") l.Bridge.ActiveConn <- true - MumbleReset() - DiscordReset() + l.Bridge.ActiveConn = nil } l.UserCountLock.Unlock() } diff --git a/config.go b/config.go index cf58b82..4522085 100644 --- a/config.go +++ b/config.go @@ -10,14 +10,16 @@ import ( "layeh.com/gumble/gumble" ) -type BridgeMode int +type bridgeMode int const ( - BridgeModeAuto BridgeMode = iota - BridgeModeManual - BridgeModeConstant + bridgeModeAuto bridgeMode = iota + bridgeModeManual + bridgeModeConstant ) +//BridgeConfig holds configuration information set at startup +//It should not change during runtime type BridgeConfig struct { Config *gumble.Config MumbleAddr string diff --git a/discord.go b/discord.go index 462abb7..2f72daf 100644 --- a/discord.go +++ b/discord.go @@ -22,10 +22,6 @@ var discordMutex sync.Mutex var discordMixerMutex sync.Mutex var fromDiscordMap = make(map[uint32]fromDiscord) -func DiscordReset() { - fromDiscordMap = make(map[uint32]fromDiscord) -} - // OnError gets called by dgvoice when an error is encountered. // By default logs to STDERR var OnError = func(str string, err error) { diff --git a/handlers.go b/handlers.go index a22101f..69b0cc0 100644 --- a/handlers.go +++ b/handlers.go @@ -11,6 +11,8 @@ import ( "layeh.com/gumble/gumble" ) +//Listener holds references to the current BridgeConf +//and BridgeState for use by the event handlers type Listener struct { BridgeConf *BridgeConfig Bridge *BridgeState @@ -22,11 +24,16 @@ func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) { log.Println("READY event registered") //Setup initial discord state var g *discordgo.Guild + g = nil for _, i := range event.Guilds { if i.ID == l.BridgeConf.GID { g = i } } + if g == nil { + log.Println("bad guild on READY") + return + } for _, vs := range g.VoiceStates { if vs.ChannelID == l.BridgeConf.CID { l.UserCountLock.Lock() @@ -48,7 +55,7 @@ func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) { func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - if l.Bridge.Mode == BridgeModeConstant { + if l.Bridge.Mode == bridgeModeConstant { return } @@ -56,23 +63,21 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat if m.Author.ID == s.State.User.ID { return } + // Find the channel that the message came from. + c, err := s.State.Channel(m.ChannelID) + if err != nil { + // Could not find channel. + return + } + + // Find the guild for that channel. + g, err := s.State.Guild(c.GuildID) + if err != nil { + // Could not find guild. + return + } prefix := "!" + l.BridgeConf.Command if strings.HasPrefix(m.Content, prefix+" link") { - - // Find the channel that the message came from. - c, err := s.State.Channel(m.ChannelID) - if err != nil { - // Could not find channel. - return - } - - // Find the guild for that channel. - g, err := s.State.Guild(c.GuildID) - if err != nil { - // Could not find guild. - return - } - // Look for the message sender in that guild's current voice states. for _, vs := range g.VoiceStates { if vs.UserID == m.Author.ID { @@ -86,57 +91,23 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat } if strings.HasPrefix(m.Content, prefix+" unlink") { - - // Find the channel that the message came from. - c, err := s.State.Channel(m.ChannelID) - if err != nil { - // Could not find channel. - return - } - - // Find the guild for that channel. - g, err := s.State.Guild(c.GuildID) - if err != nil { - // Could not find guild. - return - } - // Look for the message sender in that guild's current voice states. for _, vs := range g.VoiceStates { if vs.UserID == m.Author.ID { log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID) l.Bridge.ActiveConn <- true l.Bridge.ActiveConn = nil - MumbleReset() - DiscordReset() return } } } if strings.HasPrefix(m.Content, prefix+" refresh") { - - // Find the channel that the message came from. - c, err := s.State.Channel(m.ChannelID) - if err != nil { - // Could not find channel. - return - } - - // Find the guild for that channel. - g, err := s.State.Guild(c.GuildID) - if err != nil { - // Could not find guild. - return - } - // Look for the message sender in that guild's current voice states. for _, vs := range g.VoiceStates { if vs.UserID == m.Author.ID { log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID) l.Bridge.ActiveConn <- true - MumbleReset() - DiscordReset() time.Sleep(5 * time.Second) l.Bridge.ActiveConn = make(chan bool) go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn) @@ -146,13 +117,13 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat } if strings.HasPrefix(m.Content, prefix+" auto") { - if l.Bridge.Mode != BridgeModeAuto { - l.Bridge.Mode = BridgeModeAuto + if l.Bridge.Mode != bridgeModeAuto { + l.Bridge.Mode = bridgeModeAuto l.Bridge.AutoChan = make(chan bool) go AutoBridge(s, l) } else { l.Bridge.AutoChan <- true - l.Bridge.Mode = BridgeModeManual + l.Bridge.Mode = bridgeModeManual } } } diff --git a/main.go b/main.go index 77ce55b..e8414d1 100644 --- a/main.go +++ b/main.go @@ -121,14 +121,14 @@ func main() { case "auto": log.Println("bridge starting in automatic mode") Bridge.AutoChan = make(chan bool) - Bridge.Mode = BridgeModeAuto + Bridge.Mode = bridgeModeAuto go AutoBridge(discord, l) case "manual": log.Println("bridge starting in manual mode") - Bridge.Mode = BridgeModeManual + Bridge.Mode = bridgeModeManual case "constant": log.Println("bridge starting in constant mode") - Bridge.Mode = BridgeModeConstant + Bridge.Mode = bridgeModeConstant go startBridge(discord, *discordGID, *discordCID, l, make(chan bool)) default: discord.Close() diff --git a/mumble.go b/mumble.go index 09141bc..24d2d4e 100644 --- a/mumble.go +++ b/mumble.go @@ -18,11 +18,6 @@ type MumbleDuplex struct { Close chan bool } -func MumbleReset() { - fromMumbleArr = []chan gumble.AudioBuffer{} - mumbleStreamingArr = []bool{} -} - // OnAudioStream - Spawn routines to handle incoming packets func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {