mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-05 06:26:08 +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:
parent
2aaa2002d8
commit
7e49429f4b
|
@ -77,9 +77,6 @@ type State struct {
|
||||||
|
|
||||||
// *: State doesn't actually keep track of pinned messages.
|
// *: 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
|
// 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
|
// not found errors. Defaults to a no-op, as state errors aren't that
|
||||||
// important.
|
// important.
|
||||||
|
@ -95,6 +92,9 @@ type State struct {
|
||||||
// with the State.
|
// with the State.
|
||||||
*handler.Handler
|
*handler.Handler
|
||||||
|
|
||||||
|
readyMu *sync.Mutex
|
||||||
|
ready *gateway.ReadyEvent
|
||||||
|
|
||||||
// List of channels with few messages, so it doesn't bother hitting the API
|
// List of channels with few messages, so it doesn't bother hitting the API
|
||||||
// again.
|
// again.
|
||||||
fewMessages map[discord.ChannelID]struct{}
|
fewMessages map[discord.ChannelID]struct{}
|
||||||
|
@ -109,6 +109,10 @@ type State struct {
|
||||||
// they will be removed.
|
// they will be removed.
|
||||||
unreadyGuilds map[discord.GuildID]struct{}
|
unreadyGuilds map[discord.GuildID]struct{}
|
||||||
guildMutex *sync.Mutex
|
guildMutex *sync.Mutex
|
||||||
|
|
||||||
|
// TODO: box this into a Once[T] for v4
|
||||||
|
currentApp *discord.Application
|
||||||
|
appMutex *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new state.
|
// New creates a new state.
|
||||||
|
@ -148,6 +152,7 @@ func NewFromSession(s *session.Session, cabinet *store.Cabinet) *State {
|
||||||
unavailableGuilds: make(map[discord.GuildID]struct{}),
|
unavailableGuilds: make(map[discord.GuildID]struct{}),
|
||||||
unreadyGuilds: make(map[discord.GuildID]struct{}),
|
unreadyGuilds: make(map[discord.GuildID]struct{}),
|
||||||
guildMutex: new(sync.Mutex),
|
guildMutex: new(sync.Mutex),
|
||||||
|
appMutex: new(sync.Mutex),
|
||||||
}
|
}
|
||||||
state.hookSession()
|
state.hookSession()
|
||||||
return state
|
return state
|
||||||
|
@ -174,7 +179,10 @@ func (s *State) Ready() gateway.ReadyEvent {
|
||||||
r := s.ready
|
r := s.ready
|
||||||
s.readyMu.Unlock()
|
s.readyMu.Unlock()
|
||||||
|
|
||||||
return r
|
if r != nil {
|
||||||
|
return *r
|
||||||
|
}
|
||||||
|
return gateway.ReadyEvent{}
|
||||||
}
|
}
|
||||||
|
|
||||||
//// Helper methods
|
//// 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) {
|
func (s *State) Me() (*discord.User, error) {
|
||||||
u, err := s.Cabinet.Me()
|
u, err := s.Cabinet.Me()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Reference in a new issue