diff --git a/state/state.go b/state/state.go index 49dae53..1cf8ee6 100644 --- a/state/state.go +++ b/state/state.go @@ -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 {