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"
|
2023-09-19 15:25:48 +00:00
|
|
|
"errors"
|
2023-09-19 15:23:25 +00:00
|
|
|
"fmt"
|
2020-01-20 08:53:23 +00:00
|
|
|
"sync"
|
2020-01-18 21:04:12 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/session"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/session/shard"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store/defaultstore"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/handler"
|
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-11-19 18:43:31 +00:00
|
|
|
MaxFetchGuilds uint = 100
|
2020-01-18 19:34:08 +00:00
|
|
|
)
|
|
|
|
|
2021-10-31 20:10:34 +00:00
|
|
|
// NewShardFunc creates a shard constructor with its own state registry and
|
|
|
|
// handlers. The given opts function is called everytime the State is created.
|
|
|
|
// The user should initialize handlers and intents in the opts function.
|
2021-06-10 23:48:32 +00:00
|
|
|
func NewShardFunc(opts func(*shard.Manager, *State)) shard.NewShardFunc {
|
|
|
|
return func(m *shard.Manager, id *gateway.Identifier) (shard.Shard, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
sessn := session.NewCustom(*id, api.NewClient(id.Token), handler.New())
|
|
|
|
state := NewFromSession(sessn, defaultstore.New())
|
2021-11-03 22:16:02 +00:00
|
|
|
opts(m, state)
|
|
|
|
return state, nil
|
2021-06-10 23:48:32 +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.
|
|
|
|
//
|
2023-09-19 15:23:25 +00:00
|
|
|
// # Store
|
2020-06-19 07:33:22 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2023-09-19 15:23:25 +00:00
|
|
|
// # Handler
|
2020-06-19 07:33:22 +00:00
|
|
|
//
|
|
|
|
// 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
|
2021-06-10 23:48:32 +00:00
|
|
|
*store.Cabinet
|
2020-01-18 21:04:12 +00:00
|
|
|
|
2020-01-18 21:40:09 +00:00
|
|
|
// *: State doesn't actually keep track of pinned messages.
|
|
|
|
|
2022-08-21 07:58:01 +00:00
|
|
|
readyMu *sync.Mutex
|
|
|
|
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
|
2021-05-29 11:21:47 +00:00
|
|
|
// unavailable after connecting to the gateway, i.e. they were sent in a
|
2020-06-06 20:47:15 +00:00
|
|
|
// GuildUnavailableEvent.
|
2021-05-29 11:21:47 +00:00
|
|
|
unavailableGuilds map[discord.GuildID]struct{}
|
|
|
|
// unreadyGuilds is a set of discord.GuildIDs of the guilds received during
|
|
|
|
// the Ready event. After receiving guild create events for those guilds,
|
|
|
|
// they will be removed.
|
|
|
|
unreadyGuilds map[discord.GuildID]struct{}
|
|
|
|
guildMutex *sync.Mutex
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// New creates a new state.
|
2021-09-28 20:19:04 +00:00
|
|
|
func New(token string) *State {
|
2020-11-30 00:57:58 +00:00
|
|
|
return NewWithStore(token, defaultstore.New())
|
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.
|
2021-09-28 20:19:04 +00:00
|
|
|
func NewWithIntents(token string, intents ...gateway.Intents) *State {
|
|
|
|
s := session.NewWithIntents(token, intents...)
|
|
|
|
return NewFromSession(s, defaultstore.New())
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 01:38:50 +00:00
|
|
|
// NewWithIdentifier creates a new state with the given gateway identifier.
|
2021-12-25 23:08:01 +00:00
|
|
|
func NewWithIdentifier(id gateway.Identifier) *State {
|
2021-11-17 01:38:50 +00:00
|
|
|
s := session.NewWithIdentifier(id)
|
|
|
|
return NewFromSession(s, defaultstore.New())
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// NewWithStore creates a new state with the given store cabinet.
|
2021-09-28 20:19:04 +00:00
|
|
|
func NewWithStore(token string, cabinet *store.Cabinet) *State {
|
|
|
|
s := session.New(token)
|
|
|
|
return NewFromSession(s, cabinet)
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 15:40:08 +00:00
|
|
|
// NewFromSession creates a new State from the passed Session and Cabinet.
|
2021-06-10 23:48:32 +00:00
|
|
|
func NewFromSession(s *session.Session, cabinet *store.Cabinet) *State {
|
2020-05-03 21:02:03 +00:00
|
|
|
state := &State{
|
2020-06-06 20:47:15 +00:00
|
|
|
Session: s,
|
2020-11-30 00:57:58 +00:00
|
|
|
Cabinet: cabinet,
|
2020-06-06 20:47:15 +00:00
|
|
|
Handler: handler.New(),
|
|
|
|
StateLog: func(err error) {},
|
2020-11-17 18:59:12 +00:00
|
|
|
readyMu: new(sync.Mutex),
|
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),
|
2021-05-29 11:21:47 +00:00
|
|
|
unavailableGuilds: make(map[discord.GuildID]struct{}),
|
|
|
|
unreadyGuilds: make(map[discord.GuildID]struct{}),
|
|
|
|
guildMutex: new(sync.Mutex),
|
2020-05-03 21:02:03 +00:00
|
|
|
}
|
2020-06-19 07:59:44 +00:00
|
|
|
state.hookSession()
|
2020-12-15 15:40:08 +00:00
|
|
|
return state
|
2020-05-03 21:02:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 09:18:54 +00:00
|
|
|
// NewAPIOnlyState creates a new State that only has API functions and no
|
|
|
|
// gateway (or state caches). Use this as a drop-in for InteractionServer usage.
|
|
|
|
//
|
|
|
|
// This function may work for most use cases; however, it will not work for all
|
2022-08-23 06:12:08 +00:00
|
|
|
// use cases. For example, bots that need the gateway won't be able to fully
|
|
|
|
// work, which is expected.
|
2022-08-22 09:18:54 +00:00
|
|
|
func NewAPIOnlyState(token string, h *handler.Handler) *State {
|
|
|
|
return &State{
|
|
|
|
Session: session.NewCustom(gateway.DefaultIdentifier(token), api.NewClient(token), h),
|
|
|
|
Handler: h,
|
|
|
|
Cabinet: store.NoopCabinet,
|
|
|
|
StateLog: func(err error) {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2020-11-14 23:30:18 +00:00
|
|
|
copied.Session = s.Session.WithContext(ctx)
|
2020-05-03 21:02:03 +00:00
|
|
|
|
|
|
|
return &copied
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
// Ready returns a copy of the Ready event. Although this function is safe to
|
|
|
|
// call concurrently, its values should still not be changed, as certain types
|
|
|
|
// like slices are not concurrent-safe.
|
2020-11-14 23:30:18 +00:00
|
|
|
//
|
2020-11-30 00:57:58 +00:00
|
|
|
// Note that if Ready events are not received yet, then the returned event will
|
|
|
|
// be a zero-value Ready instance.
|
|
|
|
func (s *State) Ready() gateway.ReadyEvent {
|
2020-11-14 23:30:18 +00:00
|
|
|
s.readyMu.Lock()
|
2020-11-30 00:57:58 +00:00
|
|
|
r := s.ready
|
2020-11-14 23:30:18 +00:00
|
|
|
s.readyMu.Unlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
2022-08-21 07:58:01 +00:00
|
|
|
return r
|
2020-11-14 23:30:18 +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
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
// AuthorColor is a variant of MemberColor that possibly uses the existing
|
|
|
|
// Member field inside MessageCreateEvent.
|
2022-03-31 18:42:22 +00:00
|
|
|
func (s *State) AuthorColor(message *gateway.MessageCreateEvent) (discord.Color, bool) {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !message.GuildID.IsValid() { // this is a dm
|
2022-03-31 18:42:22 +00:00
|
|
|
return discord.NullColor, false
|
2020-01-21 01:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 04:19:24 +00:00
|
|
|
if message.Member != nil {
|
2022-04-01 11:42:03 +00:00
|
|
|
return MemberColor(message.Member, func(id discord.RoleID) *discord.Role {
|
|
|
|
r, _ := s.Role(message.GuildID, id)
|
|
|
|
return r
|
|
|
|
})
|
2020-02-13 04:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 01:06:51 +00:00
|
|
|
return s.MemberColor(message.GuildID, message.Author.ID)
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
// MemberColor fetches the color of the member with the given user ID inside the
|
|
|
|
// guild with the given ID.
|
2022-03-31 18:42:22 +00:00
|
|
|
func (s *State) MemberColor(guildID discord.GuildID, userID discord.UserID) (discord.Color, bool) {
|
2022-04-01 11:42:03 +00:00
|
|
|
m, err := s.Member(guildID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return discord.NullColor, false
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
return MemberColor(m, func(id discord.RoleID) *discord.Role {
|
|
|
|
r, _ := s.Role(guildID, id)
|
|
|
|
return r
|
|
|
|
})
|
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
// MemberColor is a weird variant of State's MemberColor method that allows a
|
|
|
|
// custom Role getter. If m is nil, then NullColor is returned.
|
|
|
|
func MemberColor(m *discord.Member, role func(discord.RoleID) *discord.Role) (discord.Color, bool) {
|
|
|
|
c := discord.NullColor
|
|
|
|
pos := -1
|
2020-01-21 01:06:51 +00:00
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
if m == nil {
|
|
|
|
return c, false
|
2020-05-16 20:13:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
for _, roleID := range m.RoleIDs {
|
|
|
|
if r := role(roleID); r != nil {
|
|
|
|
if r.Color > 0 && r.Position > pos {
|
|
|
|
c = r.Color
|
|
|
|
pos = r.Position
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
2020-01-21 01:06:51 +00:00
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
return c, pos != -1
|
2020-01-21 01:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2021-01-06 05:09:16 +00:00
|
|
|
// Permissions gets the user's permissions in the given channel. If the channel
|
|
|
|
// is not in any guild, then an error is returned.
|
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 {
|
2023-09-19 15:23:25 +00:00
|
|
|
return 0, fmt.Errorf("failed to get channel: %w", err)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 05:09:16 +00:00
|
|
|
if !ch.GuildID.IsValid() {
|
|
|
|
return 0, errors.New("channel is not in a guild")
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:13:40 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
var (
|
2023-11-03 22:34:08 +00:00
|
|
|
g *discord.Guild
|
|
|
|
m *discord.Member
|
|
|
|
rs []discord.Role
|
2020-11-19 18:43:31 +00:00
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
gerr = store.ErrNotFound
|
|
|
|
merr = store.ErrNotFound
|
2023-11-03 22:34:08 +00:00
|
|
|
rerr = store.ErrNotFound
|
2020-11-19 18:43:31 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
g, gerr = s.Cabinet.Guild(ch.GuildID)
|
2023-11-03 22:34:08 +00:00
|
|
|
rs, rerr = s.Cabinet.Roles(ch.GuildID)
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildMembers) {
|
2020-11-30 00:57:58 +00:00
|
|
|
m, merr = s.Cabinet.Member(ch.GuildID, userID)
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-03 22:34:08 +00:00
|
|
|
if gerr != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
g, gerr = s.fetchGuild(ch.GuildID)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if merr != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
m, merr = s.fetchMember(ch.GuildID, userID)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if rerr != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
rs, rerr = s.fetchRoles(ch.GuildID)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:13:40 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if gerr != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
return 0, fmt.Errorf("failed to get guild: %w", gerr)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-06-06 10:20:01 +00:00
|
|
|
if merr != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
return 0, fmt.Errorf("failed to get member: %w", merr)
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2023-11-03 22:34:08 +00:00
|
|
|
return discord.CalcOverrides(*g, *ch, *m, rs), nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-02-08 19:48:45 +00:00
|
|
|
func (s *State) Me() (*discord.User, error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
u, err := s.Cabinet.Me()
|
2020-03-01 02:54:14 +00:00
|
|
|
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
|
|
|
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.MyselfSet(*u, false)
|
|
|
|
|
|
|
|
return u, nil
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
func (s *State) Channel(id discord.ChannelID) (c *discord.Channel, err error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
c, err = s.Cabinet.Channel(id)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil && s.tracksChannel(c) {
|
|
|
|
return
|
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 {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
if s.tracksChannel(c) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.ChannelSet(c, false)
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
2020-01-17 05:17:46 +00:00
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
func (s *State) Channels(guildID discord.GuildID) (cs []discord.Channel, err error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
cs, err = s.Cabinet.Channels(guildID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
2020-01-17 22:36:53 +00:00
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
cs, err = s.Session.Channels(guildID)
|
2020-01-18 07:07:52 +00:00
|
|
|
if err != nil {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
2020-01-17 22:36:53 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range cs {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.ChannelSet(&cs[i], false)
|
2020-01-17 05:17:46 +00:00
|
|
|
}
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 07:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) CreatePrivateChannel(recipient discord.UserID) (*discord.Channel, error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
c, err := s.Cabinet.CreatePrivateChannel(recipient)
|
2020-04-08 02:33:56 +00:00
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = s.Session.CreatePrivateChannel(recipient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.ChannelSet(c, false)
|
|
|
|
|
|
|
|
return c, nil
|
2020-04-08 02:33:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
// PrivateChannels gets the direct messages of the user.
|
|
|
|
// This is not supported for bots.
|
2020-04-08 02:33:56 +00:00
|
|
|
func (s *State) PrivateChannels() ([]discord.Channel, error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
cs, err := s.Cabinet.PrivateChannels()
|
2020-04-08 02:33:56 +00:00
|
|
|
if err == nil {
|
2020-11-19 18:43:31 +00:00
|
|
|
return cs, nil
|
2020-04-08 02:33:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
cs, err = s.Session.PrivateChannels()
|
2020-04-08 02:33:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range cs {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.ChannelSet(&cs[i], false)
|
2020-04-08 02:33:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
return cs, nil
|
2020-04-08 02:33:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 07:07:52 +00:00
|
|
|
////
|
|
|
|
|
2020-03-01 02:54:14 +00:00
|
|
|
func (s *State) Emoji(
|
2020-11-19 18:43:31 +00:00
|
|
|
guildID discord.GuildID, emojiID discord.EmojiID) (e *discord.Emoji, err error) {
|
2020-03-01 02:54:14 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildEmojis) {
|
2020-11-30 00:57:58 +00:00
|
|
|
e, err = s.Cabinet.Emoji(guildID, emojiID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else { // Fast path
|
|
|
|
return s.Session.Emoji(guildID, emojiID)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es, err := s.Session.Emojis(guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.EmojiSet(guildID, es, false)
|
2020-01-18 19:34:08 +00:00
|
|
|
|
|
|
|
for _, e := range es {
|
|
|
|
if e.ID == emojiID {
|
|
|
|
return &e, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
func (s *State) Emojis(guildID discord.GuildID) (es []discord.Emoji, err error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildEmojis) {
|
2020-11-30 00:57:58 +00:00
|
|
|
es, err = s.Cabinet.Emojis(guildID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
es, err = s.Session.Emojis(guildID)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildEmojis) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.EmojiSet(guildID, es, false)
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
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) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
c, err := s.Cabinet.Guild(id)
|
2020-11-19 18:43:31 +00:00
|
|
|
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.
|
2020-11-19 18:43:31 +00:00
|
|
|
func (s *State) Guilds() (gs []discord.Guild, err error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
gs, err = s.Cabinet.Guilds()
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
gs, err = s.Session.Guilds(MaxFetchGuilds)
|
2020-01-18 19:34:08 +00:00
|
|
|
if err != nil {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range gs {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.GuildSet(&gs[i], false)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Member(guildID discord.GuildID, userID discord.UserID) (*discord.Member, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildMembers) {
|
2020-11-30 00:57:58 +00:00
|
|
|
m, err := s.Cabinet.Member(guildID, userID)
|
2020-11-19 18:43:31 +00:00
|
|
|
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-11-19 18:43:31 +00:00
|
|
|
func (s *State) Members(guildID discord.GuildID) (ms []discord.Member, err error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildMembers) {
|
2020-11-30 00:57:58 +00:00
|
|
|
ms, err = s.Cabinet.Members(guildID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
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 {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuildMembers) {
|
2021-11-03 22:16:02 +00:00
|
|
|
for i := range ms {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.MemberSet(guildID, &ms[i], false)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
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-11-30 00:57:58 +00:00
|
|
|
m, err := s.Cabinet.Message(channelID, messageID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil && s.tracksMessage(m) {
|
2020-03-01 02:54:14 +00:00
|
|
|
return m, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
2020-06-06 10:20:18 +00:00
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
c *discord.Channel
|
2020-11-30 00:57:58 +00:00
|
|
|
cerr = store.ErrNotFound
|
2020-11-19 18:43:31 +00:00
|
|
|
)
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
c, cerr = s.Cabinet.Channel(channelID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if cerr != nil || !s.tracksChannel(c) {
|
2020-06-06 10:20:18 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-06-08 14:30:16 +00:00
|
|
|
c, cerr = s.Session.Channel(channelID)
|
2021-09-28 20:19:04 +00:00
|
|
|
if cerr == nil && s.HasIntents(gateway.IntentGuilds) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.ChannelSet(c, false)
|
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 {
|
2023-09-19 15:23:25 +00:00
|
|
|
return nil, fmt.Errorf("unable to fetch message: %w", err)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:20:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if cerr != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
return nil, fmt.Errorf("unable to fetch channel: %w", cerr)
|
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-11-19 18:43:31 +00:00
|
|
|
return m, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// Messages returns a slice filled with the most recent messages sent in the
|
|
|
|
// channel with the passed ID. The method automatically paginates until it
|
|
|
|
// reaches the passed limit, or, if the limit is set to 0, has fetched all
|
|
|
|
// messages in the channel.
|
|
|
|
//
|
|
|
|
// As the underlying endpoint is capped at a maximum of 100 messages per
|
|
|
|
// request, at maximum a total of limit/100 rounded up requests will be made,
|
|
|
|
// although they may be less, if no more messages are available or there are
|
|
|
|
// cached messages.
|
|
|
|
// When fetching the messages, those with the highest ID, will be fetched
|
|
|
|
// first. The returned slice will be sorted from latest to oldest.
|
|
|
|
func (s *State) Messages(channelID discord.ChannelID, limit uint) ([]discord.Message, error) {
|
|
|
|
storeMessages, err := s.Cabinet.Messages(channelID)
|
2022-04-05 04:36:26 +00:00
|
|
|
if len(storeMessages) > 0 && s.tracksMessage(&storeMessages[0]) {
|
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()
|
2021-06-03 19:39:49 +00:00
|
|
|
return storeMessages, nil
|
2020-01-20 08:53:23 +00:00
|
|
|
}
|
2020-03-01 02:54:14 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// No, fetch from the API.
|
2020-03-01 02:54:14 +00:00
|
|
|
s.fewMutex.Unlock()
|
2021-06-03 19:39:49 +00:00
|
|
|
} else {
|
|
|
|
// Something wrong with the cached messages, make sure they aren't
|
|
|
|
// returned.
|
|
|
|
storeMessages = nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// Store already has enough messages.
|
|
|
|
if len(storeMessages) >= int(limit) && limit > 0 {
|
|
|
|
return storeMessages[:limit], nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
2021-06-10 23:48:32 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// Decrease the limit, if we aren't fetching all messages.
|
|
|
|
if limit > 0 {
|
|
|
|
limit -= uint(len(storeMessages))
|
2020-01-20 23:22:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
var before discord.MessageID = 0
|
|
|
|
if len(storeMessages) > 0 {
|
|
|
|
before = storeMessages[len(storeMessages)-1].ID
|
|
|
|
}
|
2020-01-20 23:22:34 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
apiMessages, err := s.Session.MessagesBefore(channelID, before, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
if len(storeMessages)+len(apiMessages) < s.MaxMessages() {
|
2020-01-20 08:53:23 +00:00
|
|
|
// 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()
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(apiMessages) == 0 {
|
|
|
|
return storeMessages, nil
|
|
|
|
}
|
2020-01-20 08:53:23 +00:00
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
// New messages fetched weirdly does not have GuildID filled. If we have
|
|
|
|
// cached messages, we can use their GuildID. Otherwise, we need to fetch
|
|
|
|
// it from the api.
|
|
|
|
var guildID discord.GuildID
|
|
|
|
if len(storeMessages) > 0 {
|
|
|
|
guildID = storeMessages[0].GuildID
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:42:03 +00:00
|
|
|
for i := range apiMessages {
|
|
|
|
apiMessages[i].GuildID = guildID
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.tracksMessage(&apiMessages[0]) && len(storeMessages) < s.MaxMessages() {
|
|
|
|
// Only add as many messages as the store can hold.
|
2021-06-25 05:46:27 +00:00
|
|
|
i := s.MaxMessages() - len(storeMessages)
|
|
|
|
if i > len(apiMessages) {
|
|
|
|
i = len(apiMessages)
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
msgs := apiMessages[:i]
|
|
|
|
for i := range msgs {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.MessageSet(&msgs[i], false)
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
2020-01-20 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
return append(storeMessages, apiMessages...), nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
// Presence checks the state for user presences. If no guildID is given, it
|
|
|
|
// will look for the presence in all cached guilds.
|
2021-08-08 20:19:15 +00:00
|
|
|
func (s *State) Presence(gID discord.GuildID, uID discord.UserID) (*discord.Presence, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if !s.HasIntents(gateway.IntentGuildPresences) {
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-03-01 02:13:58 +00:00
|
|
|
}
|
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-11-30 00:57:58 +00:00
|
|
|
if !gID.IsValid() {
|
2021-09-28 20:19:04 +00:00
|
|
|
if !s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
g, err := s.Cabinet.Guilds()
|
2020-03-01 02:13:58 +00:00
|
|
|
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-11-30 00:57:58 +00:00
|
|
|
if p, err := s.Cabinet.Presence(g.ID, uID); err == nil {
|
2020-03-01 02:13:58 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 18:43:31 +00:00
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-03-01 02:13:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
return s.Cabinet.Presence(gID, uID)
|
2020-03-01 02:13:58 +00:00
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
func (s *State) Role(guildID discord.GuildID, roleID discord.RoleID) (target *discord.Role, err error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2020-11-30 00:57:58 +00:00
|
|
|
target, err = s.Cabinet.Role(guildID, roleID)
|
2020-11-19 18:43:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rs, err := s.Session.Roles(guildID)
|
|
|
|
if err != nil {
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
for i, r := range rs {
|
2020-01-18 19:34:08 +00:00
|
|
|
if r.ID == roleID {
|
2020-11-19 18:43:31 +00:00
|
|
|
r := r // copy to prevent mem aliasing
|
|
|
|
target = &r
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.RoleSet(guildID, &rs[i], false)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
if target == nil {
|
2020-11-30 00:57:58 +00:00
|
|
|
return nil, store.ErrNotFound
|
2020-10-18 20:37:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:43:31 +00:00
|
|
|
return
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *State) Roles(guildID discord.GuildID) ([]discord.Role, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.HasIntents(gateway.IntentGuilds) {
|
2023-11-03 22:34:08 +00:00
|
|
|
rs, err := s.Cabinet.Roles(guildID)
|
|
|
|
if err == nil {
|
|
|
|
return rs, nil
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 22:34:08 +00:00
|
|
|
return s.fetchRoles(guildID)
|
2020-01-18 19:34:08 +00:00
|
|
|
}
|
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)
|
2021-09-28 20:19:04 +00:00
|
|
|
if err == nil && s.HasIntents(gateway.IntentGuilds) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.GuildSet(g, false)
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-03 22:34:08 +00:00
|
|
|
func (s *State) fetchRoles(gID discord.GuildID) (rs []discord.Role, err error) {
|
|
|
|
rs, err = s.Session.Roles(gID)
|
|
|
|
if err == nil && s.HasIntents(gateway.IntentGuilds) {
|
|
|
|
for i := range rs {
|
|
|
|
s.RoleSet(gID, &rs[i], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:57:58 +00:00
|
|
|
func (s *State) fetchMember(gID discord.GuildID, uID discord.UserID) (m *discord.Member, err error) {
|
|
|
|
m, err = s.Session.Member(gID, uID)
|
2021-09-28 20:19:04 +00:00
|
|
|
if err == nil && s.HasIntents(gateway.IntentGuildMembers) {
|
2021-11-09 21:45:39 +00:00
|
|
|
s.Cabinet.MemberSet(gID, m, false)
|
2020-06-06 10:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-11-19 18:43:31 +00:00
|
|
|
|
|
|
|
// tracksMessage reports whether the state would track the passed message and
|
|
|
|
// messages from the same channel.
|
|
|
|
func (s *State) tracksMessage(m *discord.Message) bool {
|
2021-09-28 20:19:04 +00:00
|
|
|
return (m.GuildID.IsValid() && s.HasIntents(gateway.IntentGuildMessages)) ||
|
|
|
|
(!m.GuildID.IsValid() && s.HasIntents(gateway.IntentDirectMessages))
|
2020-11-19 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tracksChannel reports whether the state would track the passed channel.
|
|
|
|
func (s *State) tracksChannel(c *discord.Channel) bool {
|
2021-09-28 20:19:04 +00:00
|
|
|
return (c.GuildID.IsValid() && s.HasIntents(gateway.IntentGuilds)) ||
|
2020-11-19 18:43:31 +00:00
|
|
|
!c.GuildID.IsValid()
|
|
|
|
}
|