From b782efdf048d441ec3d7601eceba7202c372ff23 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 3 Jan 2021 20:48:34 -0500 Subject: [PATCH] keep track of discord users in set --- bridge.go | 21 +++++++++++++++++++++ handlers.go | 28 ++++++++++++++-------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bridge.go b/bridge.go index 571c0cf..170bf8a 100644 --- a/bridge.go +++ b/bridge.go @@ -19,6 +19,7 @@ type BridgeState struct { Connected bool Client *gumble.Client CurrentChannel *gumble.Channel + DiscordUsers map[string]bool MumbleUserCount int DiscordUserCount int AutoChan chan bool @@ -103,6 +104,25 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin } }() + //Setup initial discord state + g, err := discord.State.Guild(discordGID) + Bridge.DiscordUsers = make(map[string]bool) + if err != nil { + log.Println("error finding guild") + panic(err) + } + for _, vs := range g.VoiceStates { + if vs.ChannelID == discordCID { + Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1 + u, err := discord.User(vs.UserID) + if err != nil { + log.Println("error looking up username") + Bridge.DiscordUsers[u.Username] = true + Bridge.CurrentChannel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) + } + } + } + select { case sig := <-c: log.Printf("\nGot %s signal. Terminating Mumble-Bridge\n", sig) @@ -117,6 +137,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin Bridge.Client = nil Bridge.MumbleUserCount = 0 Bridge.DiscordUserCount = 0 + Bridge.DiscordUsers = nil } } diff --git a/handlers.go b/handlers.go index 4c08e39..f926a59 100644 --- a/handlers.go +++ b/handlers.go @@ -139,23 +139,18 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { if event.GuildID == BridgeConf.GID { if event.ChannelID == BridgeConf.CID { - //check to see if this is actually a new user - g, err := s.State.Guild(event.GuildID) - if err != nil { - log.Println("Could not find guild while checking VoiceStateUpdate") - return - } - for _, vs := range g.VoiceStates { - if vs.UserID == event.UserID { - //user is already in channel (and is probably just muting/etc), ignore - return - } - } - log.Println("user joined watched discord channel") + //get user u, err := s.User(event.UserID) if err != nil { log.Printf("error looking up user for uid %v", event.UserID) - } else if Bridge.Connected { + } + //check to see if actually new user + if Bridge.DiscordUsers[u.Username] { + //not actually new user + return + } + log.Println("user joined watched discord channel") + if Bridge.Connected { Bridge.CurrentChannel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) } Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1 @@ -177,6 +172,11 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { } } if Bridge.DiscordUserCount > count { + u, err := s.User(event.UserID) + if err != nil { + log.Printf("error looking up user for uid %v", event.UserID) + } + delete(Bridge.DiscordUsers, u.Username) log.Println("user left watched discord channel") Bridge.DiscordUserCount = count }