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

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.

Parameters:

  • id (Integer)

    The channel ID for which to search for.

  • server (Server) (defaults to: nil)

    The server for which to search the channel for. If this isn't specified, it will be inferred using the API

Returns:

  • (Channel)

    The channel identified by the ID.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/discordrb/cache.rb', line 32

def channel(id, server = nil)
  id = id.resolve_id

  raise Discordrb::Errors::NoPermission if @restricted_channels.include? id

  debug("Obtaining data for channel with id #{id}")
  return @channels[id] if @channels[id]

  begin
    begin
      response = API::Channel.resolve(token, id)
    rescue RestClient::ResourceNotFound
      return nil
    end
    channel = Channel.new(JSON.parse(response), self, server)
    @channels[id] = channel
  rescue Discordrb::Errors::NoPermission
    debug "Tried to get access to restricted channel #{id}, blacklisting it"
    @restricted_channels << id
    raise
  end
end

#ensure_channel(data, server = nil) ⇒ Channel

Ensures a given channel object is cached and if not, cache it from the given data hash.

Parameters:

  • data (Hash)

    A data hash representing a channel.

  • server (Server, nil) (defaults to: nil)

    The server the channel is on, if known.

Returns:

  • (Channel)

    the channel represented by the data hash.



157
158
159
160
161
162
163
# File 'lib/discordrb/cache.rb', line 157

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) ⇒ Server

Ensures a given server object is cached and if not, cache it from the given data hash.

Parameters:

  • data (Hash)

    A data hash representing a server.

Returns:

  • (Server)

    the server represented by the data hash.



145
146
147
148
149
150
151
# File 'lib/discordrb/cache.rb', line 145

def ensure_server(data)
  if @servers.include?(data['id'].to_i)
    @servers[data['id'].to_i]
  else
    @servers[data['id'].to_i] = Server.new(data, self)
  end
end

#ensure_user(data) ⇒ User

Ensures a given user object is cached and if not, cache it from the given data hash.

Parameters:

  • data (Hash)

    A data hash representing a user.

Returns:

  • (User)

    the user represented by the data hash.



134
135
136
137
138
139
140
# File 'lib/discordrb/cache.rb', line 134

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.

Parameters:

  • channel_name (String)

    The channel to search for.

  • server_name (String) (defaults to: nil)

    The server to search for, or nil if only the channel should be searched for.

  • type (Integer, nil) (defaults to: nil)

    The type of channel to search for (0: text, 1: private, 2: voice, 3: group), or nil if any type of channel should be searched for

Returns:

  • (Array<Channel>)

    The array of channels that were found. May be empty if none were found.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/discordrb/cache.rb', line 200

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.values.each 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>

Finds a user given its username.

Parameters:

  • username (String)

    The username to look for.

Returns:

  • (Array<User>)

    The array of users that were found. May be empty if none were found.



220
221
222
# File 'lib/discordrb/cache.rb', line 220

def find_user(username)
  @users.values.find_all { |e| e.username == username }
end

#init_cacheObject

Initializes this cache



15
16
17
18
19
20
21
22
23
24
# File 'lib/discordrb/cache.rb', line 15

def init_cache
  @users = {}

  @servers = {}

  @channels = {}
  @pm_channels = {}

  @restricted_channels = []
end

#invite(invite) ⇒ Invite

Gets information about an invite.

Parameters:

Returns:

  • (Invite)

    The invite with information about the given invite URL.



189
190
191
192
# File 'lib/discordrb/cache.rb', line 189

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.

Parameters:

  • server_or_id (Server, Integer)

    The Server or server ID for which a member should be resolved

  • user_id (Integer)

    The ID of the user that should be resolved

Returns:

  • (Member, nil)

    The member identified by the IDs, or nil if none could be found



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/discordrb/cache.rb', line 97

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 RestClient::ResourceNotFound
    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.

Parameters:

  • id (Integer)

    The user ID to generate a private channel for.

Returns:

  • (Channel)

    A private channel for that user.



120
121
122
123
124
125
126
127
# File 'lib/discordrb/cache.rb', line 120

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.

Parameters:

  • id (Integer)

    The server ID to request chunks for.



167
168
169
# File 'lib/discordrb/cache.rb', line 167

def request_chunks(id)
  @gateway.send_request_members(id, '', 0)
end

#resolve_invite_code(invite) ⇒ String

Gets the code for an invite.

Parameters:

  • invite (String, Invite)

    The invite to get the code for. Possible formats are:

    • An Invite object
    • The code for an invite
    • A fully qualified invite URL (e. g. https://discordapp.com/invite/0A37aN7fasF7n83q)
    • A short invite URL with protocol (e. g. https://discord.gg/0A37aN7fasF7n83q)
    • A short invite URL without protocol (e. g. discord.gg/0A37aN7fasF7n83q)

Returns:

  • (String)

    Only the code for the invite.



180
181
182
183
184
# File 'lib/discordrb/cache.rb', line 180

def resolve_invite_code(invite)
  invite = invite.code if invite.is_a? Discordrb::Invite
  invite = invite[invite.rindex('/') + 1..-1] if invite.start_with?('http', 'discord.gg')
  invite
end

#server(id) ⇒ Server?

Note:

This can only resolve servers the bot is currently in.

Gets a server by its ID.

Parameters:

  • id (Integer)

    The server ID that should be resolved.

Returns:

  • (Server, nil)

    The server identified by the ID, or nil if it couldn't be found.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/discordrb/cache.rb', line 79

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 RestClient::ResourceNotFound
    return nil
  end
  server = Server.new(JSON.parse(response), self)
  @servers[id] = server
end

#user(id) ⇒ User?

Note:

This can only resolve users known by the bot (i.e. that share a server with the bot).

Gets a user by its ID.

Parameters:

  • id (Integer)

    The user ID that should be resolved.

Returns:

  • (User, nil)

    The user identified by the ID, or nil if it couldn't be found.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/discordrb/cache.rb', line 61

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 RestClient::ResourceNotFound
    return nil
  end
  user = User.new(JSON.parse(response), self)
  @users[id] = user
end