mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-11-13 19:30:47 -05:00
commit
3267dc5f2d
10
bridge.go
10
bridge.go
@ -70,6 +70,9 @@ type BridgeState struct {
|
||||
// Mumble Duplex and Event Listener
|
||||
MumbleStream *MumbleDuplex
|
||||
MumbleListener *MumbleListener
|
||||
|
||||
// Discord Voice channel to join
|
||||
DiscordChannelID string
|
||||
}
|
||||
|
||||
// startBridge established the voice connection
|
||||
@ -90,7 +93,12 @@ func (b *BridgeState) startBridge() {
|
||||
|
||||
// DISCORD Connect Voice
|
||||
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 {
|
||||
log.Println(err)
|
||||
b.DiscordVoice.Disconnect()
|
||||
|
@ -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
|
||||
@ -61,10 +61,6 @@ func (l *DiscordListener) guildCreate(s *discordgo.Session, event *discordgo.Gui
|
||||
|
||||
func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
if l.Bridge.Mode == bridgeModeConstant {
|
||||
return
|
||||
}
|
||||
|
||||
// Ignore all messages created by the bot itself
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
@ -83,11 +79,22 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
||||
return
|
||||
}
|
||||
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") {
|
||||
// Look for the message sender in that guild's current voice states.
|
||||
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 {
|
||||
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||
l.Bridge.DiscordChannelID = vs.ChannelID
|
||||
go l.Bridge.startBridge()
|
||||
return
|
||||
}
|
||||
@ -96,8 +103,12 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" unlink") {
|
||||
// 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 {
|
||||
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)
|
||||
l.Bridge.BridgeDie <- true
|
||||
return
|
||||
@ -107,6 +118,10 @@ func (l *DiscordListener) messageCreate(s *discordgo.Session, m *discordgo.Messa
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" refresh") {
|
||||
// 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 {
|
||||
if vs.UserID == m.Author.ID {
|
||||
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 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
|
||||
}
|
||||
@ -152,7 +171,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
|
||||
|
7
main.go
7
main.go
@ -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,6 +173,7 @@ func main() {
|
||||
case "constant":
|
||||
log.Println("bridge starting in constant mode")
|
||||
Bridge.Mode = bridgeModeConstant
|
||||
Bridge.DiscordChannelID = Bridge.BridgeConfig.CID
|
||||
go func() {
|
||||
for {
|
||||
Bridge.startBridge()
|
||||
@ -190,13 +192,12 @@ func main() {
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||
<-sc
|
||||
|
||||
// Signal the bridge to exit cleanly
|
||||
Bridge.BridgeDie <- true
|
||||
|
||||
log.Println("OS Signal. Bot shutting down")
|
||||
|
||||
// Wait or the bridge to exit cleanly
|
||||
if Bridge.Connected {
|
||||
//TODO BridgeDie occasionally panics on send to closed channel
|
||||
Bridge.BridgeDie <- true
|
||||
Bridge.WaitExit.Wait()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user