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 return
} }
defer mumble.Disconnect() defer mumble.Disconnect()
Bridge.Client = mumble l.Bridge.Client = mumble
// 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()
@ -109,18 +109,18 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
} }
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.ChannelID == discordCID { if vs.ChannelID == discordCID {
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1 l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
u, err := discord.User(vs.UserID) u, err := discord.User(vs.UserID)
if err != nil { if err != nil {
log.Println("error looking up username") log.Println("error looking up username")
Bridge.DiscordUsers[u.Username] = true l.Bridge.DiscordUsers[u.Username] = true
Bridge.Client.Do(func() { l.Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) 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 { select {
case sig := <-c: case sig := <-c:
@ -132,15 +132,16 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
close(die) close(die)
close(m.Close) close(m.Close)
close(toMumble) close(toMumble)
Bridge.Connected = false l.Bridge.Connected = false
Bridge.Client = nil l.Bridge.Client = nil
Bridge.MumbleUserCount = 0 l.Bridge.MumbleUserCount = 0
Bridge.DiscordUserCount = 0 l.Bridge.MumbleUsers = make(map[string]bool)
Bridge.DiscordUsers = 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 := "" status := ""
curr := 0 curr := 0
m, _ := time.ParseDuration("30s") m, _ := time.ParseDuration("30s")
@ -153,17 +154,17 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string) {
dg.UpdateListeningStatus("an error pinging mumble") dg.UpdateListeningStatus("an error pinging mumble")
} else { } else {
curr = resp.ConnectedUsers curr = resp.ConnectedUsers
if Bridge.Connected { if l.Bridge.Connected {
curr = curr - 1 curr = curr - 1
} }
if curr != Bridge.MumbleUserCount { if curr != l.Bridge.MumbleUserCount {
Bridge.MumbleUserCount = curr l.Bridge.MumbleUserCount = curr
} }
if curr == 0 { if curr == 0 {
status = "" status = ""
} else { } else {
if len(Bridge.MumbleUsers) > 0 { if len(l.Bridge.MumbleUsers) > 0 {
status = fmt.Sprintf("%v/%v users in Mumble\n", len(Bridge.MumbleUsers), curr) status = fmt.Sprintf("%v/%v users in Mumble\n", len(l.Bridge.MumbleUsers), curr)
} else { } else {
status = fmt.Sprintf("%v users in Mumble\n", curr) status = fmt.Sprintf("%v users in Mumble\n", curr)
} }
@ -178,20 +179,20 @@ func AutoBridge(s *discordgo.Session, l *Listener) {
for { for {
select { select {
default: default:
case <-Bridge.AutoChan: case <-l.Bridge.AutoChan:
log.Println("ending automode") log.Println("ending automode")
return return
} }
time.Sleep(3 * time.Second) 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") log.Println("users detected in mumble and discord, bridging")
die := make(chan bool) die := make(chan bool)
Bridge.ActiveConn = die l.Bridge.ActiveConn = die
go startBridge(s, BridgeConf.GID, BridgeConf.CID, l, 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") log.Println("no one online, killing bridge")
Bridge.ActiveConn <- true l.Bridge.ActiveConn <- true
MumbleReset() MumbleReset()
DiscordReset() 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) { func (l *Listener) voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
if event.GuildID == BridgeConf.GID { if event.GuildID == l.BridgeConf.GID {
if event.ChannelID == BridgeConf.CID { if event.ChannelID == l.BridgeConf.CID {
//get user //get user
u, err := s.User(event.UserID) u, err := s.User(event.UserID)
if err != nil { if err != nil {
log.Printf("error looking up user for uid %v", event.UserID) log.Printf("error looking up user for uid %v", event.UserID)
} }
//check to see if actually new user //check to see if actually new user
if Bridge.DiscordUsers[u.Username] { if l.Bridge.DiscordUsers[u.Username] {
//not actually new user //not actually new user
return return
} }
log.Println("user joined watched discord channel") log.Println("user joined watched discord channel")
if Bridge.Connected { if l.Bridge.Connected {
Bridge.Client.Do(func() { l.Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) l.Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
}) })
} }
Bridge.DiscordUsers[u.Username] = true l.Bridge.DiscordUsers[u.Username] = true
log.Println(Bridge.DiscordUsers) log.Println(l.Bridge.DiscordUsers)
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1 l.Bridge.DiscordUserCount = l.Bridge.DiscordUserCount + 1
} }
if event.ChannelID == "" { if event.ChannelID == "" {
//leave event, trigger recount of active users //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 // Look for current voice states in watched channel
count := 0 count := 0
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.ChannelID == BridgeConf.CID { if vs.ChannelID == l.BridgeConf.CID {
count = count + 1 count = count + 1
} }
} }
if Bridge.DiscordUserCount > count { if l.Bridge.DiscordUserCount > count {
u, err := s.User(event.UserID) u, err := s.User(event.UserID)
if err != nil { if err != nil {
log.Printf("error looking up user for uid %v", event.UserID) 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") log.Println("user left watched discord channel")
if Bridge.Connected { if l.Bridge.Connected {
Bridge.Client.Do(func() { l.Bridge.Client.Do(func() {
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false) 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) { func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
if l.BridgeConf.MumbleChannel != "" { if l.BridgeConf.MumbleChannel != "" {
//join specified channel //join specified channel
startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel) startingChannel := e.Client.Channels.Find(l.BridgeConf.MumbleChannel)
if startingChannel != nil { if startingChannel != nil {
e.Client.Self.Move(startingChannel) e.Client.Self.Move(startingChannel)
} }
@ -215,7 +215,7 @@ func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
func (l *Listener) mumbleUserChange(e *gumble.UserChangeEvent) { func (l *Listener) mumbleUserChange(e *gumble.UserChangeEvent) {
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) { 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 { for _, user := range l.Bridge.Client.Self.Channel.Users {
//note, this might be too slow for really really big channels? //note, this might be too slow for really really big channels?
//event listeners block while processing //event listeners block while processing

View File

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