class Discord::Cache
- Discord::Cache
- Reference
- Object
Overview
A cache is a utility class that stores various kinds of Discord objects,
like User
s, Role
s etc. Its purpose is to reduce both the load on
Discord's servers and reduce the latency caused by having to do an API call.
It is recommended to use caching for bots that interact heavily with
Discord-provided data, like for example administration bots, as opposed to
bots that only interact by sending and receiving messages. For that latter
kind, caching is usually even counter-productive as it only unnecessarily
increases memory usage.
Caching can either be used standalone, in a purely REST-based way:
client = Discord::Client.new(token: "Bot token", client_id: 123_u64)
cache = Discord::Cache.new(client)
puts cache.resolve_user(66237334693085184) # will perform API call
puts cache.resolve_user(66237334693085184) # will not perform an API call, as the data is now cached
It can also be integrated more deeply into a Client
(specifically one that
uses a gateway connection) to reduce cache misses even more by automatically
caching data received over the gateway:
client = Discord::Client.new(token: "Bot token", client_id: 123_u64)
cache = Discord::Cache.new(client)
client.cache = cache # Integrate the cache into the client
Note that if a cache is not used this way, its data will slowly go out of sync with Discord, and unless it is used in an environment with few changes likely to occur, a client without a gateway connection should probably refrain from caching at all.
Defined in:
discordcr/cache.crConstructors
-
.new(client : Client)
Creates a new cache with a client that requests (in case of cache misses) should be done on.
Instance Method Summary
-
#add_guild_channel(guild_id : UInt64 | Snowflake, channel_id : UInt64 | Snowflake)
Marks a channel, identified by the channel_id, as belonging to a particular guild, identified by the guild_id.
-
#add_guild_role(guild_id : UInt64 | Snowflake, role_id : UInt64 | Snowflake)
Marks a role, identified by the role_id, as belonging to a particular guild, identified by the guild_id.
-
#add_guild_stage_instance(guild_id : UInt64 | Snowflake, instance_id : UInt64 | Snowflake)
Marks a Stage instance, identified by the instance_id, as belonging to a particular guild, identified by the guild_id.
-
#cache(member : GuildMember, guild_id : UInt64 | Snowflake)
Adds a specific member to the cache, given the guild_id it is on.
-
#cache(user : User)
Adds a specific user to the cache.
-
#cache(channel : Channel)
Adds a specific channel to the cache.
-
#cache(guild : Guild)
Adds a specific guild to the cache.
-
#cache(role : Role)
Adds a specific role to the cache.
-
#cache(stage_instance : StageInstance)
Adds a specific Stage instance to the cache.
-
#cache(voice_state : VoiceState)
Adds a specific voice state to the cache.
-
#cache_current_user(current_user : User)
Caches the current user.
-
#cache_dm_channel(channel_id : UInt64 | Snowflake, recipient_id : UInt64 | Snowflake)
Adds a particular DM channel to the cache, given the channel_id and the recipient_id.
-
#cache_multiple_members(members : Array(GuildMember), guild_id : UInt64 | Snowflake)
Adds multiple members at once to the cache, given the guild_id they all share.
-
#channels : Hash(UInt64, Discord::Channel)
A map of cached channels, i.
-
#delete_channel(id : UInt64 | Snowflake)
Deletes a channel from the cache given its ID.
-
#delete_current_user
Deletes the current user from the cache, if that will ever be necessary.
-
#delete_dm_channel(recipient_id : UInt64 | Snowflake)
Deletes a DM channel with a particular user given the recipient_id.
-
#delete_guild(id : UInt64 | Snowflake)
Deletes a guild from the cache given its ID.
-
#delete_member(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake)
Deletes a member from the cache given its user_id and the guild_id it is on.
-
#delete_role(id : UInt64 | Snowflake)
Deletes a role from the cache given its ID.
-
#delete_stage_instance(id : UInt64 | Snowflake)
Deletes a stage instance from the cache given its ID.
-
#delete_user(id : UInt64 | Snowflake)
Deletes a user from the cache given its ID.
-
#delete_voice_state(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake)
Deletes voice state for user in guild from cache.
-
#dm_channels : Hash(UInt64, UInt64)
Mapping of users to the respective DM channels the bot has open with them, represented as {user ID => channel ID}.
-
#guild_channels(guild_id : UInt64 | Snowflake) : Array(UInt64)
Returns all channels of a guild, identified by its guild_id.
-
#guild_channels : Hash(UInt64, Array(UInt64))
Mapping of guilds to the channels on them, represented as {guild ID => [channel IDs]}.
-
#guild_roles(guild_id : UInt64 | Snowflake) : Array(UInt64)
Returns all roles of a guild, identified by its guild_id.
-
#guild_roles : Hash(UInt64, Array(UInt64))
Mapping of guilds to the roles on them, represented as {guild ID => [role IDs]}.
-
#guild_stage_instances(guild_id : UInt64 | Snowflake) : Array(UInt64)
Returns all Stage instances of a guild, identified by its guild_id.
-
#guild_stage_instances : Hash(UInt64, Array(UInt64))
Mapping of guilds to the Stage instances on them, represented as {guild ID => [stage instance IDs]}.
-
#guilds : Hash(UInt64, Discord::Guild)
A map of guilds (servers) the bot is on.
-
#members : Hash(UInt64, Hash(UInt64, Discord::GuildMember))
A double map of members on servers, represented as {guild ID => {user ID => member}}.
-
#remove_guild_channel(guild_id : UInt64 | Snowflake, channel_id : UInt64 | Snowflake)
Marks a channel as not belonging to a particular guild anymore.
-
#remove_guild_role(guild_id : UInt64 | Snowflake, role_id : UInt64 | Snowflake)
Marks a role as not belonging to a particular guild anymore.
-
#remove_guild_stage_instance(guild_id : UInt64 | Snowflake, instance_id : UInt64 | Snowflake)
Marks a Stage instance as not belonging to a particular guild anymore.
-
#resolve_channel(id : UInt64 | Snowflake) : Channel
Resolves a channel by its ID.
-
#resolve_current_user : User
Resolves the current user's profile.
-
#resolve_dm_channel(recipient_id : UInt64 | Snowflake) : UInt64
Resolves the ID of a DM channel with a particular user by the recipient's recipient_id.
-
#resolve_guild(id : UInt64 | Snowflake) : Guild
Resolves a guild by its ID.
-
#resolve_member(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake) : GuildMember
Resolves a member by the guild_id of the guild the member is on, and the user_id of the member itself.
-
#resolve_role(id : UInt64 | Snowflake) : Role
Resolves a role by its ID.
-
#resolve_stage_instance(id : UInt64 | Snowflake) : StageInstance
Resolves a Stage instance by its ID.
-
#resolve_user(id : UInt64 | Snowflake) : User
Resolves a user by its ID.
-
#resolve_voice_state(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake) : VoiceState
Resolves a voice state by guild ID and user ID.
-
#roles : Hash(UInt64, Discord::Role)
A map of all roles on servers the bot is on.
-
#stage_instances : Hash(UInt64, Discord::StageInstance)
A map of cached stage instances, i.
-
#users : Hash(UInt64, Discord::User)
A map of cached users.
-
#voice_states : Hash(UInt64, Hash(UInt64, Discord::VoiceState))
Mapping of users in guild to voice states, represented as {guild ID => {user ID => voice state}}
Constructor Detail
Creates a new cache with a client that requests (in case of cache misses) should be done on.
Instance Method Detail
Marks a channel, identified by the channel_id, as belonging to a particular guild, identified by the guild_id.
Marks a role, identified by the role_id, as belonging to a particular guild, identified by the guild_id.
Marks a Stage instance, identified by the instance_id, as belonging to a particular guild, identified by the guild_id.
Adds a specific member to the cache, given the guild_id it is on.
Adds a particular DM channel to the cache, given the channel_id and the recipient_id.
Adds multiple members at once to the cache, given the guild_id they all share. This method exists to slightly reduce the overhead of processing chunks; outside of that it is likely not of much use.
A map of cached channels, i. e. all channels on all servers the bot is on, as well as all DM channels.
Deletes a channel from the cache given its ID.
Deletes a DM channel with a particular user given the recipient_id.
Deletes a guild from the cache given its ID.
Deletes a member from the cache given its user_id and the guild_id it is on.
Deletes a role from the cache given its ID.
Deletes a stage instance from the cache given its ID.
Deletes a user from the cache given its ID.
Deletes voice state for user in guild from cache.
Mapping of users to the respective DM channels the bot has open with them, represented as {user ID => channel ID}.
Returns all channels of a guild, identified by its guild_id.
Mapping of guilds to the channels on them, represented as {guild ID => [channel IDs]}.
Returns all roles of a guild, identified by its guild_id.
Mapping of guilds to the roles on them, represented as {guild ID => [role IDs]}.
Returns all Stage instances of a guild, identified by its guild_id.
Mapping of guilds to the Stage instances on them, represented as {guild ID => [stage instance IDs]}.
A map of guilds (servers) the bot is on. Doesn't ignore guilds temporarily deleted due to an outage; so if an outage is going on right now the affected guilds would be missing here too.
A double map of members on servers, represented as {guild ID => {user ID
=> member}}. Will only contain previously and currently online members as
well as all members that have been chunked (see
Client#request_guild_members
).
Marks a channel as not belonging to a particular guild anymore.
Marks a role as not belonging to a particular guild anymore.
Marks a Stage instance as not belonging to a particular guild anymore.
Resolves a channel by its ID. If the requested object is not cached, it will do an API call.
Resolves the current user's profile. Requires no parameters since the endpoint has none either. If there is a gateway connection this should always be cached.
Resolves the ID of a DM channel with a particular user by the recipient's recipient_id. If there is no such channel cached, one will be created.
Resolves a guild by its ID. If the requested object is not cached, it will do an API call.
Resolves a member by the guild_id of the guild the member is on, and the user_id of the member itself. An API request will be performed if the object is not cached.
Resolves a role by its ID. No API request will be performed if the role is not cached, because there is no endpoint for individual roles; however all roles should be cached at all times so it won't be a problem.
Resolves a Stage instance by its ID. An API request will be performed if the object is not cached.
Resolves a user by its ID. If the requested object is not cached, it will do an API call.
Resolves a voice state by guild ID and user ID. No API request will be performed if voice state is not cached, because there is no endpoint for it. If there is a gateway connection this should always be cached.
A map of all roles on servers the bot is on. Does not discriminate by guild, as role IDs are unique even across guilds.
A map of cached stage instances, i. e. all stage instances on all servers the bot is on.
A map of cached users. These aren't necessarily all the users in servers
the bot has access to, but rather all the users that have been seen by
the bot in the past (and haven't been deleted by means of #delete_user
).
Mapping of users in guild to voice states, represented as {guild ID => {user ID => voice state}}