count how many users are actually in channel

This commit is contained in:
stryan 2021-01-05 19:00:28 -05:00
parent c469a65ed6
commit 1e8a0a4165
2 changed files with 22 additions and 2 deletions

View File

@ -20,6 +20,7 @@ type BridgeState struct {
Connected bool Connected bool
Client *gumble.Client Client *gumble.Client
DiscordUsers map[string]bool DiscordUsers map[string]bool
MumbleUsers map[string]bool
MumbleUserCount int MumbleUserCount int
DiscordUserCount int DiscordUserCount int
AutoChan chan bool AutoChan chan bool
@ -47,6 +48,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
} }
config.Attach(gumbleutil.Listener{ config.Attach(gumbleutil.Listener{
Connect: mumbleConnect, Connect: mumbleConnect,
UserChange: mumbleUserChange,
}) })
mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig) mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig)
@ -157,9 +159,13 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string) {
} }
if curr == 0 { if curr == 0 {
status = "" status = ""
} else {
if len(Bridge.MumbleUsers) > 0 {
status = fmt.Sprintf("%v/%v users in Mumble\n", len(Bridge.MumbleUsers), curr)
} else { } else {
status = fmt.Sprintf("%v users in Mumble\n", curr) status = fmt.Sprintf("%v users in Mumble\n", curr)
} }
}
dg.UpdateListeningStatus(status) dg.UpdateListeningStatus(status)
} }
} }

View File

@ -203,3 +203,17 @@ func mumbleConnect(e *gumble.ConnectEvent) {
} }
} }
} }
func mumbleUserChange(e *gumble.UserChangeEvent) {
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) {
Bridge.MumbleUsers = make(map[string]bool)
for _, user := range Bridge.Client.Self.Channel.Users {
//note, this might be too slow for really really big channels?
//event listeners block while processing
//also probably bad to rebuild the set every user change.
if user.Name != Bridge.Client.Self.Name {
Bridge.MumbleUsers[user.Name] = true
}
}
}
}