forked from chanbakjsd/TerraOceanPlugin
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package backend
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"TerraOceanBot/discord/config"
|
|
)
|
|
|
|
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
|
|
}
|
|
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
|
|
}
|
|
|
|
func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {
|
|
allMembers, err := GetAllMembers(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.HasPrefix(user, "@") {
|
|
user = strings.TrimPrefix(user, "@")
|
|
} else if strings.HasSuffix(user, ">") {
|
|
if strings.HasPrefix(user, "<@!") {
|
|
user = strings.TrimSuffix(strings.TrimPrefix(user, "<@!"), ">")
|
|
} else if strings.HasPrefix(user, "<@") {
|
|
user = strings.TrimSuffix(strings.TrimPrefix(user, "<@"), ">")
|
|
}
|
|
}
|
|
if strings.ContainsRune(user, '#') {
|
|
list := strings.Split(user, "#")
|
|
username := strings.Join(list[:len(list)-1], "#")
|
|
discriminator := list[len(list)-1]
|
|
for _, v := range allMembers {
|
|
if v.User.Username == username && v.User.Discriminator == discriminator {
|
|
return v, nil
|
|
}
|
|
}
|
|
}
|
|
for _, v := range allMembers {
|
|
if v.User.ID == user {
|
|
return v, nil
|
|
}
|
|
}
|
|
for _, v := range allMembers {
|
|
if v.Nick == user {
|
|
return v, nil
|
|
}
|
|
}
|
|
for _, v := range allMembers {
|
|
if v.User.Username == user {
|
|
return v, nil
|
|
}
|
|
}
|
|
return nil, ErrMemberNotFound
|
|
}
|