1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-10-31 20:14:21 +00:00

Completed guilds, added max pagination API

This commit is contained in:
diamondburned (Forefront) 2020-01-06 22:45:29 -08:00
parent 966d14a32f
commit d6ab7b9d52
8 changed files with 113 additions and 31 deletions

View file

@ -75,23 +75,34 @@ func (c *Client) DeleteGuild(guildID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointGuilds+guildID.String()) return c.FastRequest("DELETE", EndpointGuilds+guildID.String())
} }
// Members returns maximum 1000 members. // Members returns members until it reaches max. This function automatically
func (c *Client) Members(guildID discord.Snowflake) ([]discord.Member, error) { // paginates, meaning the normal 1000 limit is handled internally.
func (c *Client) Members(
guildID discord.Snowflake, max uint) ([]discord.Member, error) {
var mems []discord.Member var mems []discord.Member
var after discord.Snowflake = 0 var after discord.Snowflake = 0
for { const hardLimit int = 1000
m, err := c.MembersAfter(guildID, after, 1000)
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
if fetch > max {
fetch = max
}
max -= fetch
m, err := c.MembersAfter(guildID, after, fetch)
if err != nil { if err != nil {
return mems, err return mems, err
} }
mems = append(mems, m...) mems = append(mems, m...)
if len(mems) < 1000 { // There aren't any to fetch, even if this is less than max.
if len(mems) < hardLimit {
break break
} }
after = mems[999].User.ID after = mems[hardLimit-1].User.ID
} }
return mems, nil return mems, nil

View file

@ -12,23 +12,30 @@ import (
// this could get as many as hundred thousands of messages, making a lot of // this could get as many as hundred thousands of messages, making a lot of
// queries. // queries.
func (c *Client) Messages( func (c *Client) Messages(
channelID discord.Snowflake) ([]discord.Message, error) { channelID discord.Snowflake, max uint) ([]discord.Message, error) {
var msgs []discord.Message var msgs []discord.Message
var after discord.Snowflake = 0 var after discord.Snowflake = 0
for { const hardLimit int = 100
m, err := c.messagesRange(channelID, 0, after, 0, 100)
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
if fetch > max {
fetch = max
}
max -= fetch
m, err := c.messagesRange(channelID, 0, after, 0, fetch)
if err != nil { if err != nil {
return msgs, err return msgs, err
} }
msgs = append(msgs, m...) msgs = append(msgs, m...)
if len(m) < 100 { if len(m) < hardLimit {
break break
} }
after = m[99].Author.ID after = m[hardLimit-1].Author.ID
} }
return msgs, nil return msgs, nil

View file

@ -19,23 +19,30 @@ func (c *Client) React(
// Reactions returns all reactions. It will paginate automatically. // Reactions returns all reactions. It will paginate automatically.
func (c *Client) Reactions( func (c *Client) Reactions(
channelID, messageID discord.Snowflake, channelID, messageID discord.Snowflake,
emoji EmojiAPI) ([]discord.User, error) { max uint, emoji EmojiAPI) ([]discord.User, error) {
var users []discord.User var users []discord.User
var after discord.Snowflake = 0 var after discord.Snowflake = 0
for { const hardLimit int = 100
r, err := c.ReactionsRange(channelID, messageID, 0, after, 100, emoji)
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
if fetch > max {
fetch = max
}
max -= fetch
r, err := c.ReactionsRange(channelID, messageID, 0, after, fetch, emoji)
if err != nil { if err != nil {
return users, err return users, err
} }
users = append(users, r...) users = append(users, r...)
if len(r) < 100 { if len(r) < hardLimit {
break break
} }
after = r[99].ID after = r[hardLimit-1].ID
} }
return users, nil return users, nil

View file

@ -33,22 +33,29 @@ func (c *Client) ModifyMe(data ModifySelfData) (*discord.User, error) {
// method may abuse the API by requesting thousands or millions of guilds. For // method may abuse the API by requesting thousands or millions of guilds. For
// lower-level access, usee GuildsRange. Guilds returned have some fields // lower-level access, usee GuildsRange. Guilds returned have some fields
// filled only (ID, Name, Icon, Owner, Permissions). // filled only (ID, Name, Icon, Owner, Permissions).
func (c *Client) Guilds() ([]discord.Guild, error) { func (c *Client) Guilds(max uint) ([]discord.Guild, error) {
var guilds []discord.Guild var guilds []discord.Guild
var after discord.Snowflake = 0 var after discord.Snowflake = 0
for { const hardLimit int = 100
g, err := c.GuildsAfter(after, 100)
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
if fetch > max {
fetch = max
}
max -= fetch
g, err := c.GuildsAfter(after, fetch)
if err != nil { if err != nil {
return guilds, err return guilds, err
} }
guilds = append(guilds, g...) guilds = append(guilds, g...)
if len(g) < 100 { if len(g) < hardLimit {
break break
} }
after = g[99].ID after = g[hardLimit-1].ID
} }
return guilds, nil return guilds, nil
@ -95,4 +102,30 @@ func (c *Client) GuildsRange(
) )
} }
// func (c *Client) func (c *Client) LeaveGuild(guildID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointMe+"/guilds/"+guildID.String())
}
func (c *Client) PrivateChannels() ([]discord.Channel, error) {
var dms []discord.Channel
return dms, c.RequestJSON(&dms, "GET", EndpointMe+"/channels")
}
func (c *Client) CreatePrivateChannel(
recipient discord.Snowflake) (*discord.Channel, error) {
var param struct {
RecipientID discord.Snowflake `json:"recipient_id"`
}
param.RecipientID = recipient
var dm *discord.Channel
return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels",
httputil.WithJSONBody(c, param))
}
// shitty SDK, don't care, PR welcomed
// func (c *Client) CreateGroup(tokens []string, nicks map[])
func (c *Client) UserConnections() ([]discord.Connection, error) {}

View file

@ -98,9 +98,9 @@ type Ban struct {
} }
type Integration struct { type Integration struct {
ID Snowflake `json:"id"` ID Snowflake `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Type IntegrationType `json:"type"` Type Service `json:"type"`
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
Syncing bool `json:"syncing"` Syncing bool `json:"syncing"`

View file

@ -86,9 +86,10 @@ const (
VeryHighVerification VeryHighVerification
) )
type IntegrationType string // Service is used for guild integrations and user connections.
type Service string
const ( const (
TwitchIntegration IntegrationType = "twitch" Twitch Service = "twitch"
YouTubeIntegration IntegrationType = "youtube" YouTube Service = "youtube"
) )

View file

@ -114,9 +114,9 @@ func (e *Embed) Validate() error {
type EmbedType string type EmbedType string
const ( const (
NormalEmbed = "rich" NormalEmbed EmbedType = "rich"
ImageEmbed = "image" ImageEmbed EmbedType = "image"
VideoEmbed = "video" VideoEmbed EmbedType = "video"
// Undocumented // Undocumented
) )

View file

@ -45,3 +45,26 @@ const (
NitroClassic NitroClassic
NitroFull NitroFull
) )
type Connection struct {
ID Snowflake `json:"id"`
Name string `json:"name"`
Type Service `json:"type"`
Revoked bool `json:"revoked"`
Verified bool `json:"verified"`
FriendSync bool `json:"friend_sync"`
ShowActivity bool `json:"show_activity"`
Visibility ConnectionVisibility `json:"visibility"`
// Only partial
Integratioons []Integration `json:"integrations"`
}
type ConnectionVisibility uint8
const (
ConnectionNotVisible ConnectionVisibility = iota
ConnectionVisibleEveryone
)