Module: Discordrb::Cache
- Included in:
- Bot
- Defined in:
- lib/discordrb/cache.rb
Overview
This mixin module does caching stuff for the library. It conveniently separates the logic behind the caching (like, storing the user hashes or making API calls to retrieve things) from the Bot that actually uses it.
Instance Method Summary collapse
-
#channel(id, server = nil) ⇒ Channel?
(also: #group_channel)
Gets a channel given its ID.
-
#ensure_channel(data, server = nil) ⇒ Channel
Ensures a given channel object is cached and if not, cache it from the given data hash.
-
#ensure_server(data, force_cache = false) ⇒ Server
Ensures a given server object is cached and if not, cache it from the given data hash.
-
#ensure_thread_member(data) ⇒ Object
Ensures a given thread member object is cached.
-
#ensure_user(data) ⇒ User
Ensures a given user object is cached and if not, cache it from the given data hash.
-
#find_channel(channel_name, server_name = nil, type: nil) ⇒ Array<Channel>
Finds a channel given its name and optionally the name of the server it is in.
-
#find_user(username, discrim = nil) ⇒ Object
Finds a user given its username or username & discriminator.
-
#init_cache ⇒ Object
Initializes this cache.
-
#invite(invite) ⇒ Invite
Gets information about an invite.
-
#member(server_or_id, user_id) ⇒ Member?
Gets a member by both IDs, or
Server
and user ID. -
#pm_channel(id) ⇒ Channel
(also: #private_channel)
Creates a PM channel for the given user ID, or if one exists already, returns that one.
-
#request_chunks(id) ⇒ Object
Requests member chunks for a given server ID.
-
#resolve_invite_code(invite) ⇒ String
Gets the code for an invite.
-
#server(id) ⇒ Server?
Gets a server by its ID.
-
#server_preview(id) ⇒ ServerPreview?
Get a server preview.
-
#user(id) ⇒ User?
Gets a user by its ID.
-
#voice_regions ⇒ Object
Returns or caches the available voice regions.
Instance Method Details
#channel(id, server = nil) ⇒ Channel? Also known as: group_channel
Gets a channel given its ID. This queries the internal channel cache, and if the channel doesn't exist in there, it will get the data from Discord.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/discordrb/cache.rb', line 47 def channel(id, server = nil) id = id.resolve_id debug("Obtaining data for channel with id #{id}") return @channels[id] if @channels[id] begin response = API::Channel.resolve(token, id) rescue Discordrb::Errors::UnknownChannel return nil end channel = Channel.new(JSON.parse(response), self, server) @channels[id] = channel end |
#ensure_channel(data, server = nil) ⇒ Channel
Ensures a given channel object is cached and if not, cache it from the given data hash.
181 182 183 184 185 186 187 |
# File 'lib/discordrb/cache.rb', line 181 def ensure_channel(data, server = nil) if @channels.include?(data['id'].to_i) @channels[data['id'].to_i] else @channels[data['id'].to_i] = Channel.new(data, self, server) end end |
#ensure_server(data, force_cache = false) ⇒ Server
Ensures a given server object is cached and if not, cache it from the given data hash.
167 168 169 170 171 172 173 174 175 |
# File 'lib/discordrb/cache.rb', line 167 def ensure_server(data, force_cache = false) if @servers.include?(data['id'].to_i) server = @servers[data['id'].to_i] server.update_data(data) if force_cache server else @servers[data['id'].to_i] = Server.new(data, self) end end |
#ensure_thread_member(data) ⇒ Object
Ensures a given thread member object is cached.
191 192 193 194 195 196 197 |
# File 'lib/discordrb/cache.rb', line 191 def ensure_thread_member(data) thread_id = data['id'].to_i user_id = data['user_id'].to_i @thread_members[thread_id] ||= {} @thread_members[thread_id][user_id] = data.slice('join_timestamp', 'flags') end |
#ensure_user(data) ⇒ User
Ensures a given user object is cached and if not, cache it from the given data hash.
154 155 156 157 158 159 160 |
# File 'lib/discordrb/cache.rb', line 154 def ensure_user(data) if @users.include?(data['id'].to_i) @users[data['id'].to_i] else @users[data['id'].to_i] = User.new(data, self) end end |
#find_channel(channel_name, server_name = nil, type: nil) ⇒ Array<Channel>
Finds a channel given its name and optionally the name of the server it is in.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/discordrb/cache.rb', line 234 def find_channel(channel_name, server_name = nil, type: nil) results = [] if /<#(?<id>\d+)>?/ =~ channel_name # Check for channel mentions separately return [channel(id)] end @servers.each_value do |server| server.channels.each do |channel| results << channel if channel.name == channel_name && (server_name || server.name) == server.name && (!type || (channel.type == type)) end end results end |
#find_user(username) ⇒ Array<User> #find_user(username, discrim) ⇒ User?
This method only searches through users that have been cached. Users that have not yet been cached by the bot but still share a connection with the user (mutual server) will not be found.
Finds a user given its username or username & discriminator.
268 269 270 271 272 273 |
# File 'lib/discordrb/cache.rb', line 268 def find_user(username, discrim = nil) users = @users.values.find_all { |e| e.username == username } return users.find { |u| u.discrim == discrim } if discrim users end |
#init_cache ⇒ Object
Initializes this cache
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/discordrb/cache.rb', line 15 def init_cache @users = {} @voice_regions = {} @servers = {} @channels = {} @pm_channels = {} @thread_members = {} @server_previews = {} end |
#invite(invite) ⇒ Invite
Gets information about an invite.
223 224 225 226 |
# File 'lib/discordrb/cache.rb', line 223 def invite(invite) code = resolve_invite_code(invite) Invite.new(JSON.parse(API::Invite.resolve(token, code)), self) end |
#member(server_or_id, user_id) ⇒ Member?
Gets a member by both IDs, or Server
and user ID.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/discordrb/cache.rb', line 104 def member(server_or_id, user_id) server_id = server_or_id.resolve_id user_id = user_id.resolve_id server = server_or_id.is_a?(Server) ? server_or_id : self.server(server_id) return server.member(user_id) if server.member_cached?(user_id) LOGGER.out("Resolving member #{server_id} on server #{user_id}") begin response = API::Server.resolve_member(token, server_id, user_id) rescue Discordrb::Errors::UnknownUser, Discordrb::Errors::UnknownMember return nil end member = Member.new(JSON.parse(response), server, self) server.cache_member(member) end |
#pm_channel(id) ⇒ Channel Also known as: private_channel
Creates a PM channel for the given user ID, or if one exists already, returns that one. It is recommended that you use User#pm instead, as this is mainly for internal use. However, usage of this method may be unavoidable if only the user ID is known.
126 127 128 129 130 131 132 133 134 |
# File 'lib/discordrb/cache.rb', line 126 def pm_channel(id) id = id.resolve_id return @pm_channels[id] if @pm_channels[id] debug("Creating pm channel with user id #{id}") response = API::User.create_pm(token, id) channel = Channel.new(JSON.parse(response), self) @pm_channels[id] = channel end |
#request_chunks(id) ⇒ Object
Requests member chunks for a given server ID.
201 202 203 |
# File 'lib/discordrb/cache.rb', line 201 def request_chunks(id) @gateway.send_request_members(id, '', 0) end |
#resolve_invite_code(invite) ⇒ String
Gets the code for an invite.
214 215 216 217 218 |
# File 'lib/discordrb/cache.rb', line 214 def resolve_invite_code(invite) invite = invite.code if invite.is_a? Discordrb::Invite invite = invite[(invite.rindex('/') + 1)..] if invite.start_with?('http', 'discord.gg') invite end |
#server(id) ⇒ Server?
This can only resolve servers the bot is currently in.
Gets a server by its ID.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/discordrb/cache.rb', line 86 def server(id) id = id.resolve_id return @servers[id] if @servers[id] LOGGER.out("Resolving server #{id}") begin response = API::Server.resolve(token, id) rescue Discordrb::Errors::NoPermission return nil end server = Server.new(JSON.parse(response), self) @servers[id] = server end |
#server_preview(id) ⇒ ServerPreview?
Get a server preview. If the bot isn't a member of the server, the server must be discoverable.
141 142 143 144 145 146 147 148 149 |
# File 'lib/discordrb/cache.rb', line 141 def server_preview(id) id = id.resolve_id return @server_previews[id] if @server_previews[id] response = JSON.parse(API::Server.preview(token, id)) @server_previews[id] = ServerPreview.new(response, self) rescue StandardError nil end |
#user(id) ⇒ User?
This can only resolve users known by the bot (i.e. that share a server with the bot).
Gets a user by its ID.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/discordrb/cache.rb', line 68 def user(id) id = id.resolve_id return @users[id] if @users[id] LOGGER.out("Resolving user #{id}") begin response = API::User.resolve(token, id) rescue Discordrb::Errors::UnknownUser return nil end user = User.new(JSON.parse(response), self) @users[id] = user end |
#voice_regions ⇒ Object
Returns or caches the available voice regions
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/discordrb/cache.rb', line 29 def voice_regions return @voice_regions unless @voice_regions.empty? regions = JSON.parse API.voice_regions(token) regions.each do |data| @voice_regions[data['id']] = VoiceRegion.new(data) end @voice_regions end |