1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-23 05:45:41 -05:00
mumble-discord-bridge/internal/bridge/mumble-handlers.go

92 lines
2.3 KiB
Go
Raw Normal View History

package bridge
import (
"fmt"
2021-02-01 16:03:38 -05:00
"log"
"strings"
"time"
"github.com/stieneee/gumble/gumble"
)
// MumbleListener Handle mumble events
type MumbleListener struct {
Bridge *BridgeState
}
func (l *MumbleListener) updateUsers() {
l.Bridge.MumbleUsersMutex.Lock()
l.Bridge.MumbleUsers = make(map[string]bool)
for _, user := range l.Bridge.MumbleClient.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 != l.Bridge.MumbleClient.Self.Name {
l.Bridge.MumbleUsers[user.Name] = true
}
}
promMumbleUsers.Set(float64(len(l.Bridge.MumbleUsers)))
l.Bridge.MumbleUsersMutex.Unlock()
}
func (l *MumbleListener) MumbleConnect(e *gumble.ConnectEvent) {
2021-02-08 13:16:09 -05:00
//join specified channel
startingChannel := e.Client.Channels.Find(l.Bridge.BridgeConfig.MumbleChannel...)
if startingChannel != nil {
e.Client.Self.Move(startingChannel)
}
// l.updateUsers() // patch below
// This is an ugly patch Mumble Client state is slow to update
time.AfterFunc(5*time.Second, func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Failed to mumble user list %v \n", r)
}
}()
l.updateUsers()
})
}
func (l *MumbleListener) MumbleUserChange(e *gumble.UserChangeEvent) {
l.updateUsers()
if e.Type.Has(gumble.UserChangeConnected) {
2021-02-01 16:03:38 -05:00
log.Println("User connected to mumble " + e.User.Name)
if !l.Bridge.BridgeConfig.MumbleDisableText {
e.User.Send("Mumble-Discord-Bridge v" + l.Bridge.BridgeConfig.Version)
// Tell the user who is connected to discord
l.Bridge.DiscordUsersMutex.Lock()
if len(l.Bridge.DiscordUsers) == 0 {
e.User.Send("No users connected to Discord")
} else {
s := "Connected to Discord: "
arr := []string{}
for u := range l.Bridge.DiscordUsers {
arr = append(arr, l.Bridge.DiscordUsers[u].username)
}
s = s + strings.Join(arr[:], ",")
e.User.Send(s)
}
l.Bridge.DiscordUsersMutex.Unlock()
}
// Send discord a notice
l.Bridge.discordSendMessageAll(e.User.Name + " has joined mumble")
}
2021-02-01 16:03:38 -05:00
if e.Type.Has(gumble.UserChangeDisconnected) {
l.Bridge.discordSendMessageAll(e.User.Name + " has left mumble")
2021-02-01 16:03:38 -05:00
log.Println("User disconnected from mumble " + e.User.Name)
}
}