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

master
Luther Wen Xu 2019-10-18 12:20:11 +07:00
parent 0bb9092430
commit 1da6ceb136
Signed by: 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")
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) {

@ -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)
}
}

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

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