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)
} }
if err != nil {
return nil, err
} }
return nil, ErrMemberNotFound 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,10 +9,9 @@ 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 {
continue return
} }
for _, vs := range guild.VoiceStates { for _, vs := range guild.VoiceStates {
if vs.ChannelID != config.VoiceChannel { if vs.ChannelID != config.VoiceChannel {
@ -26,12 +25,10 @@ func UpdateVoiceChannelState(s *discordgo.Session) {
continue continue
} }
if err != nil { if err != nil {
message.AuditError(s, "", err)
continue continue
} }
server.ConnectUser(username) server.ConnectUser(username)
} }
}
} }
func VoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceState) { func VoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceState) {

@ -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,