From daacaaad5023f45dca269a0662dba0eeb7b9edb6 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 19 Jan 2021 13:19:05 -0500 Subject: [PATCH 1/4] use discord CreateGuild instead of Ready --- discord-handlers.go | 34 +++++----------------------------- main.go | 1 - 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/discord-handlers.go b/discord-handlers.go index cff766a..002edf2 100644 --- a/discord-handlers.go +++ b/discord-handlers.go @@ -15,25 +15,15 @@ type DiscordListener struct { Bridge *BridgeState } -func (l *DiscordListener) ready(s *discordgo.Session, event *discordgo.Ready) { - log.Println("READY event registered") +func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { + log.Println("CREATE event registered") - //Setup initial discord state - var g *discordgo.Guild - g = nil - - for _, i := range event.Guilds { - if i.ID == l.Bridge.BridgeConfig.GID { - g = i - } - } - - if g == nil { - log.Println("bad guild on READY") + if event.ID != l.Bridge.BridgeConfig.GID { + log.Println("received GuildCreate from a guild not in config") return } - for _, vs := range g.VoiceStates { + for _, vs := range event.VoiceStates { if vs.ChannelID == l.Bridge.BridgeConfig.CID { if s.State.User.ID == vs.UserID { // Ignore bot @@ -143,20 +133,6 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa } } -func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { - - if event.Guild.Unavailable { - return - } - - for _, channel := range event.Guild.Channels { - if channel.ID == event.Guild.ID { - log.Println("Mumble-Discord bridge is active in new guild") - return - } - } -} - func (l *DiscordListener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { l.Bridge.DiscordUsersMutex.Lock() defer l.Bridge.DiscordUsersMutex.Unlock() diff --git a/main.go b/main.go index 655943e..52c6a33 100644 --- a/main.go +++ b/main.go @@ -144,7 +144,6 @@ func main() { Bridge.DiscordListener = &DiscordListener{ Bridge: Bridge, } - Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.ready) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.messageCreate) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.guildCreate) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.voiceUpdate) From 577c5e702ecb2897e5b5491bb3cca237e1dc5f20 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 19 Jan 2021 13:48:51 -0500 Subject: [PATCH 2/4] fix auto mode, use mumble ping, die properly --- bridge.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/bridge.go b/bridge.go index 16d96fb..9d1c980 100644 --- a/bridge.go +++ b/bridge.go @@ -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() From 3b73e89ae794369171c0504d241c145e840865df Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 19 Jan 2021 13:51:32 -0500 Subject: [PATCH 3/4] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2979a60..465ffd5 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,8 @@ It is a known issue with a dependency of this project. Audio leveling from Discord needs to be improved. +Delays in connecting to Mumble (such as from external authentication plugins) may result in extra error messages on initial connection. + ## License Distributed under the MIT License. See LICENSE for more information. From 4717bdec27e879b47970e54896071da0ff73b2f8 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 26 Jan 2021 10:33:47 -0500 Subject: [PATCH 4/4] on mumble connect failure disconnect from discord too --- bridge.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bridge.go b/bridge.go index 9d1c980..7ea2e52 100644 --- a/bridge.go +++ b/bridge.go @@ -98,6 +98,7 @@ func (b *BridgeState) startBridge() { if err != nil { log.Println(err) + b.DiscordVoice.Disconnect() return } defer b.MumbleClient.Disconnect()