mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-11-14 19:45:41 -05:00
remove global conf and state variables
This commit is contained in:
parent
ef8af89893
commit
301bc1b3d1
59
bridge.go
59
bridge.go
@ -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()
|
||||||
@ -101,27 +101,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//Setup initial discord state
|
l.Bridge.Connected = true
|
||||||
g, err := discord.State.Guild(discordGID)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("error finding guild")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for _, vs := range g.VoiceStates {
|
|
||||||
if vs.ChannelID == discordCID {
|
|
||||||
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1
|
|
||||||
u, err := discord.User(vs.UserID)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("error looking up username")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
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.Connected = true
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case sig := <-c:
|
case sig := <-c:
|
||||||
@ -133,15 +113,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")
|
||||||
@ -154,17 +135,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)
|
||||||
}
|
}
|
||||||
@ -179,20 +160,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()
|
||||||
}
|
}
|
||||||
|
34
handlers.go
34
handlers.go
@ -145,26 +145,26 @@ 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
|
||||||
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
|
||||||
@ -178,23 +178,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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +205,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)
|
||||||
}
|
}
|
||||||
@ -214,7 +214,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
|
||||||
|
9
main.go
9
main.go
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user