Documentation ¶
Index ¶
- func PolicyAll[T any](_ T) bool
- func PolicyDefault[T any](t T) bool
- func PolicyMembersPending(member discord.Member) bool
- func PolicyNone[T any](_ T) bool
- type Cache
- type Caches
- type ChannelCache
- type Config
- type ConfigOpt
- func WithCacheFlags(flags ...Flags) ConfigOpt
- func WithChannelCachePolicy(policy Policy[discord.Channel]) ConfigOpt
- func WithEmojiCachePolicy(policy Policy[discord.Emoji]) ConfigOpt
- func WithGuildCachePolicy(policy Policy[discord.Guild]) ConfigOpt
- func WithGuildScheduledEventCachePolicy(policy Policy[discord.GuildScheduledEvent]) ConfigOpt
- func WithMemberCachePolicy(policy Policy[discord.Member]) ConfigOpt
- func WithMessageCachePolicy(policy Policy[discord.Message]) ConfigOpt
- func WithPresenceCachePolicy(policy Policy[discord.Presence]) ConfigOpt
- func WithRoleCachePolicy(policy Policy[discord.Role]) ConfigOpt
- func WithStageInstanceCachePolicy(policy Policy[discord.StageInstance]) ConfigOpt
- func WithStickerCachePolicy(policy Policy[discord.Sticker]) ConfigOpt
- func WithThreadMemberCachePolicy(policy Policy[discord.ThreadMember]) ConfigOpt
- func WithVoiceStateCachePolicy(policy Policy[discord.VoiceState]) ConfigOpt
- type DefaultCache
- func (c *DefaultCache[T]) All() []T
- func (c *DefaultCache[T]) FindAll(cacheFindFunc FilterFunc[T]) []T
- func (c *DefaultCache[T]) FindFirst(cacheFindFunc FilterFunc[T]) (T, bool)
- func (c *DefaultCache[T]) ForEach(forEachFunc func(entity T))
- func (c *DefaultCache[T]) Get(id snowflake.ID) (T, bool)
- func (c *DefaultCache[T]) Len() int
- func (c *DefaultCache[T]) MapAll() map[snowflake.ID]T
- func (c *DefaultCache[T]) Put(id snowflake.ID, entity T)
- func (c *DefaultCache[T]) Remove(id snowflake.ID) (T, bool)
- func (c *DefaultCache[T]) RemoveIf(filterFunc FilterFunc[T])
- type FilterFunc
- type Flags
- type GroupedCache
- type GroupedFilterFunc
- type GuildCache
- type Policy
- func AllPolicies[T any](policies ...Policy[T]) Policy[T]
- func AnyPolicy[T any](policies ...Policy[T]) Policy[T]
- func PolicyChannelExclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]
- func PolicyChannelInclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]
- func PolicyMembersInVoice(caches Caches) Policy[discord.Member]
- func PolicyMembersInclude(guildIDs ...snowflake.ID) Policy[discord.Member]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PolicyDefault ¶ added in v0.10.3
PolicyDefault returns the default cache policy.
func PolicyMembersPending ¶ added in v0.12.0
PolicyMembersPending is a policy that will only cache members that are pending.
func PolicyNone ¶ added in v0.10.3
PolicyNone returns a policy that will never cache anything.
Types ¶
type Cache ¶
type Cache[T any] interface { // Get returns a copy of the entity with the given snowflake and a bool wheaten it was found or not. Get(id snowflake.ID) (T, bool) // Put stores the given entity with the given snowflake as key. If the entity is already present, it will be overwritten. Put(id snowflake.ID, entity T) // Remove removes the entity with the given snowflake as key and returns a copy of the entity and a bool whether it was removed or not. Remove(id snowflake.ID) (T, bool) // RemoveIf removes all entities that pass the given FilterFunc RemoveIf(filterFunc FilterFunc[T]) // Len returns the number of entities in the cache. Len() int // All returns a copy of all entities in this cache as a slice. All() []T // MapAll returns a copy of all entities in this cache as a map. MapAll() map[snowflake.ID]T // FindFirst returns the first entity that passes the given FilterFunc and a bool whether it was found or not. FindFirst(cacheFindFunc FilterFunc[T]) (T, bool) // FindAll returns all entities that pass the given FilterFunc as a slice. FindAll(cacheFindFunc FilterFunc[T]) []T // ForEach calls the given function for each entity in the cache. ForEach(func(entity T)) }
Cache is a simple key value store. They key is always a snowflake.ID. The cache provides a simple way to store and retrieve entities. But is not guaranteed to be thread safe as this depends on the underlying implementation.
func NewCache ¶
NewCache returns a new DefaultCache implementation which filter the entities after the gives Flags and Policy. This cache implementation is thread safe and can be used in multiple goroutines without any issues. It also only hands out copies to the entities. Regardless these entities should be handles as immutable.
type Caches ¶
type Caches interface { // CacheFlags returns the current configured FLags of the caches. CacheFlags() Flags // GetMemberPermissions returns the calculated permissions of the given member. // This requires the FlagRoles to be set. GetMemberPermissions(member discord.Member) discord.Permissions // GetMemberPermissionsInChannel returns the calculated permissions of the given member in the given channel. // This requires the FlagRoles and FlagChannels to be set. GetMemberPermissionsInChannel(channel discord.GuildChannel, member discord.Member) discord.Permissions // MemberRoles returns all roles of the given member. // This requires the FlagRoles to be set. MemberRoles(member discord.Member) []discord.Role // AudioChannelMembers returns all members which are in the given audio channel. // This requires the FlagVoiceStates to be set. AudioChannelMembers(channel discord.GuildAudioChannel) []discord.Member // GetSelfUser returns the current bot user. // This is only available after we received the discord.GatewayEventTypeReady event. GetSelfUser() (discord.OAuth2User, bool) // PutSelfUser overrides the current bot user. PutSelfUser(user discord.OAuth2User) // GetSelfMember returns the current bot member from the given guildID. // This is only available after we received the discord.GatewayEventTypeGuildCreate event for the given guildID. GetSelfMember(guildID snowflake.ID) (discord.Member, bool) // Roles returns the role cache. Roles() GroupedCache[discord.Role] // Members returns the member cache. Members() GroupedCache[discord.Member] // ThreadMembers returns the thread member cache. ThreadMembers() GroupedCache[discord.ThreadMember] // Presences returns the presence cache. Presences() GroupedCache[discord.Presence] // VoiceStates returns the voice state cache. VoiceStates() GroupedCache[discord.VoiceState] // Messages returns the message cache. Messages() GroupedCache[discord.Message] // Emojis returns the emoji cache. Emojis() GroupedCache[discord.Emoji] // Stickers returns the sticker cache. Stickers() GroupedCache[discord.Sticker] // Guilds returns the guild cache. Guilds() GuildCache // Channels returns the channel cache. Channels() ChannelCache // StageInstances returns the stage instance cache. StageInstances() GroupedCache[discord.StageInstance] // GuildScheduledEvents returns the guild scheduled event cache. GuildScheduledEvents() GroupedCache[discord.GuildScheduledEvent] }
Caches combines all different entity caches into one with some utility methods.
type ChannelCache ¶
type ChannelCache interface { Cache[discord.Channel] // GuildChannels returns all discord.GuildChannel in a guild and a bool indicating if it exists. GuildChannels(guildID snowflake.ID) []discord.GuildChannel // GuildThreadsInChannel returns all discord.GuildThread from the ChannelCache and a bool indicating if it exists. GuildThreadsInChannel(channelID snowflake.ID) []discord.GuildThread // GetGuildChannel returns a discord.GuildChannel from the ChannelCache and a bool indicating if it exists. GetGuildChannel(channelID snowflake.ID) (discord.GuildChannel, bool) // GetMessageChannel returns a discord.MessageChannel from the ChannelCache and a bool indicating if it exists. GetMessageChannel(channelID snowflake.ID) (discord.MessageChannel, bool) // GetGuildMessageChannel returns a discord.GuildMessageChannel from the ChannelCache and a bool indicating if it exists. GetGuildMessageChannel(channelID snowflake.ID) (discord.GuildMessageChannel, bool) // GetGuildThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists. GetGuildThread(channelID snowflake.ID) (discord.GuildThread, bool) // GetGuildAudioChannel returns a discord.GetGuildAudioChannel from the ChannelCache and a bool indicating if it exists. GetGuildAudioChannel(channelID snowflake.ID) (discord.GuildAudioChannel, bool) // GetGuildTextChannel returns a discord.GuildTextChannel from the ChannelCache and a bool indicating if it exists. GetGuildTextChannel(channelID snowflake.ID) (discord.GuildTextChannel, bool) // GetDMChannel returns a discord.DMChannel from the ChannelCache and a bool indicating if it exists. GetDMChannel(channelID snowflake.ID) (discord.DMChannel, bool) // GetGuildVoiceChannel returns a discord.GuildVoiceChannel from the ChannelCache and a bool indicating if it exists. GetGuildVoiceChannel(channelID snowflake.ID) (discord.GuildVoiceChannel, bool) // GetGuildCategoryChannel returns a discord.GuildCategoryChannel from the ChannelCache and a bool indicating if it exists. GetGuildCategoryChannel(channelID snowflake.ID) (discord.GuildCategoryChannel, bool) // GetGuildNewsChannel returns a discord.GuildNewsChannel from the ChannelCache and a bool indicating if it exists. GetGuildNewsChannel(channelID snowflake.ID) (discord.GuildNewsChannel, bool) // GetGuildNewsThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists. GetGuildNewsThread(channelID snowflake.ID) (discord.GuildThread, bool) // GetGuildPublicThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists. GetGuildPublicThread(channelID snowflake.ID) (discord.GuildThread, bool) // GetGuildPrivateThread returns a discord.GuildThread from the ChannelCache and a bool indicating if it exists. GetGuildPrivateThread(channelID snowflake.ID) (discord.GuildThread, bool) // GetGuildStageVoiceChannel returns a discord.GuildStageVoiceChannel from the ChannelCache and a bool indicating if it exists. GetGuildStageVoiceChannel(channelID snowflake.ID) (discord.GuildStageVoiceChannel, bool) }
ChannelCache is a Cache for all channel types
func NewChannelCache ¶
func NewChannelCache(flags Flags, policy Policy[discord.Channel]) ChannelCache
NewChannelCache returns a new channelCacheImpl with the given flags and policy. channelCacheImpl is thread safe and can be used in multiple goroutines.
type Config ¶
type Config struct { CacheFlags Flags GuildCachePolicy Policy[discord.Guild] ChannelCachePolicy Policy[discord.Channel] StageInstanceCachePolicy Policy[discord.StageInstance] GuildScheduledEventCachePolicy Policy[discord.GuildScheduledEvent] RoleCachePolicy Policy[discord.Role] MemberCachePolicy Policy[discord.Member] ThreadMemberCachePolicy Policy[discord.ThreadMember] PresenceCachePolicy Policy[discord.Presence] VoiceStateCachePolicy Policy[discord.VoiceState] MessageCachePolicy Policy[discord.Message] EmojiCachePolicy Policy[discord.Emoji] StickerCachePolicy Policy[discord.Sticker] }
Config lets you configure your Caches instance.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with sensible defaults.
type ConfigOpt ¶
type ConfigOpt func(config *Config)
ConfigOpt is a type alias for a function that takes a Config and is used to configure your Caches.
func WithCacheFlags ¶
WithCacheFlags sets the Flags of the Config.
func WithChannelCachePolicy ¶ added in v0.10.3
WithChannelCachePolicy sets the Policy[discord.Channel] of the Config.
func WithEmojiCachePolicy ¶ added in v0.10.3
WithEmojiCachePolicy sets the Policy[discord.Emoji] of the Config.
func WithGuildCachePolicy ¶ added in v0.10.3
WithGuildCachePolicy sets the Policy[discord.Guild] of the Config.
func WithGuildScheduledEventCachePolicy ¶ added in v0.10.3
func WithGuildScheduledEventCachePolicy(policy Policy[discord.GuildScheduledEvent]) ConfigOpt
WithGuildScheduledEventCachePolicy sets the Policy[discord.GuildScheduledEvent] of the Config.
func WithMemberCachePolicy ¶
WithMemberCachePolicy sets the Policy[discord.Member] of the Config.
func WithMessageCachePolicy ¶
WithMessageCachePolicy sets the Policy[discord.Message] of the Config.
func WithPresenceCachePolicy ¶ added in v0.10.3
WithPresenceCachePolicy sets the Policy[discord.Presence] of the Config.
func WithRoleCachePolicy ¶ added in v0.10.3
WithRoleCachePolicy sets the Policy[discord.Role] of the Config.
func WithStageInstanceCachePolicy ¶ added in v0.10.3
func WithStageInstanceCachePolicy(policy Policy[discord.StageInstance]) ConfigOpt
WithStageInstanceCachePolicy sets the Policy[discord.Guild] of the Config.
func WithStickerCachePolicy ¶ added in v0.10.3
WithStickerCachePolicy sets the Policy[discord.Sticker] of the Config.
func WithThreadMemberCachePolicy ¶ added in v0.10.3
func WithThreadMemberCachePolicy(policy Policy[discord.ThreadMember]) ConfigOpt
WithThreadMemberCachePolicy sets the Policy[discord.ThreadMember] of the Config.
func WithVoiceStateCachePolicy ¶ added in v0.10.3
func WithVoiceStateCachePolicy(policy Policy[discord.VoiceState]) ConfigOpt
WithVoiceStateCachePolicy sets the Policy[discord.VoiceState] of the Config.
type DefaultCache ¶
type DefaultCache[T any] struct { // contains filtered or unexported fields }
DefaultCache is a simple thread safe cache key value store.
func (*DefaultCache[T]) All ¶
func (c *DefaultCache[T]) All() []T
func (*DefaultCache[T]) FindAll ¶
func (c *DefaultCache[T]) FindAll(cacheFindFunc FilterFunc[T]) []T
func (*DefaultCache[T]) FindFirst ¶
func (c *DefaultCache[T]) FindFirst(cacheFindFunc FilterFunc[T]) (T, bool)
func (*DefaultCache[T]) ForEach ¶
func (c *DefaultCache[T]) ForEach(forEachFunc func(entity T))
func (*DefaultCache[T]) Get ¶
func (c *DefaultCache[T]) Get(id snowflake.ID) (T, bool)
func (*DefaultCache[T]) Len ¶ added in v0.12.0
func (c *DefaultCache[T]) Len() int
func (*DefaultCache[T]) MapAll ¶
func (c *DefaultCache[T]) MapAll() map[snowflake.ID]T
func (*DefaultCache[T]) Put ¶
func (c *DefaultCache[T]) Put(id snowflake.ID, entity T)
func (*DefaultCache[T]) Remove ¶
func (c *DefaultCache[T]) Remove(id snowflake.ID) (T, bool)
func (*DefaultCache[T]) RemoveIf ¶
func (c *DefaultCache[T]) RemoveIf(filterFunc FilterFunc[T])
type Flags ¶
type Flags int
Flags are used to enable/disable certain internal caches
const ( FlagGuilds Flags = 1 << iota FlagGuildScheduledEvents FlagMembers FlagThreadMembers FlagMessages FlagPresences FlagChannels FlagRoles FlagEmojis FlagStickers FlagVoiceStates FlagStageInstances FlagsNone Flags = 0 FlagsDefault = FlagsNone FlagsAll = FlagGuilds | FlagGuildScheduledEvents | FlagChannels | FlagRoles | FlagEmojis | FlagStickers | FlagVoiceStates | FlagStageInstances | FlagPresences )
values for CacheFlags
type GroupedCache ¶
type GroupedCache[T any] interface { // Get returns a copy of the entity with the given groupID and ID and a bool wheaten it was found or not. Get(groupID snowflake.ID, id snowflake.ID) (T, bool) // Put stores the given entity with the given groupID and ID as key. If the entity is already present, it will be overwritten. Put(groupID snowflake.ID, id snowflake.ID, entity T) // Remove removes the entity with the given groupID and ID as key and returns a copy of the entity and a bool whether it was removed or not. Remove(groupID snowflake.ID, id snowflake.ID) (T, bool) // RemoveAll removes all entities in the given groupID. RemoveAll(groupID snowflake.ID) // RemoveIf removes all entities that pass the given GroupedFilterFunc RemoveIf(filterFunc GroupedFilterFunc[T]) // Len returns the total number of entities in the cache. Len() int // GroupLen returns the number of entities in the cache within the groupID. GroupLen(groupID snowflake.ID) int // All returns a copy of all entities in the cache. All() map[snowflake.ID][]T // GroupAll returns a copy of all entities in a specific group. GroupAll(groupID snowflake.ID) []T // MapAll returns a copy of all entities in the cache as a map. MapAll() map[snowflake.ID]map[snowflake.ID]T // MapGroupAll returns a copy of all entities in a specific group as a map. MapGroupAll(groupID snowflake.ID) map[snowflake.ID]T // FindFirst returns the first entity that passes the given GroupedFilterFunc. FindFirst(cacheFindFunc GroupedFilterFunc[T]) (T, bool) // GroupFindFirst returns the first entity that passes the given GroupedFilterFunc within the groupID. GroupFindFirst(groupID snowflake.ID, cacheFindFunc GroupedFilterFunc[T]) (T, bool) // FindAll returns all entities that pass the given GroupedFilterFunc. FindAll(cacheFindFunc GroupedFilterFunc[T]) []T // GroupFindAll returns all entities that pass the given GroupedFilterFunc within the groupID. GroupFindAll(groupID snowflake.ID, cacheFindFunc GroupedFilterFunc[T]) []T // ForEach calls the given function for each entity in the cache. ForEach(func(groupID snowflake.ID, entity T)) // GroupForEach calls the given function for each entity in the cache within the groupID. GroupForEach(groupID snowflake.ID, forEachFunc func(entity T)) }
GroupedCache is a simple key value store grouped by a snowflake.ID. They key is always a snowflake.ID. The cache provides a simple way to store and retrieve entities. But is not guaranteed to be thread safe as this depends on the underlying implementation.
func NewGroupedCache ¶
func NewGroupedCache[T any](flags Flags, neededFlags Flags, policy Policy[T]) GroupedCache[T]
NewGroupedCache returns a new default GroupedCache with the provided flags, neededFlags and policy.
type GroupedFilterFunc ¶ added in v0.12.0
GroupedFilterFunc is used to filter grouped cached entities.
type GuildCache ¶
type GuildCache interface { Cache[discord.Guild] // SetReady sets the specified guildID as ready in the specific shard. SetReady(shardID int, guildID snowflake.ID) // SetUnready sets the specified guildID as not ready in the specific shard. SetUnready(shardID int, guildID snowflake.ID) // IsUnready returns a bool indicating if the specified guildID is ready or not in the specific shard. IsUnready(shardID int, guildID snowflake.ID) bool // UnreadyGuilds returns all guildIDs that are not ready in the specific shard. UnreadyGuilds(shardID int) []snowflake.ID SetUnavailable(guildID snowflake.ID) // SetAvailable sets the specified guildID as available. SetAvailable(guildID snowflake.ID) IsUnavailable(guildID snowflake.ID) bool UnavailableGuilds() []snowflake.ID }
GuildCache is a Cache for guilds. It also keeps track of unready and unavailable guilds.
func NewGuildCache ¶
func NewGuildCache(flags Flags, policy Policy[discord.Guild]) GuildCache
NewGuildCache a new guildCacheImpl with the given flags and policy. guildCacheImpl is thread safe and can be used in multiple goroutines.
type Policy ¶
Policy can be used to define your own policy for when entities should be cached.
func AllPolicies ¶
AllPolicies is a shorthand for CachePolicy.And(CachePolicy).And(CachePolicy) etc.
func PolicyChannelExclude ¶ added in v0.12.0
func PolicyChannelExclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]
PolicyChannelExclude returns a policy that will not cache channels of the given types.
func PolicyChannelInclude ¶ added in v0.12.0
func PolicyChannelInclude(channelTypes ...discord.ChannelType) Policy[discord.Channel]
PolicyChannelInclude returns a policy that will only cache channels of the given types.
func PolicyMembersInVoice ¶ added in v0.12.0
PolicyMembersInVoice returns a policy that will only cache members that are connected to an audio channel.
func PolicyMembersInclude ¶ added in v0.12.0
PolicyMembersInclude returns a policy that will only cache members of the given guilds.