keep track of discord users in set

This commit is contained in:
stryan 2021-01-03 20:48:34 -05:00
parent 8257bf55ab
commit b782efdf04
2 changed files with 35 additions and 14 deletions

View File

@ -19,6 +19,7 @@ type BridgeState struct {
Connected bool Connected bool
Client *gumble.Client Client *gumble.Client
CurrentChannel *gumble.Channel CurrentChannel *gumble.Channel
DiscordUsers map[string]bool
MumbleUserCount int MumbleUserCount int
DiscordUserCount int DiscordUserCount int
AutoChan chan bool 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 { select {
case sig := <-c: case sig := <-c:
log.Printf("\nGot %s signal. Terminating Mumble-Bridge\n", sig) 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.Client = nil
Bridge.MumbleUserCount = 0 Bridge.MumbleUserCount = 0
Bridge.DiscordUserCount = 0 Bridge.DiscordUserCount = 0
Bridge.DiscordUsers = nil
} }
} }

View File

@ -139,23 +139,18 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
if event.GuildID == BridgeConf.GID { if event.GuildID == BridgeConf.GID {
if event.ChannelID == BridgeConf.CID { if event.ChannelID == BridgeConf.CID {
//check to see if this is actually a new user //get 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")
u, err := s.User(event.UserID) u, err := s.User(event.UserID)
if err != nil { if err != nil {
log.Printf("error looking up user for uid %v", event.UserID) 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.CurrentChannel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
} }
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1 Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1
@ -177,6 +172,11 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
} }
} }
if Bridge.DiscordUserCount > count { 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") log.Println("user left watched discord channel")
Bridge.DiscordUserCount = count Bridge.DiscordUserCount = count
} }