1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-02 20:02:53 +00:00

state: Cache CurrentApplication

This commit adds these methods into *state.State:

    - (*State).CurrentApplication
    - (*State).CurrentApplicationID

This should make interacting with API functions that require AppIDs much
more pleasant.
This commit is contained in:
diamondburned 2022-08-20 16:07:25 -07:00
parent 2aaa2002d8
commit 7e49429f4b
No known key found for this signature in database
GPG key ID: D78C4471CE776659

View file

@ -77,9 +77,6 @@ type State struct {
// *: State doesn't actually keep track of pinned messages.
readyMu *sync.Mutex
ready gateway.ReadyEvent
// 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.
@ -95,6 +92,9 @@ type State struct {
// with the State.
*handler.Handler
readyMu *sync.Mutex
ready *gateway.ReadyEvent
// List of channels with few messages, so it doesn't bother hitting the API
// again.
fewMessages map[discord.ChannelID]struct{}
@ -109,6 +109,10 @@ type State struct {
// they will be removed.
unreadyGuilds map[discord.GuildID]struct{}
guildMutex *sync.Mutex
// TODO: box this into a Once[T] for v4
currentApp *discord.Application
appMutex *sync.Mutex
}
// New creates a new state.
@ -148,6 +152,7 @@ func NewFromSession(s *session.Session, cabinet *store.Cabinet) *State {
unavailableGuilds: make(map[discord.GuildID]struct{}),
unreadyGuilds: make(map[discord.GuildID]struct{}),
guildMutex: new(sync.Mutex),
appMutex: new(sync.Mutex),
}
state.hookSession()
return state
@ -174,7 +179,10 @@ func (s *State) Ready() gateway.ReadyEvent {
r := s.ready
s.readyMu.Unlock()
return r
if r != nil {
return *r
}
return gateway.ReadyEvent{}
}
//// Helper methods
@ -328,6 +336,28 @@ func (s *State) Permissions(
////
func (s *State) CurrentApplication() (*discord.Application, error) {
s.appMutex.Lock()
defer s.appMutex.Unlock()
var err error
if s.currentApp == nil {
s.currentApp, err = s.Client.CurrentApplication()
}
return s.currentApp, err
}
func (s *State) CurrentApplicationID() discord.AppID {
app, _ := s.CurrentApplication()
if app != nil {
return app.ID
}
return 0
}
////
func (s *State) Me() (*discord.User, error) {
u, err := s.Cabinet.Me()
if err == nil {