2020-01-18 07:45:44 +00:00
|
|
|
// Package state provides interfaces for a local or remote state, as well as
|
|
|
|
// abstractions around the REST API and Gateway events.
|
2020-01-17 05:17:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-05-03 21:02:03 +00:00
|
|
|
"context"
|
2020-01-20 08:53:23 +00:00
|
|
|
"sync"
|
2020-01-18 21:04:12 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v2/internal/moreatomic"
|
|
|
|
"github.com/diamondburned/arikawa/v2/session"
|
|
|
|
"github.com/diamondburned/arikawa/v2/utils/handler"
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-17 05:17:46 +00:00
|
|
|
)
|
|
|
|
|
2020-01-18 19:34:08 +00:00
|
|
|
var (
|
2020-01-19 21:54:16 +00:00
|
|
|
MaxFetchMembers uint = 1000
|
2020-07-28 19:00:01 +00:00
|
|
|
MaxFetchGuilds uint = 10
|
2020-01-18 19:34:08 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 07:33:22 +00:00
|
|
|
// State is the cache to store events coming from Discord as well as data from
|
|
|
|
// API calls.
|
|
|
|
//
|
|
|
|
// Store
|
|
|
|
//
|
|
|
|
// The state basically provides abstractions on top of the API and the state
|
|
|
|
// storage (Store). The state storage is effectively a set of interfaces which
|
|
|
|
// allow arbitrary backends to be implemented.
|
|
|
|
//
|
|
|
|
// The default storage backend is a typical in-memory structure consisting of
|
|
|
|
// maps and slices. Custom backend implementations could embed this storage
|
|
|
|
// backend as an in-memory fallback. A good example of this would be embedding
|
|
|
|
// the default store for messages only, while handling everything else in Redis.
|
|
|
|
//
|
|
|
|
// The package also provides a no-op store (NoopStore) that implementations
|
|
|
|
// could embed. This no-op store will always return an error, which makes the
|
|
|
|
// state fetch information from the API. The setters are all no-ops, so the
|
|
|
|
// fetched data won't be updated.
|
|
|
|
//
|
|
|
|
// Handler
|
|
|
|
//
|
|
|
|
// The state uses its own handler over session's to make all handlers run after
|
|
|
|
// the state updates itself. A PreHandler is exposed in any case the user needs
|
|
|
|
// the handlers to run before the state updates itself. Refer to that field's
|
|
|
|
// documentation.
|
|
|
|
//
|
|
|
|
// The state also provides extra events and overrides to make up for Discord's
|
|
|
|
// inconsistencies in data. The following are known instances of such.
|
|
|
|
//
|
|
|
|
// The Guild Create event is split up to make the state's Guild Available, Guild
|
|
|
|
// Ready and Guild Join events. Refer to these events' documentations for more
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
// The Message Create and Message Update events with the Member field provided
|
|
|
|
// will have the User field copied from Author. This is because the User field
|
|
|
|
// will be empty, while the Member structure expects it to be there.
|
2020-01-17 05:17:46 +00:00
|
|
|
type State struct {
|
|
|
|
*session.Session
|
2020-01-18 21:04:12 +00:00
|
|
|
Store
|
|
|
|
|
2020-01-18 21:40:09 +00:00
|
|
|
// *: State doesn't actually keep track of pinned messages.
|
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
// Ready is not updated by the state.
|
|
|
|
Ready gateway.ReadyEvent
|
|
|
|
|
2020-01-20 09:06:47 +00:00
|
|
|
// StateLog logs all errors that come from the state cache. This includes
|
|
|
|
// not found errors. Defaults to a no-op, as state errors aren't that
|
|
|
|
// important.
|
|
|
|
StateLog func(error)
|
2020-01-17 05:17:46 +00:00
|
|
|
|
|
|
|
// PreHandler is the manual hook that is executed before the State handler
|
|
|
|
// is. This should only be used for low-level operations.
|
|
|
|
// It's recommended to set Synchronous to true if you mutate the events.
|
2020-01-18 07:07:52 +00:00
|
|
|
PreHandler *handler.Handler // default nil
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-02-25 05:50:13 +00:00
|
|
|
// Command handler with inherited methods. Ran after PreHandler. You should
|
|
|
|
// most of the time use this instead of Session's, to avoid race conditions
|
2020-06-19 07:33:22 +00:00
|
|
|
// with the State.
|
2020-02-25 05:50:13 +00:00
|
|
|
*handler.Handler
|
|
|
|
|
2020-01-20 08:53:23 +00:00
|
|
|
// List of channels with few messages, so it doesn't bother hitting the API
|
|
|
|
// again.
|
2020-07-21 20:27:59 +00:00
|
|
|
fewMessages map[discord.ChannelID]struct{}
|
2020-07-29 23:29:01 +00:00
|
|
|
fewMutex *sync.Mutex
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
// unavailableGuilds is a set of discord.GuildIDs of guilds that became
|
2020-06-06 20:47:15 +00:00
|
|
|
// unavailable when already connected to the gateway, i.e. sent in a
|
|
|
|
// GuildUnavailableEvent.
|
2020-07-21 20:27:59 +00:00
|
|
|
unavailableGuilds *moreatomic.GuildIDSet
|
|
|
|
// unreadyGuilds is a set of discord.GuildIDs of guilds that were
|
2020-06-06 20:47:15 +00:00
|
|
|
// unavailable when connecting to the gateway, i.e. they had Unavailable
|
|
|
|
// set to true during Ready.
|
2020-07-21 20:27:59 +00:00
|
|
|
unreadyGuilds *moreatomic.GuildIDSet
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// New creates a new state.
|
2020-01-18 07:07:52 +00:00
|
|
|
func New(token string) (*State, error) {
|
2020-01-25 05:24:33 +00:00
|
|
|
return NewWithStore(token, NewDefaultStore(nil))
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// NewWithIntents creates a new state with the given gateway intents. For more
|
|
|
|
// information, refer to gateway.Intents.
|
|
|
|
func NewWithIntents(token string, intents ...gateway.Intents) (*State, error) {
|
|
|
|
s, err := session.NewWithIntents(token, intents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewFromSession(s, NewDefaultStore(nil))
|
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
func NewWithStore(token string, store Store) (*State, error) {
|
|
|
|
s, err := session.New(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:04:12 +00:00
|
|
|
return NewFromSession(s, store)
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 07:59:44 +00:00
|
|
|
// NewFromSession never returns an error. This API is kept for backwards
|
|
|
|
// compatibility.
|
2020-05-03 21:02:03 +00:00
|
|
|
func NewFromSession(s *session.Session, store Store) (*State, error) {
|
|
|
|
state := &State{
|
2020-06-06 20:47:15 +00:00
|
|
|
Session: s,
|
|
|
|
Store: store,
|
|
|
|
Handler: handler.New(),
|
|
|
|
StateLog: func(err error) {},
|
2020-07-21 20:27:59 +00:00
|
|
|
fewMessages: map[discord.ChannelID]struct{}{},
|
2020-07-29 23:29:01 +00:00
|
|
|
fewMutex: new(sync.Mutex),
|
2020-07-21 20:27:59 +00:00
|
|
|
unavailableGuilds: moreatomic.NewGuildIDSet(),
|
|
|
|
unreadyGuilds: moreatomic.NewGuildIDSet(),
|
2020-05-03 21:02:03 +00:00
|
|
|
}
|
2020-06-19 07:59:44 +00:00
|
|
|
state.hookSession()
|
|
|
|
return state, nil
|
2020-05-03 21:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 21:04:09 +00:00
|
|
|
// WithContext returns a shallow copy of State with the context replaced in the
|
|
|
|
// API client. All methods called on the State will use this given context. This
|
|
|
|
// method is thread-safe.
|
2020-05-03 21:02:03 +00:00
|
|
|
func (s *State) WithContext(ctx context.Context) *State {
|
|
|
|
copied := *s
|
|
|
|
copied.Client = copied.Client.WithContext(ctx)
|
|
|
|
|
|
|
|
return &copied
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 01:06:51 +00:00
|
|
|
//// Helper methods
|
|
|
|
|
2020-04-12 23:24:28 +00:00
|
|
|
func (s *State) AuthorDisplayName(message *gateway.MessageCreateEvent) string {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !message.GuildID.IsValid() {
|
2020-01-21 01:06:51 +00:00
|
|
|
return message.Author.Username
|
|
|
|
}
|
|
|
|
|
2020-02-13 04:19:24 +00:00
|
|
|
if message.Member != nil {
|
|
|
|
if message.Member.Nick != "" {
|
|
|
|
return message.Member.Nick
|
|
|
|
}
|
|
|
|
return message.Author.Username
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:06:51 +00:00
|
|
|
n, err := s.MemberDisplayName(message.GuildID, message.Author.ID)
|
|
|
|
if err != nil {
|
|
|
|
return message.Author.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) MemberDisplayName(guildID discord.GuildID, userID discord.UserID) (string, error) {
|
2020-01-21 01:06:51 +00:00
|
|
|
member, err := s.Member(guildID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if member.Nick == "" {
|
|
|
|
return member.User.Username, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return member.Nick, nil
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:13:40 +00:00
|
|
|
func (s *State) AuthorColor(message *gateway.MessageCreateEvent) (discord.Color, error) {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !message.GuildID.IsValid() { // this is a dm
|
2020-05-16 20:36:46 +00:00
|
|
|
return discord.DefaultMemberColor, nil
|
2020-01-21 01:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 04:19:24 +00:00
|
|
|
if message.Member != nil {
|
|
|
|
guild, err := s.Guild(message.GuildID)
|
|
|
|
if err != nil {
|
2020-05-16 20:36:46 +00:00
|
|
|
return 0, err
|
2020-02-13 04:19:24 +00:00
|
|
|
}
|
2020-05-16 20:13:40 +00:00
|
|
|
return discord.MemberColor(*guild, *message.Member), nil
|
2020-02-13 04:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 01:06:51 +00:00
|
|
|
return s.MemberColor(message.GuildID, message.Author.ID)
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) MemberColor(guildID discord.GuildID, userID discord.UserID) (discord.Color, error) {
|
2020-05-16 20:13:40 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
g, gerr := s.Store.Guild(guildID)
|
2020-06-06 10:20:01 +00:00
|
|
|
m, merr := s.Store.Member(guildID, userID)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case gerr != nil && merr != nil:
|
2020-05-16 20:13:40 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-06-06 10:20:01 +00:00
|
|
|
g, gerr = s.fetchGuild(guildID)
|
2020-05-16 20:13:40 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-01-21 01:06:51 +00:00
|
|
|
|
2020-06-06 10:20:01 +00:00
|
|
|
m, merr = s.fetchMember(guildID, userID)
|
|
|
|
case gerr != nil:
|
|
|
|
g, gerr = s.fetchGuild(guildID)
|
|
|
|
case merr != nil:
|
|
|
|
m, merr = s.fetchMember(guildID, userID)
|
2020-05-16 20:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if gerr != nil {
|
|
|
|
return 0, errors.Wrap(merr, "failed to get guild")
|
2020-01-21 01:06:51 +00:00
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
if merr != nil {
|
|
|
|
return 0, errors.Wrap(merr, "failed to get member")
|
|
|
|
}
|
2020-01-21 01:06:51 +00:00
|
|
|
|
2020-05-16 20:13:40 +00:00
|
|
|
return discord.MemberColor(*g, *m), nil
|
2020-01-21 01:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
func (s *State) Permissions(
|
|
|
|
channelID discord.ChannelID, userID discord.UserID) (discord.Permissions, error) {
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
ch, err := s.Channel(channelID)
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return 0, errors.Wrap(err, "failed to get channel")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 20:13:40 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
g, gerr := s.Store.Guild(ch.GuildID)
|
2020-06-06 10:20:01 +00:00
|
|
|
m, merr := s.Store.Member(ch.GuildID, userID)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case gerr != nil && merr != nil:
|
2020-05-16 20:13:40 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-06-06 10:20:01 +00:00
|
|
|
g, gerr = s.fetchGuild(ch.GuildID)
|
2020-05-16 20:13:40 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-06-06 10:20:01 +00:00
|
|
|
m, merr = s.fetchMember(ch.GuildID, userID)
|
|
|
|
case gerr != nil:
|
|
|
|
g, gerr = s.fetchGuild(ch.GuildID)
|
|
|
|
case merr != nil:
|
|
|
|
m, merr = s.fetchMember(ch.GuildID, userID)
|
2020-05-16 20:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if gerr != nil {
|
|
|
|
return 0, errors.Wrap(merr, "failed to get guild")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
if merr != nil {
|
|
|
|
return 0, errors.Wrap(merr, "failed to get member")
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
return discord.CalcOverwrites(*g, *ch, *m), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-02-08 19:48:45 +00:00
|
|
|
func (s *State) Me() (*discord.User, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
u, err := s.Store.Me()
|
|
|
|
if err == nil {
|
|
|
|
return u, nil
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
u, err = s.Session.Me()
|
2020-01-18 07:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
return u, s.Store.MyselfSet(*u)
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Channel(id discord.ChannelID) (*discord.Channel, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err := s.Store.Channel(id)
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err = s.Session.Channel(id)
|
2020-01-18 07:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
return c, s.Store.ChannelSet(*c)
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Channels(guildID discord.GuildID) ([]discord.Channel, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err := s.Store.Channels(guildID)
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
2020-01-17 22:36:53 +00:00
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err = s.Session.Channels(guildID)
|
2020-01-18 07:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 22:36:53 +00:00
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
for _, ch := range c {
|
|
|
|
ch := ch
|
2020-02-07 08:22:41 +00:00
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.Store.ChannelSet(ch); err != nil {
|
2020-03-01 02:54:14 +00:00
|
|
|
return nil, err
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) CreatePrivateChannel(recipient discord.UserID) (*discord.Channel, error) {
|
2020-04-08 02:33:56 +00:00
|
|
|
c, err := s.Store.CreatePrivateChannel(recipient)
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = s.Session.CreatePrivateChannel(recipient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
return c, s.Store.ChannelSet(*c)
|
2020-04-08 02:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) PrivateChannels() ([]discord.Channel, error) {
|
|
|
|
c, err := s.Store.PrivateChannels()
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = s.Session.PrivateChannels()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range c {
|
|
|
|
ch := ch
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.Store.ChannelSet(ch); err != nil {
|
2020-04-08 02:33:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
func (s *State) Emoji(
|
2020-07-21 20:27:59 +00:00
|
|
|
guildID discord.GuildID, emojiID discord.EmojiID) (*discord.Emoji, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
|
|
|
|
e, err := s.Store.Emoji(guildID, emojiID)
|
|
|
|
if err == nil {
|
|
|
|
return e, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es, err := s.Session.Emojis(guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
if err := s.Store.EmojiSet(guildID, es); err != nil {
|
|
|
|
return nil, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range es {
|
|
|
|
if e.ID == emojiID {
|
|
|
|
return &e, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrStoreNotFound
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Emojis(guildID discord.GuildID) ([]discord.Emoji, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
e, err := s.Store.Emojis(guildID)
|
|
|
|
if err == nil {
|
|
|
|
return e, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es, err := s.Session.Emojis(guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
return es, s.Store.EmojiSet(guildID, es)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Guild(id discord.GuildID) (*discord.Guild, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err := s.Store.Guild(id)
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:01 +00:00
|
|
|
return s.fetchGuild(id)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guilds will only fill a maximum of 100 guilds from the API.
|
|
|
|
func (s *State) Guilds() ([]discord.Guild, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err := s.Store.Guilds()
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
c, err = s.Session.Guilds(MaxFetchGuilds)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
for _, ch := range c {
|
|
|
|
ch := ch
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.Store.GuildSet(ch); err != nil {
|
2020-03-01 02:54:14 +00:00
|
|
|
return nil, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Member(guildID discord.GuildID, userID discord.UserID) (*discord.Member, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
m, err := s.Store.Member(guildID, userID)
|
|
|
|
if err == nil {
|
|
|
|
return m, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:01 +00:00
|
|
|
return s.fetchMember(guildID, userID)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Members(guildID discord.GuildID) ([]discord.Member, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
ms, err := s.Store.Members(guildID)
|
|
|
|
if err == nil {
|
|
|
|
return ms, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
ms, err = s.Session.Members(guildID, MaxFetchMembers)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
for _, m := range ms {
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.Store.MemberSet(guildID, m); err != nil {
|
2020-03-01 02:54:14 +00:00
|
|
|
return nil, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
return ms, s.Gateway.RequestGuildMembers(gateway.RequestGuildMembersData{
|
2020-07-21 20:27:59 +00:00
|
|
|
GuildID: []discord.GuildID{guildID},
|
2020-03-01 02:54:14 +00:00
|
|
|
Presences: true,
|
|
|
|
})
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
func (s *State) Message(
|
|
|
|
channelID discord.ChannelID, messageID discord.MessageID) (*discord.Message, error) {
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
m, err := s.Store.Message(channelID, messageID)
|
|
|
|
if err == nil {
|
|
|
|
return m, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:18 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
c, cerr := s.Store.Channel(channelID)
|
|
|
|
if cerr != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-06-08 14:30:16 +00:00
|
|
|
c, cerr = s.Session.Channel(channelID)
|
|
|
|
if cerr == nil {
|
2020-07-28 19:00:01 +00:00
|
|
|
cerr = s.Store.ChannelSet(*c)
|
2020-06-06 10:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
m, err = s.Session.Message(channelID, messageID)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
2020-06-06 10:20:18 +00:00
|
|
|
return nil, errors.Wrap(err, "unable to fetch message")
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if cerr != nil {
|
|
|
|
return nil, errors.Wrap(cerr, "unable to fetch channel")
|
2020-01-25 08:05:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:18 +00:00
|
|
|
m.ChannelID = c.ID
|
2020-07-17 18:35:44 +00:00
|
|
|
m.GuildID = c.GuildID
|
2020-06-06 10:20:18 +00:00
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
return m, s.Store.MessageSet(*m)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
// Messages fetches maximum 100 messages from the API, if it has to. There is no
|
|
|
|
// limit if it's from the State storage.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Messages(channelID discord.ChannelID) ([]discord.Message, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
// TODO: Think of a design that doesn't rely on MaxMessages().
|
|
|
|
var maxMsgs = s.MaxMessages()
|
2020-01-20 08:53:23 +00:00
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
ms, err := s.Store.Messages(channelID)
|
|
|
|
if err == nil {
|
|
|
|
// If the state already has as many messages as it can, skip the API.
|
|
|
|
if maxMsgs <= len(ms) {
|
|
|
|
return ms, nil
|
|
|
|
}
|
2020-03-01 02:50:50 +00:00
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
// Is the channel tiny?
|
|
|
|
s.fewMutex.Lock()
|
|
|
|
if _, ok := s.fewMessages[channelID]; ok {
|
2020-02-22 06:03:44 +00:00
|
|
|
s.fewMutex.Unlock()
|
2020-03-01 02:54:14 +00:00
|
|
|
return ms, nil
|
2020-01-20 08:53:23 +00:00
|
|
|
}
|
2020-03-01 02:54:14 +00:00
|
|
|
|
|
|
|
// No, fetch from the state.
|
|
|
|
s.fewMutex.Unlock()
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
ms, err = s.Session.Messages(channelID, uint(maxMsgs))
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 23:22:34 +00:00
|
|
|
// New messages fetched weirdly does not have GuildID filled. We'll try and
|
|
|
|
// get it for consistency with incoming message creates.
|
2020-07-21 20:27:59 +00:00
|
|
|
var guildID discord.GuildID
|
2020-01-20 23:22:34 +00:00
|
|
|
|
|
|
|
// A bit too convoluted, but whatever.
|
|
|
|
c, err := s.Channel(channelID)
|
|
|
|
if err == nil {
|
|
|
|
// If it's 0, it's 0 anyway. We don't need a check here.
|
|
|
|
guildID = c.GuildID
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:04:54 +00:00
|
|
|
// Iterate in reverse, since the store is expected to prepend the latest
|
|
|
|
// messages.
|
|
|
|
for i := len(ms) - 1; i >= 0; i-- {
|
2020-01-20 23:22:34 +00:00
|
|
|
// Set the guild ID, fine if it's 0 (it's already 0 anyway).
|
|
|
|
ms[i].GuildID = guildID
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.Store.MessageSet(ms[i]); err != nil {
|
2020-01-18 19:34:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:53:23 +00:00
|
|
|
if len(ms) < maxMsgs {
|
|
|
|
// Tiny channel, store this.
|
|
|
|
s.fewMutex.Lock()
|
2020-02-22 06:03:44 +00:00
|
|
|
s.fewMessages[channelID] = struct{}{}
|
2020-01-20 08:53:23 +00:00
|
|
|
s.fewMutex.Unlock()
|
|
|
|
|
|
|
|
return ms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the latest messages are at the end and we already know the maxMsgs,
|
|
|
|
// we could slice this right away.
|
|
|
|
return ms[:maxMsgs], nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-03-01 02:13:58 +00:00
|
|
|
// Presence checks the state for user presences. If no guildID is given, it will
|
2020-03-01 02:54:14 +00:00
|
|
|
// look for the presence in all guilds.
|
2020-07-28 19:00:01 +00:00
|
|
|
func (s *State) Presence(
|
|
|
|
guildID discord.GuildID, userID discord.UserID) (*discord.Presence, error) {
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
p, err := s.Store.Presence(guildID, userID)
|
2020-03-01 02:13:58 +00:00
|
|
|
if err == nil {
|
|
|
|
return p, nil
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
|
2020-03-01 02:13:58 +00:00
|
|
|
// If there's no guild ID, look in all guilds
|
2020-07-29 20:10:17 +00:00
|
|
|
if !guildID.IsValid() {
|
2020-03-01 02:13:58 +00:00
|
|
|
g, err := s.Guilds()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
|
2020-03-01 02:13:58 +00:00
|
|
|
for _, g := range g {
|
2020-03-01 02:54:14 +00:00
|
|
|
if p, err := s.Store.Presence(g.ID, userID); err == nil {
|
2020-03-01 02:13:58 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Role(guildID discord.GuildID, roleID discord.RoleID) (*discord.Role, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
r, err := s.Store.Role(guildID, roleID)
|
|
|
|
if err == nil {
|
|
|
|
return r, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rs, err := s.Session.Roles(guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var role *discord.Role
|
|
|
|
|
|
|
|
for _, r := range rs {
|
2020-02-07 08:22:41 +00:00
|
|
|
r := r
|
|
|
|
|
2020-01-18 19:34:08 +00:00
|
|
|
if r.ID == roleID {
|
|
|
|
role = &r
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.RoleSet(guildID, r); err != nil {
|
2020-03-01 02:54:14 +00:00
|
|
|
return role, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:37:01 +00:00
|
|
|
if role == nil {
|
|
|
|
return nil, ErrStoreNotFound
|
|
|
|
}
|
|
|
|
|
2020-01-18 19:34:08 +00:00
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Roles(guildID discord.GuildID) ([]discord.Role, error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
rs, err := s.Store.Roles(guildID)
|
|
|
|
if err == nil {
|
|
|
|
return rs, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
rs, err = s.Session.Roles(guildID)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
for _, r := range rs {
|
|
|
|
r := r
|
2020-02-07 08:22:41 +00:00
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
if err := s.RoleSet(guildID, r); err != nil {
|
2020-03-01 02:54:14 +00:00
|
|
|
return rs, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs, nil
|
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) fetchGuild(id discord.GuildID) (g *discord.Guild, err error) {
|
2020-06-06 10:20:01 +00:00
|
|
|
g, err = s.Session.Guild(id)
|
|
|
|
if err == nil {
|
2020-07-28 19:00:01 +00:00
|
|
|
err = s.Store.GuildSet(*g)
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:00:01 +00:00
|
|
|
func (s *State) fetchMember(
|
|
|
|
guildID discord.GuildID, userID discord.UserID) (m *discord.Member, err error) {
|
|
|
|
|
2020-06-06 10:20:01 +00:00
|
|
|
m, err = s.Session.Member(guildID, userID)
|
|
|
|
if err == nil {
|
2020-07-28 19:00:01 +00:00
|
|
|
err = s.Store.MemberSet(guildID, *m)
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|