Skip to content

Commit

Permalink
Merge pull request #13 from DisgoOrg/development
Browse files Browse the repository at this point in the history
fixed voice states not getting cached
  • Loading branch information
topi314 authored Apr 27, 2021
2 parents 490e24c + d6d400e commit cf49145
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
22 changes: 11 additions & 11 deletions internal/cache_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *CacheImpl) GlobalCommandCache() map[api.Snowflake]*api.Command {

// CacheGlobalCommand adds a global command to the cache
func (c *CacheImpl) CacheGlobalCommand(command *api.Command) *api.Command {
if !c.CacheFlags().Has(api.CacheFlagCommands) {
if c.CacheFlags().Missing(api.CacheFlagCommands) {
return command
}
if _, ok := c.globalCommands[command.ID]; ok {
Expand All @@ -150,7 +150,7 @@ func (c *CacheImpl) CacheGlobalCommand(command *api.Command) *api.Command {

// CacheGuildCommand adds a Guild Command to the cache
func (c *CacheImpl) CacheGuildCommand(command *api.Command) *api.Command {
if !c.CacheFlags().Has(api.CacheFlagCommands) {
if c.CacheFlags().Missing(api.CacheFlagCommands) {
return command
}
if guildCommands, ok := c.guildCommands[command.ID]; ok {
Expand Down Expand Up @@ -483,7 +483,7 @@ func (c *CacheImpl) AllMemberCache() map[api.Snowflake]map[api.Snowflake]*api.Me
// CacheMember adds a member to the cache
func (c *CacheImpl) CacheMember(member *api.Member) *api.Member {
// only cache member if we want to & always cache self member!
if member.User.ID != member.Disgo.ApplicationID() && !c.memberCachePolicy(member) {
if !c.memberCachePolicy(member) && member.User.ID != member.Disgo.ApplicationID() {
return member
}
if guildMembers, ok := c.members[member.GuildID]; ok {
Expand Down Expand Up @@ -561,7 +561,7 @@ func (c *CacheImpl) VoiceStateCache(guildID api.Snowflake) map[api.Snowflake]*ap
// CacheVoiceState adds a api.VoiceState from the api.Cache
func (c *CacheImpl) CacheVoiceState(voiceState *api.VoiceState) *api.VoiceState {
// only cache voice states for ourself or member is cached & cache flag activated
if voiceState.UserID != c.disgo.ApplicationID() && (!c.cacheFlags.Has(api.CacheFlagVoiceState) || c.Member(voiceState.GuildID, voiceState.UserID) == nil) {
if c.cacheFlags.Missing(api.CacheFlagVoiceState) && voiceState.UserID != c.disgo.ApplicationID() {
return voiceState
}
if guildVoiceStates, ok := c.voiceStates[voiceState.GuildID]; ok {
Expand Down Expand Up @@ -643,7 +643,7 @@ func (c *CacheImpl) AllRoleCache() map[api.Snowflake]map[api.Snowflake]*api.Role

// CacheRole adds a role to the cache
func (c *CacheImpl) CacheRole(role *api.Role) *api.Role {
if !c.cacheFlags.Has(api.CacheFlagRoles) {
if c.cacheFlags.Missing(api.CacheFlagRoles) {
return role
}
if guildRoles, ok := c.roles[role.GuildID]; ok {
Expand Down Expand Up @@ -764,7 +764,7 @@ func (c *CacheImpl) DMChannelCache() map[api.Snowflake]*api.DMChannel {

// CacheDMChannel adds a DM channel to the cache
func (c *CacheImpl) CacheDMChannel(dmChannel *api.DMChannel) *api.DMChannel {
if !c.cacheFlags.Has(api.CacheFlagDMChannels) {
if c.cacheFlags.Missing(api.CacheFlagDMChannels) {
return dmChannel
}
if oldChannel, ok := c.dmChannels[dmChannel.ID]; ok {
Expand Down Expand Up @@ -870,7 +870,7 @@ func (c *CacheImpl) AllTextChannelCache() map[api.Snowflake]map[api.Snowflake]*a

// CacheTextChannel adds a channel to the cache
func (c *CacheImpl) CacheTextChannel(textChannel *api.TextChannel) *api.TextChannel {
if !c.cacheFlags.Has(api.CacheFlagTextChannels) {
if c.cacheFlags.Missing(api.CacheFlagTextChannels) {
return textChannel
}
if guildTextChannels, ok := c.textChannels[*textChannel.GuildChannel.GuildID]; ok {
Expand Down Expand Up @@ -978,7 +978,7 @@ func (c *CacheImpl) AllStoreChannelCache() map[api.Snowflake]map[api.Snowflake]*

// CacheStoreChannel adds a store channel to the cache
func (c *CacheImpl) CacheStoreChannel(storeChannel *api.StoreChannel) *api.StoreChannel {
if !c.cacheFlags.Has(api.CacheFlagStoreChannels) {
if c.cacheFlags.Missing(api.CacheFlagStoreChannels) {
return storeChannel
}
if guildStoreChannels, ok := c.storeChannels[*storeChannel.GuildID]; ok {
Expand Down Expand Up @@ -1081,7 +1081,7 @@ func (c *CacheImpl) AllVoiceChannelCache() map[api.Snowflake]map[api.Snowflake]*

// CacheVoiceChannel adds a voice channel to cache
func (c *CacheImpl) CacheVoiceChannel(voiceChannel *api.VoiceChannel) *api.VoiceChannel {
if !c.cacheFlags.Has(api.CacheFlagVoiceChannels) {
if c.cacheFlags.Missing(api.CacheFlagVoiceChannels) {
return voiceChannel
}
if guildVoiceChannels, ok := c.voiceChannels[*voiceChannel.GuildID]; ok {
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func (c *CacheImpl) AllCategoryCache() map[api.Snowflake]map[api.Snowflake]*api.

// CacheCategory adds a category to the cache
func (c *CacheImpl) CacheCategory(category *api.Category) *api.Category {
if !c.cacheFlags.Has(api.CacheFlagCategories) {
if c.cacheFlags.Missing(api.CacheFlagCategories) {
return category
}
if guildCategories, ok := c.categories[*category.GuildID]; ok {
Expand Down Expand Up @@ -1276,7 +1276,7 @@ func (c *CacheImpl) AllEmoteCache() map[api.Snowflake]map[api.Snowflake]*api.Emo

// CacheEmote adds an Emote to the api.Cache if emoji caches are used
func (c *CacheImpl) CacheEmote(emote *api.Emote) *api.Emote {
if !c.cacheFlags.Has(api.CacheFlagEmotes) {
if c.cacheFlags.Missing(api.CacheFlagEmotes) {
return emote
}
if guildEmotes, ok := c.emotes[emote.GuildID]; ok {
Expand Down
13 changes: 8 additions & 5 deletions internal/handlers/voice_state_update_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ func (h VoiceStateUpdateHandler) HandleGatewayEvent(disgo api.Disgo, eventManage
if !ok {
return
}

oldVoiceState := disgo.Cache().VoiceState(voiceStateUpdate.GuildID, voiceStateUpdate.UserID)
if oldVoiceState != nil {
oldVoiceState = &*oldVoiceState
}
voiceStateUpdate.VoiceState = disgo.EntityBuilder().CreateVoiceState(voiceStateUpdate.GuildID, voiceStateUpdate.VoiceState, api.CacheStrategyYes)
voiceStateUpdate.Member = disgo.EntityBuilder().CreateMember(voiceStateUpdate.Member.GuildID, voiceStateUpdate.Member, api.CacheStrategyYes)
member := disgo.EntityBuilder().CreateMember(voiceStateUpdate.Member.GuildID, voiceStateUpdate.Member, api.CacheStrategyYes)
voiceState := disgo.EntityBuilder().CreateVoiceState(voiceStateUpdate.GuildID, voiceStateUpdate.VoiceState, api.CacheStrategyYes)

// voice state update for ourself received
// execute voice VoiceDispatchInterceptor.OnVoiceStateUpdate
if disgo.ApplicationID() == voiceStateUpdate.UserID {
if interceptor := disgo.VoiceDispatchInterceptor(); interceptor != nil {
interceptor.OnVoiceStateUpdate(voiceStateUpdate)
Expand All @@ -39,7 +42,7 @@ func (h VoiceStateUpdateHandler) HandleGatewayEvent(disgo api.Disgo, eventManage

guild := voiceStateUpdate.Guild()
if guild == nil {
disgo.Logger().Error("received guild voice state update for unknown guild: %s", voiceStateUpdate.GuildID)
disgo.Logger().Warnf("received guild voice state update for unknown guild: %s", voiceStateUpdate.GuildID)
return
}

Expand All @@ -51,13 +54,13 @@ func (h VoiceStateUpdateHandler) HandleGatewayEvent(disgo api.Disgo, eventManage

genericGuildMemberEvent := events.GenericGuildMemberEvent{
GenericGuildEvent: genericGuildEvent,
Member: voiceStateUpdate.Member,
Member: member,
}
disgo.EventManager().Dispatch(genericGuildMemberEvent)

genericGuildVoiceEvent := events.GenericGuildVoiceEvent{
GenericGuildMemberEvent: genericGuildMemberEvent,
VoiceState: voiceStateUpdate.VoiceState,
VoiceState: voiceState,
}
disgo.EventManager().Dispatch(genericGuildVoiceEvent)

Expand Down

0 comments on commit cf49145

Please sign in to comment.