From 1da6ceb136eebdfbe048e9835f939b4bcd84232f Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Fri, 18 Oct 2019 12:20:11 +0800 Subject: [PATCH] go: discord: Rely less on state and more on events --- GoBot/discord/backend/members.go | 12 +++++----- GoBot/discord/backend/plugin.go | 35 ++++++++++++++---------------- GoBot/discord/discord.go | 2 +- GoBot/discord/modules/minecraft.go | 6 +++-- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/GoBot/discord/backend/members.go b/GoBot/discord/backend/members.go index 754b6e2..1e1c182 100644 --- a/GoBot/discord/backend/members.go +++ b/GoBot/discord/backend/members.go @@ -13,12 +13,14 @@ import ( var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found") func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) { - for _, guild := range s.State.Guilds { - if guild.ID == config.GuildID { - return guild.Members, nil - } + guild, err := s.State.Guild(config.GuildID) + if err != nil { + guild, err = s.Guild(config.GuildID) } - return nil, ErrMemberNotFound + if err != nil { + return nil, err + } + return guild.Members, nil } func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) { diff --git a/GoBot/discord/backend/plugin.go b/GoBot/discord/backend/plugin.go index 760f680..6d47d1e 100644 --- a/GoBot/discord/backend/plugin.go +++ b/GoBot/discord/backend/plugin.go @@ -9,28 +9,25 @@ import ( "TerraOceanBot/server" ) -func UpdateVoiceChannelState(s *discordgo.Session) { - for _, guild := range s.State.Guilds { - if guild.ID != config.GuildID { +func UpdateVoiceChannelState(s *discordgo.Session, guild *discordgo.GuildCreate) { + if guild.ID != config.GuildID { + return + } + for _, vs := range guild.VoiceStates { + if vs.ChannelID != config.VoiceChannel { + continue + } + if vs.Deaf || vs.SelfDeaf { continue } - for _, vs := range guild.VoiceStates { - if vs.ChannelID != config.VoiceChannel { - continue - } - if vs.Deaf || vs.SelfDeaf { - continue - } - username, err := db.GetMinecraftUsername(vs.UserID) - if err == db.ErrNotFound { - continue - } - if err != nil { - message.AuditError(s, "", err) - continue - } - server.ConnectUser(username) + username, err := db.GetMinecraftUsername(vs.UserID) + if err == db.ErrNotFound { + continue + } + if err != nil { + continue } + server.ConnectUser(username) } } diff --git a/GoBot/discord/discord.go b/GoBot/discord/discord.go index 1451931..057e9e2 100644 --- a/GoBot/discord/discord.go +++ b/GoBot/discord/discord.go @@ -20,6 +20,7 @@ func StartBot(token string, kill chan bool) { dg.AddHandler(ProcessVoiceStateUpdate) dg.AddHandler(modules.CheckForReact) dg.AddHandler(modules.CheckForVote) + dg.AddHandler(backend.UpdateVoiceChannelState) dg.State.TrackMembers = true dg.State.TrackVoice = true @@ -30,7 +31,6 @@ func StartBot(token string, kill chan bool) { fmt.Println("Bot is now running. Press CTRL-C to exit.") modules.GenerateHelpEmbed() go modules.ListenToVoteFinishes(dg) - go backend.UpdateVoiceChannelState(dg) <-kill diff --git a/GoBot/discord/modules/minecraft.go b/GoBot/discord/modules/minecraft.go index 68c96e8..6548577 100644 --- a/GoBot/discord/modules/minecraft.go +++ b/GoBot/discord/modules/minecraft.go @@ -4,7 +4,6 @@ import ( "github.com/bwmarrin/discordgo" "TerraOceanBot/db" - "TerraOceanBot/discord/backend" "TerraOceanBot/discord/config" "TerraOceanBot/discord/message" "TerraOceanBot/server" @@ -77,11 +76,14 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna return } - backend.UpdateVoiceChannelState(s) if oldErr == nil { server.WhitelistRemove(oldUsername) server.DisconnectUser(oldUsername) } + if err == nil { + server.WhitelistAdd(newUsername) + server.ConnectUser(newUsername) + } message.InitNewEmbed( config.SetMCUsernameSuccessTitle,