1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-14 19:45:41 -05:00

Merge pull request #2 from stryan/mumble-thread-cleanup

Mumble thread cleanup
This commit is contained in:
stryan 2021-01-05 19:10:34 -05:00 committed by GitHub
commit f41a2706c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 12 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"layeh.com/gumble/gumble" "layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleutil"
) )
type BridgeState struct { type BridgeState struct {
@ -19,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
@ -44,7 +46,10 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
if mumbleInsecure { if mumbleInsecure {
tlsConfig.InsecureSkipVerify = true tlsConfig.InsecureSkipVerify = true
} }
config.Attach(gumbleutil.Listener{
Connect: mumbleConnect,
UserChange: mumbleUserChange,
})
mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig) mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig)
if err != nil { if err != nil {
@ -53,13 +58,6 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
} }
defer mumble.Disconnect() defer mumble.Disconnect()
Bridge.Client = mumble Bridge.Client = mumble
if BridgeConf.MumbleChannel != "" {
//join specified channel
startingChannel := mumble.Channels.Find(BridgeConf.MumbleChannel)
if startingChannel != nil {
mumble.Self.Move(startingChannel)
}
}
// Shared Channels // Shared Channels
// Shared channels pass PCM information in 10ms chunks [480]int16 // Shared channels pass PCM information in 10ms chunks [480]int16
var toMumble = mumble.AudioOutgoing() var toMumble = mumble.AudioOutgoing()
@ -71,6 +69,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
// Mumble // Mumble
go m.fromMumbleMixer(toDiscord, die) go m.fromMumbleMixer(toDiscord, die)
det := config.AudioListeners.Attach(m) det := config.AudioListeners.Attach(m)
//Discord //Discord
go discordReceivePCM(dgv, die) go discordReceivePCM(dgv, die)
go fromDiscordMixer(toMumble, die) go fromDiscordMixer(toMumble, die)
@ -115,7 +114,9 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
if err != nil { if err != nil {
log.Println("error looking up username") log.Println("error looking up username")
Bridge.DiscordUsers[u.Username] = true Bridge.DiscordUsers[u.Username] = true
Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
})
} }
} }
} }
@ -160,9 +161,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

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"layeh.com/gumble/gumble"
) )
func ready(s *discordgo.Session, event *discordgo.Ready) { func ready(s *discordgo.Session, event *discordgo.Ready) {
@ -153,7 +154,9 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
} }
log.Println("user joined watched discord channel") log.Println("user joined watched discord channel")
if Bridge.Connected { if Bridge.Connected {
Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
})
} }
Bridge.DiscordUsers[u.Username] = true Bridge.DiscordUsers[u.Username] = true
log.Println(Bridge.DiscordUsers) log.Println(Bridge.DiscordUsers)
@ -183,7 +186,9 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
delete(Bridge.DiscordUsers, u.Username) delete(Bridge.DiscordUsers, u.Username)
log.Println("user left watched discord channel") log.Println("user left watched discord channel")
if Bridge.Connected { if Bridge.Connected {
Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false) Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false)
})
} }
Bridge.DiscordUserCount = count Bridge.DiscordUserCount = count
} }
@ -192,3 +197,27 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
} }
return return
} }
func mumbleConnect(e *gumble.ConnectEvent) {
if BridgeConf.MumbleChannel != "" {
//join specified channel
startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel)
if startingChannel != nil {
e.Client.Self.Move(startingChannel)
}
}
}
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
}
}
}
}

View File

@ -91,6 +91,7 @@ func main() {
config.Password = *mumblePassword config.Password = *mumblePassword
config.AudioInterval = time.Millisecond * 10 config.AudioInterval = time.Millisecond * 10
// Bridge setup
BridgeConf = &BridgeConfig{ BridgeConf = &BridgeConfig{
Config: config, Config: config,
MumbleAddr: *mumbleAddr + ":" + strconv.Itoa(*mumblePort), MumbleAddr: *mumbleAddr + ":" + strconv.Itoa(*mumblePort),