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

remove global conf and state variables

This commit is contained in:
stryan 2021-01-05 20:25:38 -05:00
parent ac800167f6
commit db0af7edcd
3 changed files with 45 additions and 47 deletions

View File

@ -57,7 +57,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
return
}
defer mumble.Disconnect()
Bridge.Client = mumble
l.Bridge.Client = mumble
// Shared Channels
// Shared channels pass PCM information in 10ms chunks [480]int16
var toMumble = mumble.AudioOutgoing()
@ -109,18 +109,18 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
}
for _, vs := range g.VoiceStates {
if vs.ChannelID == discordCID {
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1
l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
u, err := discord.User(vs.UserID)
if err != nil {
log.Println("error looking up username")
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)
l.Bridge.DiscordUsers[u.Username] = true
l.Bridge.Client.Do(func() {
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
})
}
}
}
Bridge.Connected = true
l.Bridge.Connected = true
select {
case sig := <-c:
@ -132,15 +132,16 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
close(die)
close(m.Close)
close(toMumble)
Bridge.Connected = false
Bridge.Client = nil
Bridge.MumbleUserCount = 0
Bridge.DiscordUserCount = 0
Bridge.DiscordUsers = make(map[string]bool)
l.Bridge.Connected = false
l.Bridge.Client = nil
l.Bridge.MumbleUserCount = 0
l.Bridge.MumbleUsers = make(map[string]bool)
l.Bridge.DiscordUserCount = 0
l.Bridge.DiscordUsers = make(map[string]bool)
}
}
func discordStatusUpdate(dg *discordgo.Session, host, port string) {
func discordStatusUpdate(dg *discordgo.Session, host, port string, l *Listener) {
status := ""
curr := 0
m, _ := time.ParseDuration("30s")
@ -153,17 +154,17 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string) {
dg.UpdateListeningStatus("an error pinging mumble")
} else {
curr = resp.ConnectedUsers
if Bridge.Connected {
if l.Bridge.Connected {
curr = curr - 1
}
if curr != Bridge.MumbleUserCount {
Bridge.MumbleUserCount = curr
if curr != l.Bridge.MumbleUserCount {
l.Bridge.MumbleUserCount = curr
}
if curr == 0 {
status = ""
} else {
if len(Bridge.MumbleUsers) > 0 {
status = fmt.Sprintf("%v/%v users in Mumble\n", len(Bridge.MumbleUsers), curr)
if len(l.Bridge.MumbleUsers) > 0 {
status = fmt.Sprintf("%v/%v users in Mumble\n", len(l.Bridge.MumbleUsers), curr)
} else {
status = fmt.Sprintf("%v users in Mumble\n", curr)
}
@ -178,20 +179,20 @@ func AutoBridge(s *discordgo.Session, l *Listener) {
for {
select {
default:
case <-Bridge.AutoChan:
case <-l.Bridge.AutoChan:
log.Println("ending automode")
return
}
time.Sleep(3 * time.Second)
if !Bridge.Connected && Bridge.MumbleUserCount > 0 && Bridge.DiscordUserCount > 0 {
if !l.Bridge.Connected && l.Bridge.MumbleUserCount > 0 && l.Bridge.DiscordUserCount > 0 {
log.Println("users detected in mumble and discord, bridging")
die := make(chan bool)
Bridge.ActiveConn = die
go startBridge(s, BridgeConf.GID, BridgeConf.CID, l, die)
l.Bridge.ActiveConn = die
go startBridge(s, l.BridgeConf.GID, l.BridgeConf.CID, l, die)
}
if Bridge.Connected && Bridge.MumbleUserCount == 0 && Bridge.DiscordUserCount <= 1 {
if l.Bridge.Connected && l.Bridge.MumbleUserCount == 0 && l.Bridge.DiscordUserCount <= 1 {
log.Println("no one online, killing bridge")
Bridge.ActiveConn <- true
l.Bridge.ActiveConn <- true
MumbleReset()
DiscordReset()
}

View File

@ -145,27 +145,27 @@ func (l *Listener) guildCreate(s *discordgo.Session, event *discordgo.GuildCreat
}
func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
if event.GuildID == BridgeConf.GID {
if event.ChannelID == BridgeConf.CID {
if event.GuildID == l.BridgeConf.GID {
if event.ChannelID == l.BridgeConf.CID {
//get user
u, err := s.User(event.UserID)
if err != nil {
log.Printf("error looking up user for uid %v", event.UserID)
}
//check to see if actually new user
if Bridge.DiscordUsers[u.Username] {
if l.Bridge.DiscordUsers[u.Username] {
//not actually new user
return
}
log.Println("user joined watched discord channel")
if Bridge.Connected {
Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
if l.Bridge.Connected {
l.Bridge.Client.Do(func() {
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
})
}
Bridge.DiscordUsers[u.Username] = true
log.Println(Bridge.DiscordUsers)
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1
l.Bridge.DiscordUsers[u.Username] = true
log.Println(l.Bridge.DiscordUsers)
l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
}
if event.ChannelID == "" {
//leave event, trigger recount of active users
@ -179,23 +179,23 @@ func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceState
// Look for current voice states in watched channel
count := 0
for _, vs := range g.VoiceStates {
if vs.ChannelID == BridgeConf.CID {
if vs.ChannelID == l.BridgeConf.CID {
count = count + 1
}
}
if Bridge.DiscordUserCount > count {
if l.Bridge.DiscordUserCount > count {
u, err := s.User(event.UserID)
if err != nil {
log.Printf("error looking up user for uid %v", event.UserID)
}
delete(Bridge.DiscordUsers, u.Username)
delete(l.Bridge.DiscordUsers, u.Username)
log.Println("user left watched discord channel")
if Bridge.Connected {
Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false)
if l.Bridge.Connected {
l.Bridge.Client.Do(func() {
l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false)
})
}
Bridge.DiscordUserCount = count
l.Bridge.DiscordUserCount = count
}
}
@ -206,7 +206,7 @@ func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceState
func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
if l.BridgeConf.MumbleChannel != "" {
//join specified channel
startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel)
startingChannel := e.Client.Channels.Find(l.BridgeConf.MumbleChannel)
if startingChannel != nil {
e.Client.Self.Move(startingChannel)
}
@ -215,7 +215,7 @@ func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
func (l *Listener) 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)
l.Bridge.MumbleUsers = make(map[string]bool)
for _, user := range l.Bridge.Client.Self.Channel.Users {
//note, this might be too slow for really really big channels?
//event listeners block while processing

View File

@ -15,9 +15,6 @@ import (
_ "layeh.com/gumble/opus"
)
var BridgeConf *BridgeConfig
var Bridge *BridgeState
func main() {
godotenv.Load()
@ -73,7 +70,7 @@ func main() {
config.AudioInterval = time.Millisecond * 10
// Bridge setup
BridgeConf = &BridgeConfig{
BridgeConf := &BridgeConfig{
Config: config,
MumbleAddr: *mumbleAddr + ":" + strconv.Itoa(*mumblePort),
MumbleInsecure: *mumbleInsecure,
@ -83,7 +80,7 @@ func main() {
GID: *discordGID,
CID: *discordCID,
}
Bridge = &BridgeState{
Bridge := &BridgeState{
ActiveConn: make(chan bool),
Connected: false,
MumbleUserCount: 0,
@ -130,7 +127,7 @@ func main() {
log.Fatalln("invalid bridge mode set")
}
go discordStatusUpdate(discord, *mumbleAddr, strconv.Itoa(*mumblePort))
go discordStatusUpdate(discord, *mumbleAddr, strconv.Itoa(*mumblePort), l)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc