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")
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)
}
if err != nil {
return nil, err
}
return nil, ErrMemberNotFound
return guild.Members, nil
}
func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {

@ -9,10 +9,9 @@ import (
"TerraOceanBot/server"
)
func UpdateVoiceChannelState(s *discordgo.Session) {
for _, guild := range s.State.Guilds {
func UpdateVoiceChannelState(s *discordgo.Session, guild *discordgo.GuildCreate) {
if guild.ID != config.GuildID {
continue
return
}
for _, vs := range guild.VoiceStates {
if vs.ChannelID != config.VoiceChannel {
@ -26,12 +25,10 @@ func UpdateVoiceChannelState(s *discordgo.Session) {
continue
}
if err != nil {
message.AuditError(s, "", err)
continue
}
server.ConnectUser(username)
}
}
}
func VoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceState) {

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