go: Improve performance by using discordgo caching

master
Luther Wen Xu 2019-10-15 19:06:42 +07:00
parent b788849469
commit 0477b55885
Signed by: 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")
func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
initial, err := s.GuildMembers(config.GuildID, "", 1000)
if err != nil {
return nil, err
for _, guild := range s.State.Guilds {
if guild.ID == config.GuildID {
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) {

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