fix auto mode, use mumble ping, die properly

This commit is contained in:
stryan 2021-01-19 13:48:51 -05:00
parent daacaaad50
commit 577c5e702e

View File

@ -50,6 +50,9 @@ type BridgeState struct {
MumbleUsers map[string]bool
MumbleUsersMutex sync.Mutex
// Total Number of Mumble users
MumbleUserCount int
// Kill the auto connect routine
AutoChanDie chan bool
@ -129,14 +132,21 @@ func (b *BridgeState) startBridge() {
ticker := time.NewTicker(500 * time.Millisecond)
for {
<-ticker.C
if b.MumbleClient.State() != 2 {
log.Println("Lost mumble connection " + strconv.Itoa(int(b.MumbleClient.State())))
if b.MumbleClient == nil || b.MumbleClient.State() != 2 {
if b.MumbleClient != nil {
log.Println("Lost mumble connection " + strconv.Itoa(int(b.MumbleClient.State())))
} else {
log.Println("Lost mumble connection due to bridge dieing")
return
}
select {
default:
close(b.BridgeDie)
case <-b.BridgeDie:
//die is already closed
default:
close(b.BridgeDie)
}
}
}
}()
@ -150,6 +160,7 @@ func (b *BridgeState) startBridge() {
det.Detach()
close(toDiscord)
close(toMumble)
close(b.BridgeDie)
b.Connected = false
b.DiscordVoice = nil
b.MumbleClient = nil
@ -170,17 +181,17 @@ func (b *BridgeState) discordStatusUpdate() {
b.DiscordSession.UpdateListeningStatus("an error pinging mumble")
} else {
b.MumbleUsersMutex.Lock()
userCount := resp.ConnectedUsers
b.MumbleUserCount = resp.ConnectedUsers
if b.Connected {
userCount = userCount - 1
b.MumbleUserCount = b.MumbleUserCount - 1
}
if userCount == 0 {
if b.MumbleUserCount == 0 {
status = "No users in Mumble"
} else {
if len(b.MumbleUsers) > 0 {
status = fmt.Sprintf("%v/%v users in Mumble\n", len(b.MumbleUsers), userCount)
status = fmt.Sprintf("%v/%v users in Mumble\n", len(b.MumbleUsers), b.MumbleUserCount)
} else {
status = fmt.Sprintf("%v users in Mumble\n", userCount)
status = fmt.Sprintf("%v users in Mumble\n", b.MumbleUserCount)
}
}
b.MumbleUsersMutex.Unlock()
@ -207,14 +218,13 @@ func (b *BridgeState) AutoBridge() {
b.MumbleUsersMutex.Lock()
b.DiscordUsersMutex.Lock()
if !b.Connected && len(b.MumbleUsers) > 0 && len(b.DiscordUsers) > 0 {
if !b.Connected && b.MumbleUserCount > 0 && len(b.DiscordUsers) > 0 {
log.Println("users detected in mumble and discord, bridging")
go b.startBridge()
}
if b.Connected && len(b.MumbleUsers) == 0 && len(b.DiscordUsers) <= 1 {
if b.Connected && b.MumbleUserCount == 0 && len(b.DiscordUsers) <= 1 {
log.Println("no one online, killing bridge")
b.BridgeDie <- true
b.BridgeDie = nil
}
b.MumbleUsersMutex.Unlock()