class

Discord::Cache

Inherits Reference / Object

A cache is a utility class that stores various kinds of Discord objects, like Users, Roles 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.

Constructors

new(client : Client)

Creates a new cache with a client that requests (in case of cache misses) should be done on.

Source

Instance methods

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.

Source
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.

Source
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.

Source
cache(member : GuildMember, guild_id : UInt64 | Snowflake)

Adds a specific member to the cache, given the guild_id it is on.

Source
cache(user : User)

Adds a specific user to the cache.

Source
cache(channel : Channel)

Adds a specific channel to the cache.

Source
cache(guild : Guild)

Adds a specific guild to the cache.

Source
cache(role : Role)

Adds a specific role to the cache.

Source
cache(stage_instance : StageInstance)

Adds a specific Stage instance to the cache.

Source
cache(voice_state : VoiceState)

Adds a specific voice state to the cache.

Source
cache_current_user(current_user : User)

Caches the current user.

Source
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.

Source
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. This method exists to slightly reduce the overhead of processing chunks; outside of that it is likely not of much use.

Source
channels

A map of cached channels, i. e. all channels on all servers the bot is on, as well as all DM channels.

Source
delete_channel(id : UInt64 | Snowflake)

Deletes a channel from the cache given its ID.

Source
delete_current_user

Deletes the current user from the cache, if that will ever be necessary.

Source
delete_dm_channel(recipient_id : UInt64 | Snowflake)

Deletes a DM channel with a particular user given the recipient_id.

Source
delete_guild(id : UInt64 | Snowflake)

Deletes a guild from the cache given its ID.

Source
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.

Source
delete_role(id : UInt64 | Snowflake)

Deletes a role from the cache given its ID.

Source
delete_stage_instance(id : UInt64 | Snowflake)

Deletes a stage instance from the cache given its ID.

Source
delete_user(id : UInt64 | Snowflake)

Deletes a user from the cache given its ID.

Source
delete_voice_state(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake)

Deletes voice state for user in guild from cache.

Source
dm_channels

Mapping of users to the respective DM channels the bot has open with them, represented as {user ID => channel ID}.

Source
guild_channels(guild_id : UInt64 | Snowflake) : Array(UInt64)

Returns all channels of a guild, identified by its guild_id.

Source
guild_channels

Mapping of guilds to the channels on them, represented as {guild ID => [channel IDs]}.

Source
guild_roles(guild_id : UInt64 | Snowflake) : Array(UInt64)

Returns all roles of a guild, identified by its guild_id.

Source
guild_roles

Mapping of guilds to the roles on them, represented as {guild ID => [role IDs]}.

Source
guild_stage_instances(guild_id : UInt64 | Snowflake) : Array(UInt64)

Returns all Stage instances of a guild, identified by its guild_id.

Source
guild_stage_instances

Mapping of guilds to the Stage instances on them, represented as {guild ID => [stage instance IDs]}.

Source
guilds

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.

Source
members

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).

Source
remove_guild_channel(guild_id : UInt64 | Snowflake, channel_id : UInt64 | Snowflake)

Marks a channel as not belonging to a particular guild anymore.

Source
remove_guild_role(guild_id : UInt64 | Snowflake, role_id : UInt64 | Snowflake)

Marks a role as not belonging to a particular guild anymore.

Source
remove_guild_stage_instance(guild_id : UInt64 | Snowflake, instance_id : UInt64 | Snowflake)

Marks a Stage instance as not belonging to a particular guild anymore.

Source
resolve_channel(id : UInt64 | Snowflake) : Channel

Resolves a channel by its ID. If the requested object is not cached, it will do an API call.

Source
resolve_current_user

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.

Source
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. If there is no such channel cached, one will be created.

Source
resolve_guild(id : UInt64 | Snowflake) : Guild

Resolves a guild by its ID. If the requested object is not cached, it will do an API call.

Source
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. An API request will be performed if the object is not cached.

Source
resolve_role(id : UInt64 | Snowflake) : Role

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.

Source
resolve_stage_instance(id : UInt64 | Snowflake) : StageInstance

Resolves a Stage instance by its ID. An API request will be performed if the object is not cached.

Source
resolve_user(id : UInt64 | Snowflake) : User

Resolves a user by its ID. If the requested object is not cached, it will do an API call.

Source
resolve_voice_state(guild_id : UInt64 | Snowflake, user_id : UInt64 | Snowflake) : VoiceState

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.

Source
roles

A map of all roles on servers the bot is on. Does not discriminate by guild, as role IDs are unique even across guilds.

Source
stage_instances

A map of cached stage instances, i. e. all stage instances on all servers the bot is on.

Source
users

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).

Source
voice_states

Mapping of users in guild to voice states, represented as {guild ID => {user ID => voice state}}

Source