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

store channelid in state so other functions can use it

This commit is contained in:
stryan 2021-02-07 15:44:20 -05:00
parent 8e5bb4b479
commit 027d970c11
3 changed files with 19 additions and 12 deletions

View File

@ -70,11 +70,13 @@ type BridgeState struct {
// Mumble Duplex and Event Listener
MumbleStream *MumbleDuplex
MumbleListener *MumbleListener
// Discord Voice channel to join
DiscordChannelID string
}
// startBridge established the voice connection
// if channelID is empty, use channelID from config
func (b *BridgeState) startBridge(channelID string) {
func (b *BridgeState) startBridge() {
b.lock.Lock()
defer b.lock.Unlock()
@ -91,11 +93,11 @@ func (b *BridgeState) startBridge(channelID string) {
// DISCORD Connect Voice
log.Println("Attempting to join Discord voice channel")
if channelID == "" {
b.DiscordVoice, err = b.DiscordSession.ChannelVoiceJoin(b.BridgeConfig.GID, b.BridgeConfig.CID, false, false)
} else {
b.DiscordVoice, err = b.DiscordSession.ChannelVoiceJoin(b.BridgeConfig.GID, channelID, false, false)
if b.DiscordChannelID == "" {
log.Println("Tried to start bridge but no Discord channel specified")
return
}
b.DiscordVoice, err = b.DiscordSession.ChannelVoiceJoin(b.BridgeConfig.GID, b.DiscordChannelID, false, false)
if err != nil {
log.Println(err)
@ -247,7 +249,7 @@ func (b *BridgeState) AutoBridge() {
if !b.Connected && b.MumbleUserCount > 0 && len(b.DiscordUsers) > 0 {
log.Println("users detected in mumble and discord, bridging")
go b.startBridge("")
go b.startBridge()
}
if b.Connected && b.MumbleUserCount == 0 && len(b.DiscordUsers) <= 1 {
log.Println("no one online, killing bridge")

View File

@ -24,7 +24,7 @@ func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.Gui
}
for _, vs := range event.VoiceStates {
if vs.ChannelID == l.Bridge.BridgeConfig.CID {
if vs.ChannelID == l.Bridge.DiscordChannelID {
if s.State.User.ID == vs.UserID {
// Ignore bot
continue
@ -88,7 +88,8 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID {
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
go l.Bridge.startBridge(vs.ChannelID)
l.Bridge.DiscordChannelID = vs.ChannelID
go l.Bridge.startBridge()
return
}
}
@ -114,7 +115,7 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
time.Sleep(5 * time.Second)
go l.Bridge.startBridge(vs.ChannelID)
go l.Bridge.startBridge()
return
}
}
@ -124,10 +125,12 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
if l.Bridge.Mode != bridgeModeAuto {
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Auto mode enabled")
l.Bridge.Mode = bridgeModeAuto
l.Bridge.DiscordChannelID = l.Bridge.BridgeConfig.CID
l.Bridge.AutoChanDie = make(chan bool)
go l.Bridge.AutoBridge()
} else {
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Auto mode disabled")
l.Bridge.DiscordChannelID = ""
l.Bridge.AutoChanDie <- true
l.Bridge.Mode = bridgeModeManual
}
@ -154,7 +157,7 @@ func (l *DiscordListener) voiceUpdate(s *discordgo.Session, event *discordgo.Voi
// Sync the channel voice states to the local discordUsersMap
for _, vs := range g.VoiceStates {
if vs.ChannelID == l.Bridge.BridgeConfig.CID {
if vs.ChannelID == l.Bridge.DiscordChannelID {
if s.State.User.ID == vs.UserID {
// Ignore bot
continue

View File

@ -165,6 +165,7 @@ func main() {
log.Println("bridge starting in automatic mode")
Bridge.AutoChanDie = make(chan bool)
Bridge.Mode = bridgeModeAuto
Bridge.DiscordChannelID = Bridge.BridgeConfig.CID
go Bridge.AutoBridge()
case "manual":
log.Println("bridge starting in manual mode")
@ -172,9 +173,10 @@ func main() {
case "constant":
log.Println("bridge starting in constant mode")
Bridge.Mode = bridgeModeConstant
Bridge.DiscordChannelID = Bridge.BridgeConfig.CID
go func() {
for {
Bridge.startBridge("")
Bridge.startBridge()
log.Println("Bridge died. Restarting")
}
}()