1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-02-11 14:02:58 +00:00

Merge pull request #13 from matthewpi/feature/voicestate-caching

Add VoiceState caching to the state package
This commit is contained in:
diamondburned 2020-04-20 10:53:11 -07:00 committed by GitHub
commit 05d49527f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 3 deletions

View file

@ -19,8 +19,6 @@ func (s *State) hookSession() error {
}
func (s *State) onEvent(iface interface{}) {
// TODO: voice states
switch ev := iface.(type) {
case *gateway.ReadyEvent:
// Set Ready to the state
@ -257,6 +255,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")
}
}
}
}
@ -334,6 +344,13 @@ func handleGuildCreate(store Store, guild *gateway.GuildCreateEvent) []error {
}
}
// Handle guild voice states
for i := range guild.VoiceStates {
if err := store.VoiceStateSet(guild.ID, &guild.VoiceStates[i]); err != nil {
error(err, "Failed to set guild voice state in Ready")
}
}
return *stack
}

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
}