Skip to content

Commit

Permalink
Merge pull request #9 from stryan/main
Browse files Browse the repository at this point in the history
swap GuildCreate and Ready, fix auto-mode
  • Loading branch information
Stieneee authored Jan 27, 2021
2 parents 79f305c + 4717bde commit 2fe2080
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 23 additions & 12 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -95,6 +98,7 @@ func (b *BridgeState) startBridge() {

if err != nil {
log.Println(err)
b.DiscordVoice.Disconnect()
return
}
defer b.MumbleClient.Disconnect()
Expand Down Expand Up @@ -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)
}

}
}
}()
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down
34 changes: 5 additions & 29 deletions discord-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,15 @@ type DiscordListener struct {
Bridge *BridgeState
}

func (l *DiscordListener) ready(s *discordgo.Session, event *discordgo.Ready) {
log.Println("READY 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
}
}
func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
log.Println("CREATE event registered")

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
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2fe2080

Please sign in to comment.