1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-14 19:45:41 -05:00

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