Class: MijDiscord::Cache::ServerCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mij-discord/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, bot) ⇒ ServerCache

Returns a new instance of ServerCache.



150
151
152
153
154
# File 'lib/mij-discord/cache.rb', line 150

def initialize(server, bot)
  @server, @bot = server, bot

  reset
end

Instance Method Details

#get_channel(key, local: false) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/mij-discord/cache.rb', line 195

def get_channel(key, local: false)
  id = key&.to_id
  return @channels[id] if @channels.has_key?(id)

  channel = @bot.cache.get_channel(key, local: local)
  return nil unless channel&.server == @server

  @channels[channel.id] = channel
end

#get_member(key, local: false) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mij-discord/cache.rb', line 172

def get_member(key, local: false)
  id = key&.to_id
  return @members[id] if @members.has_key?(id)
  return nil if local

  begin
    response = MijDiscord::Core::API::Server.resolve_member(@bot.auth, @server.id, id)
  rescue RestClient::ResourceNotFound
    return nil
  end

  @members[id] = MijDiscord::Data::Member.new(JSON.parse(response), @server, @bot)
end

#get_role(key, local: false) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/mij-discord/cache.rb', line 186

def get_role(key, local: false)
  id = key&.to_id
  return @roles[id] if @roles.has_key?(id)
  return nil if local

  # No API to get individual role
  nil
end

#list_channelsObject



168
169
170
# File 'lib/mij-discord/cache.rb', line 168

def list_channels
  @channels.values
end

#list_membersObject



160
161
162
# File 'lib/mij-discord/cache.rb', line 160

def list_members
  @members.values
end

#list_rolesObject



164
165
166
# File 'lib/mij-discord/cache.rb', line 164

def list_roles
  @roles.values
end

#put_channel(data, update: false) ⇒ Object



225
226
227
228
# File 'lib/mij-discord/cache.rb', line 225

def put_channel(data, update: false)
  channel = @bot.cache.put_channel(data, @server, update: update)
  @channels[channel.id] = channel
end

#put_channel!(channel) ⇒ Object



230
231
232
# File 'lib/mij-discord/cache.rb', line 230

def put_channel!(channel)
  @channels[channel.id] = channel
end

#put_member(data, update: false) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/mij-discord/cache.rb', line 205

def put_member(data, update: false)
  id = data['user']['id'].to_i
  if @members.has_key?(id)
    @members[id].update_data(data) if update
    return @members[id]
  end

  @members[id] = MijDiscord::Data::Member.new(data, @server, @bot)
end

#put_role(data, update: false) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/mij-discord/cache.rb', line 215

def put_role(data, update: false)
  id = data['id'].to_i
  if @roles.has_key?(id)
    @roles[id].update_data(data) if update
    return @roles[id]
  end

  @roles[id] = MijDiscord::Data::Role.new(data, @server, @bot)
end

#remove_channel(key) ⇒ Object



242
243
244
245
246
247
# File 'lib/mij-discord/cache.rb', line 242

def remove_channel(key)
  channel = @channels.delete(key&.to_id)
  @bot.cache.remove_channel(key) if channel

  channel
end

#remove_member(key) ⇒ Object



234
235
236
# File 'lib/mij-discord/cache.rb', line 234

def remove_member(key)
  @members.delete(key&.to_id)
end

#remove_role(key) ⇒ Object



238
239
240
# File 'lib/mij-discord/cache.rb', line 238

def remove_role(key)
  @roles.delete(key&.to_id)
end

#resetObject



156
157
158
# File 'lib/mij-discord/cache.rb', line 156

def reset
  @channels, @members, @roles = {}, {}, {}
end