mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-11-22 21:35:44 -05:00
lint cleanup, remove code duplication
This commit is contained in:
parent
78dca8d9dc
commit
2e83d6d515
@ -14,10 +14,11 @@ import (
|
||||
"layeh.com/gumble/gumble"
|
||||
)
|
||||
|
||||
//BridgeState manages dynamic information about the bridge during runtime
|
||||
type BridgeState struct {
|
||||
ActiveConn chan bool
|
||||
Connected bool
|
||||
Mode BridgeMode
|
||||
Mode bridgeMode
|
||||
Client *gumble.Client
|
||||
DiscordUsers map[string]bool
|
||||
MumbleUsers map[string]bool
|
||||
@ -157,6 +158,9 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string, l *Listener)
|
||||
}
|
||||
}
|
||||
|
||||
//AutoBridge starts a goroutine to check the number of users in discord and mumble
|
||||
//when there is at least one user on both, starts up the bridge
|
||||
//when there are no users on either side, kills the bridge
|
||||
func AutoBridge(s *discordgo.Session, l *Listener) {
|
||||
log.Println("beginning auto mode")
|
||||
for {
|
||||
@ -177,8 +181,7 @@ func AutoBridge(s *discordgo.Session, l *Listener) {
|
||||
if l.Bridge.Connected && l.Bridge.MumbleUserCount == 0 && l.Bridge.DiscordUserCount <= 1 {
|
||||
log.Println("no one online, killing bridge")
|
||||
l.Bridge.ActiveConn <- true
|
||||
MumbleReset()
|
||||
DiscordReset()
|
||||
l.Bridge.ActiveConn = nil
|
||||
}
|
||||
l.UserCountLock.Unlock()
|
||||
}
|
||||
|
10
config.go
10
config.go
@ -10,14 +10,16 @@ import (
|
||||
"layeh.com/gumble/gumble"
|
||||
)
|
||||
|
||||
type BridgeMode int
|
||||
type bridgeMode int
|
||||
|
||||
const (
|
||||
BridgeModeAuto BridgeMode = iota
|
||||
BridgeModeManual
|
||||
BridgeModeConstant
|
||||
bridgeModeAuto bridgeMode = iota
|
||||
bridgeModeManual
|
||||
bridgeModeConstant
|
||||
)
|
||||
|
||||
//BridgeConfig holds configuration information set at startup
|
||||
//It should not change during runtime
|
||||
type BridgeConfig struct {
|
||||
Config *gumble.Config
|
||||
MumbleAddr string
|
||||
|
@ -22,10 +22,6 @@ var discordMutex sync.Mutex
|
||||
var discordMixerMutex sync.Mutex
|
||||
var fromDiscordMap = make(map[uint32]fromDiscord)
|
||||
|
||||
func DiscordReset() {
|
||||
fromDiscordMap = make(map[uint32]fromDiscord)
|
||||
}
|
||||
|
||||
// OnError gets called by dgvoice when an error is encountered.
|
||||
// By default logs to STDERR
|
||||
var OnError = func(str string, err error) {
|
||||
|
55
handlers.go
55
handlers.go
@ -11,6 +11,8 @@ import (
|
||||
"layeh.com/gumble/gumble"
|
||||
)
|
||||
|
||||
//Listener holds references to the current BridgeConf
|
||||
//and BridgeState for use by the event handlers
|
||||
type Listener struct {
|
||||
BridgeConf *BridgeConfig
|
||||
Bridge *BridgeState
|
||||
@ -22,11 +24,16 @@ func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||
log.Println("READY event registered")
|
||||
//Setup initial discord state
|
||||
var g *discordgo.Guild
|
||||
g = nil
|
||||
for _, i := range event.Guilds {
|
||||
if i.ID == l.BridgeConf.GID {
|
||||
g = i
|
||||
}
|
||||
}
|
||||
if g == nil {
|
||||
log.Println("bad guild on READY")
|
||||
return
|
||||
}
|
||||
for _, vs := range g.VoiceStates {
|
||||
if vs.ChannelID == l.BridgeConf.CID {
|
||||
l.UserCountLock.Lock()
|
||||
@ -48,7 +55,7 @@ func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||
|
||||
func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
if l.Bridge.Mode == BridgeModeConstant {
|
||||
if l.Bridge.Mode == bridgeModeConstant {
|
||||
return
|
||||
}
|
||||
|
||||
@ -56,9 +63,6 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
prefix := "!" + l.BridgeConf.Command
|
||||
if strings.HasPrefix(m.Content, prefix+" link") {
|
||||
|
||||
// Find the channel that the message came from.
|
||||
c, err := s.State.Channel(m.ChannelID)
|
||||
if err != nil {
|
||||
@ -72,7 +76,8 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
||||
// Could not find guild.
|
||||
return
|
||||
}
|
||||
|
||||
prefix := "!" + l.BridgeConf.Command
|
||||
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 vs.UserID == m.Author.ID {
|
||||
@ -86,57 +91,23 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" unlink") {
|
||||
|
||||
// Find the channel that the message came from.
|
||||
c, err := s.State.Channel(m.ChannelID)
|
||||
if err != nil {
|
||||
// Could not find channel.
|
||||
return
|
||||
}
|
||||
|
||||
// Find the guild for that channel.
|
||||
g, err := s.State.Guild(c.GuildID)
|
||||
if err != nil {
|
||||
// Could not find guild.
|
||||
return
|
||||
}
|
||||
|
||||
// Look for the message sender in that guild's current voice states.
|
||||
for _, vs := range g.VoiceStates {
|
||||
if vs.UserID == m.Author.ID {
|
||||
log.Printf("Trying to leave GID %v and VID %v\n", g.ID, vs.ChannelID)
|
||||
l.Bridge.ActiveConn <- true
|
||||
l.Bridge.ActiveConn = nil
|
||||
MumbleReset()
|
||||
DiscordReset()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" refresh") {
|
||||
|
||||
// Find the channel that the message came from.
|
||||
c, err := s.State.Channel(m.ChannelID)
|
||||
if err != nil {
|
||||
// Could not find channel.
|
||||
return
|
||||
}
|
||||
|
||||
// Find the guild for that channel.
|
||||
g, err := s.State.Guild(c.GuildID)
|
||||
if err != nil {
|
||||
// Could not find guild.
|
||||
return
|
||||
}
|
||||
|
||||
// Look for the message sender in that guild's current voice states.
|
||||
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)
|
||||
l.Bridge.ActiveConn <- true
|
||||
MumbleReset()
|
||||
DiscordReset()
|
||||
time.Sleep(5 * time.Second)
|
||||
l.Bridge.ActiveConn = make(chan bool)
|
||||
go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn)
|
||||
@ -146,13 +117,13 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, prefix+" auto") {
|
||||
if l.Bridge.Mode != BridgeModeAuto {
|
||||
l.Bridge.Mode = BridgeModeAuto
|
||||
if l.Bridge.Mode != bridgeModeAuto {
|
||||
l.Bridge.Mode = bridgeModeAuto
|
||||
l.Bridge.AutoChan = make(chan bool)
|
||||
go AutoBridge(s, l)
|
||||
} else {
|
||||
l.Bridge.AutoChan <- true
|
||||
l.Bridge.Mode = BridgeModeManual
|
||||
l.Bridge.Mode = bridgeModeManual
|
||||
}
|
||||
}
|
||||
}
|
||||
|
6
main.go
6
main.go
@ -121,14 +121,14 @@ func main() {
|
||||
case "auto":
|
||||
log.Println("bridge starting in automatic mode")
|
||||
Bridge.AutoChan = make(chan bool)
|
||||
Bridge.Mode = BridgeModeAuto
|
||||
Bridge.Mode = bridgeModeAuto
|
||||
go AutoBridge(discord, l)
|
||||
case "manual":
|
||||
log.Println("bridge starting in manual mode")
|
||||
Bridge.Mode = BridgeModeManual
|
||||
Bridge.Mode = bridgeModeManual
|
||||
case "constant":
|
||||
log.Println("bridge starting in constant mode")
|
||||
Bridge.Mode = BridgeModeConstant
|
||||
Bridge.Mode = bridgeModeConstant
|
||||
go startBridge(discord, *discordGID, *discordCID, l, make(chan bool))
|
||||
default:
|
||||
discord.Close()
|
||||
|
@ -18,11 +18,6 @@ type MumbleDuplex struct {
|
||||
Close chan bool
|
||||
}
|
||||
|
||||
func MumbleReset() {
|
||||
fromMumbleArr = []chan gumble.AudioBuffer{}
|
||||
mumbleStreamingArr = []bool{}
|
||||
}
|
||||
|
||||
// OnAudioStream - Spawn routines to handle incoming packets
|
||||
func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user