Add voice state caching

This commit is contained in:
Matthew Penner 2020-04-18 20:22:49 -06:00
parent 0d81f9176a
commit 9686a41539
4 changed files with 95 additions and 1 deletions

View File

@ -257,6 +257,18 @@ func (s *State) onEvent(iface interface{}) {
if err := s.Store.MyselfSet((*discord.User)(ev)); err != nil {
s.stateErr(err, "Failed to update myself from USER_UPDATE")
}
case *gateway.VoiceStateUpdateEvent:
vs := (*discord.VoiceState)(ev)
if vs.ChannelID == 0 {
if err := s.Store.VoiceStateRemove(vs.GuildID, vs.UserID); err != nil {
s.stateErr(err, "Failed to remove voice state from state")
}
} else {
if err := s.Store.VoiceStateSet(vs.GuildID, vs); err != nil {
s.stateErr(err, "Failed to update voice state in state")
}
}
}
}

View File

@ -51,6 +51,8 @@ type StoreGetter interface {
Role(guildID, roleID discord.Snowflake) (*discord.Role, error)
Roles(guildID discord.Snowflake) ([]discord.Role, error)
VoiceState(guildID discord.Snowflake, userID discord.Snowflake) (*discord.VoiceState, error)
}
type StoreModifier interface {
@ -78,6 +80,9 @@ type StoreModifier interface {
RoleSet(guildID discord.Snowflake, role *discord.Role) error
RoleRemove(guildID, roleID discord.Snowflake) error
VoiceStateSet(guildID discord.Snowflake, voiceState *discord.VoiceState) error
VoiceStateRemove(guildID discord.Snowflake, userID discord.Snowflake) error
}
// ErrStoreNotFound is an error that a store can use to return when something

View File

@ -22,6 +22,7 @@ type DefaultStore struct {
members map[discord.Snowflake][]discord.Member // guildID:members
presences map[discord.Snowflake][]discord.Presence // guildID:presences
messages map[discord.Snowflake][]discord.Message // channelID:messages
voiceStates map[discord.Snowflake][]discord.VoiceState // guildID:voiceStates
mut sync.Mutex
}
@ -664,3 +665,67 @@ func (s *DefaultStore) RoleRemove(guildID, roleID discord.Snowflake) error {
return ErrStoreNotFound
}
////
func (s *DefaultStore) VoiceState(guildID, userID discord.Snowflake) (*discord.VoiceState, error) {
s.mut.Lock()
defer s.mut.Unlock()
states, ok := s.voiceStates[guildID]
if !ok {
return nil, ErrStoreNotFound
}
for _, vs := range states {
if vs.UserID == userID {
return &vs, nil
}
}
return nil, ErrStoreNotFound
}
func (s *DefaultStore) VoiceStateSet(guildID discord.Snowflake, voiceState *discord.VoiceState) error {
s.mut.Lock()
defer s.mut.Unlock()
states, ok := s.voiceStates[guildID]
if !ok {
return ErrStoreNotFound
}
for i, vs := range states {
if vs.UserID == voiceState.UserID {
states[i] = *voiceState
s.voiceStates[guildID] = states
return nil
}
}
states = append(states, *voiceState)
s.voiceStates[guildID] = states
return nil
}
func (s *DefaultStore) VoiceStateRemove(guildID, userID discord.Snowflake) error {
s.mut.Lock()
defer s.mut.Unlock()
states, ok := s.voiceStates[guildID]
if !ok {
return ErrStoreNotFound
}
for i, vs := range states {
if vs.UserID == userID {
states = append(states[:i], states[i+1:]...)
s.voiceStates[guildID] = states
return nil
}
}
return ErrStoreNotFound
}

View File

@ -109,7 +109,7 @@ func (NoopStore) MaxMessages() int {
return 100
}
func (NoopStore) MessageSet(message *discord.Message) error {
func (NoopStore) MessageSet(*discord.Message) error {
return nil
}
@ -148,3 +148,15 @@ func (NoopStore) RoleSet(discord.Snowflake, *discord.Role) error {
func (NoopStore) RoleRemove(_, _ discord.Snowflake) error {
return nil
}
func (NoopStore) VoiceState(_, _ discord.Snowflake) (*discord.VoiceState, error) {
return nil, ErrNotImplemented
}
func (NoopStore) VoiceStateSet(discord.Snowflake, *discord.VoiceState) error {
return ErrNotImplemented
}
func (NoopStore) VoiceStateRemove(_, _ discord.Snowflake) error {
return ErrNotImplemented
}