59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
|
package rest
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
const memberEndpoint = "https://discord.com/api/v9/guilds/%s/members?after=%s"
|
||
|
|
||
|
func Members(guildID, token string) ([]Member, error) {
|
||
|
var mems []Member
|
||
|
|
||
|
after := "0"
|
||
|
for {
|
||
|
m, err := members(guildID, token, after)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
mems = append(mems, m...)
|
||
|
if len(mems) < 1000 {
|
||
|
break
|
||
|
}
|
||
|
after = mems[len(mems)-1].User.ID
|
||
|
}
|
||
|
|
||
|
return mems, nil
|
||
|
}
|
||
|
|
||
|
func members(guildID, token, after string) ([]Member, error) {
|
||
|
var members []Member
|
||
|
|
||
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(memberEndpoint, guildID, after), nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
req.Header.Set("Authorization", "Bot "+token)
|
||
|
|
||
|
resp, err := http.DefaultClient.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if err := json.NewDecoder(resp.Body).Decode(&members); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return members, nil
|
||
|
}
|
||
|
|
||
|
type Member struct {
|
||
|
Nick string `json:"nick"`
|
||
|
User struct {
|
||
|
ID string `json:"id"`
|
||
|
Username string `json:"username"`
|
||
|
} `json:"user"`
|
||
|
}
|