lint cleanup, remove code duplication

This commit is contained in:
stryan 2021-01-08 14:38:21 -05:00
parent 78dca8d9dc
commit 2e83d6d515
6 changed files with 39 additions and 72 deletions

View File

@ -14,10 +14,11 @@ import (
"layeh.com/gumble/gumble" "layeh.com/gumble/gumble"
) )
//BridgeState manages dynamic information about the bridge during runtime
type BridgeState struct { type BridgeState struct {
ActiveConn chan bool ActiveConn chan bool
Connected bool Connected bool
Mode BridgeMode Mode bridgeMode
Client *gumble.Client Client *gumble.Client
DiscordUsers map[string]bool DiscordUsers map[string]bool
MumbleUsers 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) { func AutoBridge(s *discordgo.Session, l *Listener) {
log.Println("beginning auto mode") log.Println("beginning auto mode")
for { for {
@ -177,8 +181,7 @@ func AutoBridge(s *discordgo.Session, l *Listener) {
if l.Bridge.Connected && l.Bridge.MumbleUserCount == 0 && l.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")
l.Bridge.ActiveConn <- true l.Bridge.ActiveConn <- true
MumbleReset() l.Bridge.ActiveConn = nil
DiscordReset()
} }
l.UserCountLock.Unlock() l.UserCountLock.Unlock()
} }

View File

@ -10,14 +10,16 @@ import (
"layeh.com/gumble/gumble" "layeh.com/gumble/gumble"
) )
type BridgeMode int type bridgeMode int
const ( const (
BridgeModeAuto BridgeMode = iota bridgeModeAuto bridgeMode = iota
BridgeModeManual bridgeModeManual
BridgeModeConstant bridgeModeConstant
) )
//BridgeConfig holds configuration information set at startup
//It should not change during runtime
type BridgeConfig struct { type BridgeConfig struct {
Config *gumble.Config Config *gumble.Config
MumbleAddr string MumbleAddr string

View File

@ -22,10 +22,6 @@ var discordMutex sync.Mutex
var discordMixerMutex sync.Mutex var discordMixerMutex sync.Mutex
var fromDiscordMap = make(map[uint32]fromDiscord) var fromDiscordMap = make(map[uint32]fromDiscord)
func DiscordReset() {
fromDiscordMap = make(map[uint32]fromDiscord)
}
// OnError gets called by dgvoice when an error is encountered. // OnError gets called by dgvoice when an error is encountered.
// By default logs to STDERR // By default logs to STDERR
var OnError = func(str string, err error) { var OnError = func(str string, err error) {

View File

@ -11,6 +11,8 @@ import (
"layeh.com/gumble/gumble" "layeh.com/gumble/gumble"
) )
//Listener holds references to the current BridgeConf
//and BridgeState for use by the event handlers
type Listener struct { type Listener struct {
BridgeConf *BridgeConfig BridgeConf *BridgeConfig
Bridge *BridgeState Bridge *BridgeState
@ -22,11 +24,16 @@ func (l *Listener) ready(s *discordgo.Session, event *discordgo.Ready) {
log.Println("READY event registered") log.Println("READY event registered")
//Setup initial discord state //Setup initial discord state
var g *discordgo.Guild var g *discordgo.Guild
g = nil
for _, i := range event.Guilds { for _, i := range event.Guilds {
if i.ID == l.BridgeConf.GID { if i.ID == l.BridgeConf.GID {
g = i g = i
} }
} }
if g == nil {
log.Println("bad guild on READY")
return
}
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.ChannelID == l.BridgeConf.CID { if vs.ChannelID == l.BridgeConf.CID {
l.UserCountLock.Lock() 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) { func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if l.Bridge.Mode == BridgeModeConstant { if l.Bridge.Mode == bridgeModeConstant {
return return
} }
@ -56,23 +63,21 @@ func (l *Listener) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
return return
} }
// 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
}
prefix := "!" + l.BridgeConf.Command prefix := "!" + l.BridgeConf.Command
if strings.HasPrefix(m.Content, prefix+" link") { 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 {
// 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. // Look for the message sender in that guild's current voice states.
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID { 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") { 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. // Look for the message sender in that guild's current voice states.
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 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.ActiveConn <- true l.Bridge.ActiveConn <- true
l.Bridge.ActiveConn = nil l.Bridge.ActiveConn = nil
MumbleReset()
DiscordReset()
return return
} }
} }
} }
if strings.HasPrefix(m.Content, prefix+" refresh") { 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. // Look for the message sender in that guild's current voice states.
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)
l.Bridge.ActiveConn <- true l.Bridge.ActiveConn <- true
MumbleReset()
DiscordReset()
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
l.Bridge.ActiveConn = make(chan bool) l.Bridge.ActiveConn = make(chan bool)
go startBridge(s, g.ID, vs.ChannelID, l, l.Bridge.ActiveConn) 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 strings.HasPrefix(m.Content, prefix+" auto") {
if l.Bridge.Mode != BridgeModeAuto { if l.Bridge.Mode != bridgeModeAuto {
l.Bridge.Mode = BridgeModeAuto l.Bridge.Mode = bridgeModeAuto
l.Bridge.AutoChan = make(chan bool) l.Bridge.AutoChan = make(chan bool)
go AutoBridge(s, l) go AutoBridge(s, l)
} else { } else {
l.Bridge.AutoChan <- true l.Bridge.AutoChan <- true
l.Bridge.Mode = BridgeModeManual l.Bridge.Mode = bridgeModeManual
} }
} }
} }

View File

@ -121,14 +121,14 @@ func main() {
case "auto": case "auto":
log.Println("bridge starting in automatic mode") log.Println("bridge starting in automatic mode")
Bridge.AutoChan = make(chan bool) Bridge.AutoChan = make(chan bool)
Bridge.Mode = BridgeModeAuto Bridge.Mode = bridgeModeAuto
go AutoBridge(discord, l) go AutoBridge(discord, l)
case "manual": case "manual":
log.Println("bridge starting in manual mode") log.Println("bridge starting in manual mode")
Bridge.Mode = BridgeModeManual Bridge.Mode = bridgeModeManual
case "constant": case "constant":
log.Println("bridge starting in constant mode") log.Println("bridge starting in constant mode")
Bridge.Mode = BridgeModeConstant Bridge.Mode = bridgeModeConstant
go startBridge(discord, *discordGID, *discordCID, l, make(chan bool)) go startBridge(discord, *discordGID, *discordCID, l, make(chan bool))
default: default:
discord.Close() discord.Close()

View File

@ -18,11 +18,6 @@ type MumbleDuplex struct {
Close chan bool Close chan bool
} }
func MumbleReset() {
fromMumbleArr = []chan gumble.AudioBuffer{}
mumbleStreamingArr = []bool{}
}
// OnAudioStream - Spawn routines to handle incoming packets // OnAudioStream - Spawn routines to handle incoming packets
func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) { func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {