mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-12-07 04:32:08 +00:00
Finished state
This commit is contained in:
parent
5b25f0cdb4
commit
958f592cb4
|
|
@ -52,10 +52,12 @@ func (c *Client) Guilds(max uint) ([]discord.Guild, error) {
|
||||||
const hardLimit int = 100
|
const hardLimit int = 100
|
||||||
|
|
||||||
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
||||||
if fetch > max {
|
if max > 0 {
|
||||||
fetch = max
|
if fetch > max {
|
||||||
|
fetch = max
|
||||||
|
}
|
||||||
|
max -= fetch
|
||||||
}
|
}
|
||||||
max -= fetch
|
|
||||||
|
|
||||||
g, err := c.GuildsAfter(after, fetch)
|
g, err := c.GuildsAfter(after, fetch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,17 @@ import (
|
||||||
"github.com/diamondburned/arikawa/internal/httputil"
|
"github.com/diamondburned/arikawa/internal/httputil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c *Client) Member(
|
||||||
|
guildID, userID discord.Snowflake) (*discord.Member, error) {
|
||||||
|
|
||||||
|
var m *discord.Member
|
||||||
|
return m, c.RequestJSON(&m, "GET",
|
||||||
|
EndpointGuilds+guildID.String()+"/members/"+userID.String())
|
||||||
|
}
|
||||||
|
|
||||||
// Members returns members until it reaches max. This function automatically
|
// Members returns members until it reaches max. This function automatically
|
||||||
// paginates, meaning the normal 1000 limit is handled internally.
|
// paginates, meaning the normal 1000 limit is handled internally. Max can be 0,
|
||||||
|
// in which the function will try and fetch everything.
|
||||||
func (c *Client) Members(
|
func (c *Client) Members(
|
||||||
guildID discord.Snowflake, max uint) ([]discord.Member, error) {
|
guildID discord.Snowflake, max uint) ([]discord.Member, error) {
|
||||||
|
|
||||||
|
|
@ -16,10 +25,12 @@ func (c *Client) Members(
|
||||||
const hardLimit int = 1000
|
const hardLimit int = 1000
|
||||||
|
|
||||||
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
||||||
if fetch > max {
|
if max > 0 {
|
||||||
fetch = max
|
if fetch > max {
|
||||||
|
fetch = max
|
||||||
|
}
|
||||||
|
max -= fetch
|
||||||
}
|
}
|
||||||
max -= fetch
|
|
||||||
|
|
||||||
m, err := c.MembersAfter(guildID, after, fetch)
|
m, err := c.MembersAfter(guildID, after, fetch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,12 @@ func (c *Client) Messages(
|
||||||
const hardLimit int = 100
|
const hardLimit int = 100
|
||||||
|
|
||||||
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
||||||
if fetch > max {
|
if max > 0 {
|
||||||
fetch = max
|
if fetch > max {
|
||||||
|
fetch = max
|
||||||
|
}
|
||||||
|
max -= fetch
|
||||||
}
|
}
|
||||||
max -= fetch
|
|
||||||
|
|
||||||
m, err := c.messagesRange(channelID, 0, after, 0, fetch)
|
m, err := c.messagesRange(channelID, 0, after, 0, fetch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,12 @@ func (c *Client) Reactions(
|
||||||
const hardLimit int = 100
|
const hardLimit int = 100
|
||||||
|
|
||||||
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
||||||
if fetch > max {
|
if max > 0 {
|
||||||
fetch = max
|
if fetch > max {
|
||||||
|
fetch = max
|
||||||
|
}
|
||||||
|
max -= fetch
|
||||||
}
|
}
|
||||||
max -= fetch
|
|
||||||
|
|
||||||
r, err := c.ReactionsRange(channelID, messageID, 0, after, fetch, emoji)
|
r, err := c.ReactionsRange(channelID, messageID, 0, after, fetch, emoji)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
227
state/state.go
227
state/state.go
|
|
@ -8,6 +8,11 @@ import (
|
||||||
"github.com/diamondburned/arikawa/session"
|
"github.com/diamondburned/arikawa/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
MaxFetchMembers = 1000
|
||||||
|
MaxFetchGuilds = 100
|
||||||
|
)
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
*session.Session
|
*session.Session
|
||||||
|
|
||||||
|
|
@ -111,6 +116,228 @@ func (s *State) Channels(guildID discord.Snowflake) ([]discord.Channel, error) {
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
||||||
|
func (s *State) Emoji(
|
||||||
|
guildID, emojiID discord.Snowflake) (*discord.Emoji, error) {
|
||||||
|
|
||||||
|
e, err := s.Store.Emoji(guildID, emojiID)
|
||||||
|
if err == nil {
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
es, err := s.Session.Emojis(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Store.EmojiSet(guildID, es); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range es {
|
||||||
|
if e.ID == emojiID {
|
||||||
|
return &e, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, ErrStoreNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) Emojis(guildID discord.Snowflake) ([]discord.Emoji, error) {
|
||||||
|
e, err := s.Store.Emojis(guildID)
|
||||||
|
if err == nil {
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
es, err := s.Session.Emojis(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return es, s.Store.EmojiSet(guildID, es)
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
func (s *State) Guild(id discord.Snowflake) (*discord.Guild, error) {
|
||||||
|
c, err := s.Store.Guild(id)
|
||||||
|
if err == nil {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err = s.Session.Guild(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, s.Store.GuildSet(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guilds will only fill a maximum of 100 guilds from the API.
|
||||||
|
func (s *State) Guilds() ([]discord.Guild, error) {
|
||||||
|
c, err := s.Store.Guilds()
|
||||||
|
if err == nil {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err = s.Session.Guilds(100)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ch := range c {
|
||||||
|
if err := s.Store.GuildSet(&ch); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
func (s *State) Member(
|
||||||
|
guildID, userID discord.Snowflake) (*discord.Member, error) {
|
||||||
|
|
||||||
|
m, err := s.Store.Member(guildID, userID)
|
||||||
|
if err == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err = s.Session.Member(guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, s.Store.MemberSet(guildID, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Members
|
||||||
|
func (s *State) Members(guildID discord.Snowflake) ([]discord.Member, error) {
|
||||||
|
ms, err := s.Store.Members(guildID)
|
||||||
|
if err == nil {
|
||||||
|
return ms, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ms, err = s.Session.Members(guildID, 1000)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, m := range ms {
|
||||||
|
if err := s.Store.MemberSet(guildID, &m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ms, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
func (s *State) Message(
|
||||||
|
channelID, messageID discord.Snowflake) (*discord.Message, error) {
|
||||||
|
|
||||||
|
m, err := s.Store.Message(channelID, messageID)
|
||||||
|
if err == nil {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err = s.Session.Message(channelID, messageID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, s.Store.MessageSet(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Messages fetches maximum 100 messages from the API, if it has to. There is no
|
||||||
|
// limit if it's from the State storage.
|
||||||
|
func (s *State) Messages(channelID discord.Snowflake) ([]discord.Message, error) {
|
||||||
|
ms, err := s.Store.Messages(channelID)
|
||||||
|
if err == nil {
|
||||||
|
return ms, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ms, err = s.Session.Messages(channelID, 100)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, m := range ms {
|
||||||
|
if err := s.Store.MessageSet(&m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ms, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
func (s *State) Presence(
|
||||||
|
guildID, userID discord.Snowflake) (*discord.Presence, error) {
|
||||||
|
|
||||||
|
return s.Store.Presence(guildID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) Presences(
|
||||||
|
guildID discord.Snowflake) ([]discord.Presence, error) {
|
||||||
|
|
||||||
|
return s.Store.Presences(guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
func (s *State) Role(
|
||||||
|
guildID, roleID discord.Snowflake) (*discord.Role, error) {
|
||||||
|
|
||||||
|
r, err := s.Store.Role(guildID, roleID)
|
||||||
|
if err == nil {
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rs, err := s.Session.Roles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var role *discord.Role
|
||||||
|
|
||||||
|
for _, r := range rs {
|
||||||
|
if r.ID == roleID {
|
||||||
|
role = &r
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.RoleSet(guildID, &r); err != nil {
|
||||||
|
return role, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return role, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) Roles(guildID discord.Snowflake) ([]discord.Role, error) {
|
||||||
|
rs, err := s.Store.Roles(guildID)
|
||||||
|
if err == nil {
|
||||||
|
return rs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rs, err = s.Session.Roles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range rs {
|
||||||
|
if err := s.RoleSet(guildID, &r); err != nil {
|
||||||
|
return rs, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
func (s *State) hookSession() error {
|
func (s *State) hookSession() error {
|
||||||
/*
|
/*
|
||||||
s.unhooker = s.Session.AddHandler(func(iface interface{}) {
|
s.unhooker = s.Session.AddHandler(func(iface interface{}) {
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ type StoreGetter interface {
|
||||||
Message(channelID, messageID discord.Snowflake) (*discord.Message, error)
|
Message(channelID, messageID discord.Snowflake) (*discord.Message, error)
|
||||||
Messages(channelID discord.Snowflake) ([]discord.Message, error)
|
Messages(channelID discord.Snowflake) ([]discord.Message, error)
|
||||||
|
|
||||||
|
// These don't get fetched from the API, it's Gateway only.
|
||||||
Presence(guildID, userID discord.Snowflake) (*discord.Presence, error)
|
Presence(guildID, userID discord.Snowflake) (*discord.Presence, error)
|
||||||
Presences(guildID discord.Snowflake) ([]discord.Presence, error)
|
Presences(guildID discord.Snowflake) ([]discord.Presence, error)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue