mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-11-23 05:45:41 -05:00
commit
3267dc5f2d
10
bridge.go
10
bridge.go
@ -70,6 +70,9 @@ type BridgeState struct {
|
|||||||
// Mumble Duplex and Event Listener
|
// Mumble Duplex and Event Listener
|
||||||
MumbleStream *MumbleDuplex
|
MumbleStream *MumbleDuplex
|
||||||
MumbleListener *MumbleListener
|
MumbleListener *MumbleListener
|
||||||
|
|
||||||
|
// Discord Voice channel to join
|
||||||
|
DiscordChannelID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// startBridge established the voice connection
|
// startBridge established the voice connection
|
||||||
@ -90,7 +93,12 @@ func (b *BridgeState) startBridge() {
|
|||||||
|
|
||||||
// DISCORD Connect Voice
|
// DISCORD Connect Voice
|
||||||
log.Println("Attempting to join Discord voice channel")
|
log.Println("Attempting to join Discord voice channel")
|
||||||
b.DiscordVoice, err = b.DiscordSession.ChannelVoiceJoin(b.BridgeConfig.GID, b.BridgeConfig.CID, 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 {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
b.DiscordVoice.Disconnect()
|
b.DiscordVoice.Disconnect()
|
||||||
|
@ -24,7 +24,7 @@ func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.Gui
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, vs := range event.VoiceStates {
|
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 {
|
if s.State.User.ID == vs.UserID {
|
||||||
// Ignore bot
|
// Ignore bot
|
||||||
continue
|
continue
|
||||||
@ -61,10 +61,6 @@ func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.Gui
|
|||||||
|
|
||||||
func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
if l.Bridge.Mode == bridgeModeConstant {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore all messages created by the bot itself
|
// Ignore all messages created by the bot itself
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
@ -83,11 +79,22 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
prefix := "!" + l.Bridge.BridgeConfig.Command
|
prefix := "!" + l.Bridge.BridgeConfig.Command
|
||||||
|
|
||||||
|
if l.Bridge.Mode == bridgeModeConstant && strings.HasPrefix(m.Content, prefix) {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Constant mode enabled, manual commands can not be entered")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(m.Content, prefix+" link") {
|
if strings.HasPrefix(m.Content, prefix+" link") {
|
||||||
// Look for the message sender in that guild's current voice states.
|
// Look for the message sender in that guild's current voice states.
|
||||||
for _, vs := range g.VoiceStates {
|
for _, vs := range g.VoiceStates {
|
||||||
|
if l.Bridge.Connected {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Bridge already running, unlink first")
|
||||||
|
return
|
||||||
|
}
|
||||||
if vs.UserID == m.Author.ID {
|
if vs.UserID == m.Author.ID {
|
||||||
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
|
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||||
|
l.Bridge.DiscordChannelID = vs.ChannelID
|
||||||
go l.Bridge.startBridge()
|
go l.Bridge.startBridge()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -96,8 +103,12 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
|||||||
|
|
||||||
if strings.HasPrefix(m.Content, prefix+" unlink") {
|
if strings.HasPrefix(m.Content, prefix+" unlink") {
|
||||||
// Look for the message sender in that guild's current voice states.
|
// Look for the message sender in that guild's current voice states.
|
||||||
|
if !l.Bridge.Connected {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Bridge is not currently running")
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, vs := range g.VoiceStates {
|
for _, vs := range g.VoiceStates {
|
||||||
if vs.UserID == m.Author.ID {
|
if vs.UserID == m.Author.ID && vs.ChannelID == l.Bridge.DiscordChannelID {
|
||||||
log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID)
|
log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||||
l.Bridge.BridgeDie <- true
|
l.Bridge.BridgeDie <- true
|
||||||
return
|
return
|
||||||
@ -107,6 +118,10 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
|||||||
|
|
||||||
if strings.HasPrefix(m.Content, prefix+" refresh") {
|
if strings.HasPrefix(m.Content, prefix+" refresh") {
|
||||||
// Look for the message sender in that guild's current voice states.
|
// Look for the message sender in that guild's current voice states.
|
||||||
|
if !l.Bridge.Connected {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Bridge is not currently running")
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, vs := range g.VoiceStates {
|
for _, vs := range g.VoiceStates {
|
||||||
if vs.UserID == m.Author.ID {
|
if vs.UserID == m.Author.ID {
|
||||||
log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID)
|
log.Printf("Trying to refresh GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||||
@ -122,10 +137,14 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
|||||||
|
|
||||||
if strings.HasPrefix(m.Content, prefix+" auto") {
|
if strings.HasPrefix(m.Content, prefix+" auto") {
|
||||||
if l.Bridge.Mode != bridgeModeAuto {
|
if l.Bridge.Mode != bridgeModeAuto {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Auto mode enabled")
|
||||||
l.Bridge.Mode = bridgeModeAuto
|
l.Bridge.Mode = bridgeModeAuto
|
||||||
|
l.Bridge.DiscordChannelID = l.Bridge.BridgeConfig.CID
|
||||||
l.Bridge.AutoChanDie = make(chan bool)
|
l.Bridge.AutoChanDie = make(chan bool)
|
||||||
go l.Bridge.AutoBridge()
|
go l.Bridge.AutoBridge()
|
||||||
} else {
|
} else {
|
||||||
|
l.Bridge.DiscordSession.ChannelMessageSend(m.ChannelID, "Auto mode disabled")
|
||||||
|
l.Bridge.DiscordChannelID = ""
|
||||||
l.Bridge.AutoChanDie <- true
|
l.Bridge.AutoChanDie <- true
|
||||||
l.Bridge.Mode = bridgeModeManual
|
l.Bridge.Mode = bridgeModeManual
|
||||||
}
|
}
|
||||||
@ -152,7 +171,7 @@ func (l *DiscordListener) voiceUpdate(s *discordgo.Session, event *discordgo.Voi
|
|||||||
|
|
||||||
// Sync the channel voice states to the local discordUsersMap
|
// Sync the channel voice states to the local discordUsersMap
|
||||||
for _, vs := range g.VoiceStates {
|
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 {
|
if s.State.User.ID == vs.UserID {
|
||||||
// Ignore bot
|
// Ignore bot
|
||||||
continue
|
continue
|
||||||
|
7
main.go
7
main.go
@ -165,6 +165,7 @@ func main() {
|
|||||||
log.Println("bridge starting in automatic mode")
|
log.Println("bridge starting in automatic mode")
|
||||||
Bridge.AutoChanDie = make(chan bool)
|
Bridge.AutoChanDie = make(chan bool)
|
||||||
Bridge.Mode = bridgeModeAuto
|
Bridge.Mode = bridgeModeAuto
|
||||||
|
Bridge.DiscordChannelID = Bridge.BridgeConfig.CID
|
||||||
go Bridge.AutoBridge()
|
go Bridge.AutoBridge()
|
||||||
case "manual":
|
case "manual":
|
||||||
log.Println("bridge starting in manual mode")
|
log.Println("bridge starting in manual mode")
|
||||||
@ -172,6 +173,7 @@ func main() {
|
|||||||
case "constant":
|
case "constant":
|
||||||
log.Println("bridge starting in constant mode")
|
log.Println("bridge starting in constant mode")
|
||||||
Bridge.Mode = bridgeModeConstant
|
Bridge.Mode = bridgeModeConstant
|
||||||
|
Bridge.DiscordChannelID = Bridge.BridgeConfig.CID
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
Bridge.startBridge()
|
Bridge.startBridge()
|
||||||
@ -190,13 +192,12 @@ func main() {
|
|||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||||
<-sc
|
<-sc
|
||||||
|
|
||||||
// Signal the bridge to exit cleanly
|
|
||||||
Bridge.BridgeDie <- true
|
|
||||||
|
|
||||||
log.Println("OS Signal. Bot shutting down")
|
log.Println("OS Signal. Bot shutting down")
|
||||||
|
|
||||||
// Wait or the bridge to exit cleanly
|
// Wait or the bridge to exit cleanly
|
||||||
if Bridge.Connected {
|
if Bridge.Connected {
|
||||||
|
//TODO BridgeDie occasionally panics on send to closed channel
|
||||||
|
Bridge.BridgeDie <- true
|
||||||
Bridge.WaitExit.Wait()
|
Bridge.WaitExit.Wait()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user