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.
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.

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

View File

@ -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()

View File

@ -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)