Merge pull request #9 from stryan/main

swap GuildCreate and Ready, fix auto-mode
This commit is contained in:
Tyler Stiene 2021-01-26 21:39:40 -05:00 committed by GitHub
commit 2fe2080bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 42 deletions

View File

@ -139,6 +139,8 @@ It is a known issue with a dependency of this project.
Audio leveling from Discord needs to be improved. 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 ## License
Distributed under the MIT License. See LICENSE for more information. Distributed under the MIT License. See LICENSE for more information.

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
@ -95,6 +98,7 @@ func (b *BridgeState) startBridge() {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
b.DiscordVoice.Disconnect()
return return
} }
defer b.MumbleClient.Disconnect() defer b.MumbleClient.Disconnect()
@ -129,14 +133,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 {
if b.MumbleClient != nil {
log.Println("Lost mumble connection " + strconv.Itoa(int(b.MumbleClient.State()))) 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 +161,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 +182,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 +219,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()

View File

@ -15,25 +15,15 @@ type DiscordListener struct {
Bridge *BridgeState Bridge *BridgeState
} }
func (l *DiscordListener) ready(s *discordgo.Session, event *discordgo.Ready) { func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
log.Println("READY event registered") log.Println("CREATE event registered")
//Setup initial discord state if event.ID != l.Bridge.BridgeConfig.GID {
var g *discordgo.Guild log.Println("received GuildCreate from a guild not in config")
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")
return return
} }
for _, vs := range g.VoiceStates { for _, vs := range event.VoiceStates {
if vs.ChannelID == l.Bridge.BridgeConfig.CID { if vs.ChannelID == l.Bridge.BridgeConfig.CID {
if s.State.User.ID == vs.UserID { if s.State.User.ID == vs.UserID {
// Ignore bot // 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) { func (l *DiscordListener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
l.Bridge.DiscordUsersMutex.Lock() l.Bridge.DiscordUsersMutex.Lock()
defer l.Bridge.DiscordUsersMutex.Unlock() defer l.Bridge.DiscordUsersMutex.Unlock()

View File

@ -144,7 +144,6 @@ func main() {
Bridge.DiscordListener = &DiscordListener{ Bridge.DiscordListener = &DiscordListener{
Bridge: Bridge, Bridge: Bridge,
} }
Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.ready)
Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.messageCreate) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.messageCreate)
Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.guildCreate) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.guildCreate)
Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.voiceUpdate) Bridge.DiscordSession.AddHandler(Bridge.DiscordListener.voiceUpdate)