go: Improve performance by using discordgo caching

Luther Wen Xu 2019-10-15 19:06:42 +07:00
parent b788849469
commit 0477b55885
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
2 changed files with 5 additions and 16 deletions

@ -12,24 +12,12 @@ 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) {
initial, err := s.GuildMembers(config.GuildID, "", 1000) for _, guild := range s.State.Guilds {
if err != nil { if guild.ID == config.GuildID {
return nil, err return guild.Members, nil
}
next := initial[len(initial)-1].User.ID
for next != "" {
nextArray, err := s.GuildMembers(config.GuildID, next, 1000)
if err != nil {
return nil, err
}
if len(nextArray) == 0 {
next = ""
} else {
next = nextArray[len(nextArray)-1].User.ID
initial = append(initial, nextArray...)
} }
} }
return initial, nil return nil, ErrMemberNotFound
} }
func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) { func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {

@ -19,6 +19,7 @@ func StartBot(token string, kill chan bool) {
dg.AddHandler(ProcessCommand) dg.AddHandler(ProcessCommand)
dg.AddHandler(modules.CheckForReact) dg.AddHandler(modules.CheckForReact)
dg.AddHandler(modules.CheckForVote) dg.AddHandler(modules.CheckForVote)
dg.StateEnabled = true
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
panic(err) panic(err)