go: discord: Rely less on state and more on events

Luther Wen Xu 2019-10-18 12:20:11 +07:00
parent 0bb9092430
commit 1da6ceb136
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
4 changed files with 28 additions and 27 deletions

@ -13,12 +13,14 @@ import (
var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found") var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found")
func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) { func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
for _, guild := range s.State.Guilds { guild, err := s.State.Guild(config.GuildID)
if guild.ID == config.GuildID { if err != nil {
return guild.Members, 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) { func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {

@ -9,28 +9,25 @@ import (
"TerraOceanBot/server" "TerraOceanBot/server"
) )
func UpdateVoiceChannelState(s *discordgo.Session) { func UpdateVoiceChannelState(s *discordgo.Session, guild *discordgo.GuildCreate) {
for _, guild := range s.State.Guilds { if guild.ID != config.GuildID {
if guild.ID != config.GuildID { return
}
for _, vs := range guild.VoiceStates {
if vs.ChannelID != config.VoiceChannel {
continue
}
if vs.Deaf || vs.SelfDeaf {
continue continue
} }
for _, vs := range guild.VoiceStates { username, err := db.GetMinecraftUsername(vs.UserID)
if vs.ChannelID != config.VoiceChannel { if err == db.ErrNotFound {
continue continue
} }
if vs.Deaf || vs.SelfDeaf { if err != nil {
continue continue
}
username, err := db.GetMinecraftUsername(vs.UserID)
if err == db.ErrNotFound {
continue
}
if err != nil {
message.AuditError(s, "", err)
continue
}
server.ConnectUser(username)
} }
server.ConnectUser(username)
} }
} }

@ -20,6 +20,7 @@ func StartBot(token string, kill chan bool) {
dg.AddHandler(ProcessVoiceStateUpdate) dg.AddHandler(ProcessVoiceStateUpdate)
dg.AddHandler(modules.CheckForReact) dg.AddHandler(modules.CheckForReact)
dg.AddHandler(modules.CheckForVote) dg.AddHandler(modules.CheckForVote)
dg.AddHandler(backend.UpdateVoiceChannelState)
dg.State.TrackMembers = true dg.State.TrackMembers = true
dg.State.TrackVoice = 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.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
modules.GenerateHelpEmbed() modules.GenerateHelpEmbed()
go modules.ListenToVoteFinishes(dg) go modules.ListenToVoteFinishes(dg)
go backend.UpdateVoiceChannelState(dg)
<-kill <-kill

@ -4,7 +4,6 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"TerraOceanBot/db" "TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config" "TerraOceanBot/discord/config"
"TerraOceanBot/discord/message" "TerraOceanBot/discord/message"
"TerraOceanBot/server" "TerraOceanBot/server"
@ -77,11 +76,14 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna
return return
} }
backend.UpdateVoiceChannelState(s)
if oldErr == nil { if oldErr == nil {
server.WhitelistRemove(oldUsername) server.WhitelistRemove(oldUsername)
server.DisconnectUser(oldUsername) server.DisconnectUser(oldUsername)
} }
if err == nil {
server.WhitelistAdd(newUsername)
server.ConnectUser(newUsername)
}
message.InitNewEmbed( message.InitNewEmbed(
config.SetMCUsernameSuccessTitle, config.SetMCUsernameSuccessTitle,