Class: MijDiscord::Data::TextChannel

Inherits:
Channel
  • Object
show all
Defined in:
lib/mij-discord/data/channel.rb

Constant Summary

Constants inherited from Channel

Channel::TYPES

Instance Attribute Summary collapse

Attributes inherited from Channel

#bot, #cache, #name, #parent_id, #permission_overwrites, #position, #server, #type

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods inherited from Channel

#category?, create, #default_channel?, #define_overwrite, #delete, #delete_overwrite, #group?, #member_overwrites, #mention, #parent, #pm?, #private?, #role_overwrites, #set_name, #set_options, #set_overwrites, #set_parent, #set_position, #sync_overwrites, #text?, #voice?

Methods included from IDObject

#==, #creation_time, #hash, synthesize

Constructor Details

#initialize(data, bot, server) ⇒ TextChannel

Returns a new instance of TextChannel.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/mij-discord/data/channel.rb', line 221

def initialize(data, bot, server)
  super(data, bot, server)

  @last_message_id = data['last_message_id']

  if private?
    @recipients = []
    if data['recipients']
      data['recipients'].each do |rd|
        user = @bot.cache.put_user(rd)
        @recipients << Recipient.new(user, self, @bot)
      end
    end

    if pm?
      @name = @recipients.first.username
    else
      @owner_id = data['owner_id'].to_i
    end
  end
end

Instance Attribute Details

#last_message_idObject (readonly)

Returns the value of attribute last_message_id.



219
220
221
# File 'lib/mij-discord/data/channel.rb', line 219

def last_message_id
  @last_message_id
end

#nsfwObject (readonly) Also known as: nsfw?

Returns the value of attribute nsfw.



214
215
216
# File 'lib/mij-discord/data/channel.rb', line 214

def nsfw
  @nsfw
end

#owner_idObject (readonly)

Returns the value of attribute owner_id.



212
213
214
# File 'lib/mij-discord/data/channel.rb', line 212

def owner_id
  @owner_id
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



217
218
219
# File 'lib/mij-discord/data/channel.rb', line 217

def recipients
  @recipients
end

#topicObject (readonly)

Returns the value of attribute topic.



210
211
212
# File 'lib/mij-discord/data/channel.rb', line 210

def topic
  @topic
end

Instance Method Details

#add_group_users(users) ⇒ Object



375
376
377
378
379
380
381
382
# File 'lib/mij-discord/data/channel.rb', line 375

def add_group_users(users)
  raise 'Attempted to add a user to a non-group channel' unless group?

  users.each do |u|
    MijDiscord::Core::API::Channel.add_group_user(@bot.auth, @id, u.to_id)
  end
  nil
end

#create_group(users) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'lib/mij-discord/data/channel.rb', line 364

def create_group(users)
  raise 'Attempted to create group channel on a non-pm channel' unless pm?

  ids = users.map(&:to_id)
  response = MijDiscord::Core::API::Channel.create_group(@bot.auth, @id, ids.shift)
  channel = @bot.cache.put_channel(JSON.parse(response))
  channel.add_group_users(ids)

  channel
end

#delete_message(message) ⇒ Object



312
313
314
315
# File 'lib/mij-discord/data/channel.rb', line 312

def delete_message(message)
  MijDiscord::Core::API::Channel.delete_message(@bot.auth, @id, message.to_id)
  @cache.remove_message(message)
end

#delete_messages(messages) ⇒ Object



336
337
338
339
340
341
342
343
344
# File 'lib/mij-discord/data/channel.rb', line 336

def delete_messages(messages)
  two_weeks = Time.now - (14 * 86_400)
  min_snowflake = IDObject.synthesize(two_weeks)
  ids = messages.map(&:to_id).delete_if {|m| m < min_snowflake }

  MijDiscord::Core::API::Channel.bulk_delete_messages(@bot.auth, @id, ids)
  ids.each {|m| @cache.remove_message(m) }
  nil
end

#invitesObject



346
347
348
349
# File 'lib/mij-discord/data/channel.rb', line 346

def invites
  response = MijDiscord::Core::API::Channel.invites(@bot.auth, @id)
  JSON.parse(response).map {|x| Invite.new(x, @bot) }
end

#last_messageObject



275
276
277
# File 'lib/mij-discord/data/channel.rb', line 275

def last_message
  @last_message_id ? @cache.get_message(@last_message_id) : nil
end

#leave_groupObject



393
394
395
396
397
398
# File 'lib/mij-discord/data/channel.rb', line 393

def leave_group
  raise 'Attempoted to leave a non-group channel' unless group?

  MijDiscord::Core::API::Channel.leave_group(@bot.auth, @id)
  nil
end

#make_invite(reason = nil, max_age: 0, max_uses: 0, temporary: false, unique: false) ⇒ Object Also known as: invite



351
352
353
354
355
# File 'lib/mij-discord/data/channel.rb', line 351

def make_invite(reason = nil, max_age: 0, max_uses: 0, temporary: false, unique: false)
  response = MijDiscord::Core::API::Channel.create_invite(@bot.auth, @id,
    max_age, max_uses, temporary, unique, reason)
  Invite.new(JSON.parse(response), @bot)
end

#message(id) ⇒ Object



317
318
319
# File 'lib/mij-discord/data/channel.rb', line 317

def message(id)
  @cache.get_message(id)
end

#message_history(amount, before: nil, after: nil, around: nil) ⇒ Object Also known as: history



321
322
323
324
325
# File 'lib/mij-discord/data/channel.rb', line 321

def message_history(amount, before: nil, after: nil, around: nil)
  response = MijDiscord::Core::API::Channel.messages(@bot.auth, @id,
    amount, before&.to_id, after&.to_id, around&.to_id)
  JSON.parse(response).map {|m| Message.new(m, @bot) }
end

#ownerObject



271
272
273
# File 'lib/mij-discord/data/channel.rb', line 271

def owner
  @owner_id ? @bot.cache.get_user(@owner_id) : nil
end

#pinned_messagesObject Also known as: pinned



329
330
331
332
# File 'lib/mij-discord/data/channel.rb', line 329

def pinned_messages
  response = MijDiscord::Core::API::Channel.pinned_messages(@bot.auth, @id)
  JSON.parse(response).map {|m| Message.new(m, @bot) }
end

#recipientObject



267
268
269
# File 'lib/mij-discord/data/channel.rb', line 267

def recipient
  @recipients&.first
end

#remove_group_users(users) ⇒ Object



384
385
386
387
388
389
390
391
# File 'lib/mij-discord/data/channel.rb', line 384

def remove_group_users(users)
  raise 'Attempted to remove a user to a non-group channel' unless group?

  users.each do |u|
    MijDiscord::Core::API::Channel.remove_group_user(@bot.auth, @id, u.to_id)
  end
  nil
end

#send_file(file, caption: '', tts: false) ⇒ Object



305
306
307
308
309
310
# File 'lib/mij-discord/data/channel.rb', line 305

def send_file(file, caption: '', tts: false)
  raise MijDiscord::Core::Errors::MessageTooLong if caption.length > 2000

  response = MijDiscord::Core::API::Channel.upload_file(@bot.auth, @id, file, caption, tts)
  @cache.put_message(JSON.parse(response))
end

#send_message(text: '', embed: nil, tts: false) ⇒ Object



297
298
299
300
301
302
303
# File 'lib/mij-discord/data/channel.rb', line 297

def send_message(text: '', embed: nil, tts: false)
  raise MijDiscord::Core::Errors::MessageTooLong if text.length > 2000

  response = MijDiscord::Core::API::Channel.create_message(@bot.auth, @id,
    text, tts, embed&.to_h)
  @cache.put_message(JSON.parse(response))
end

#set_nsfw(nsfw, reason = nil) ⇒ Object Also known as: nsfw=



288
289
290
291
292
293
# File 'lib/mij-discord/data/channel.rb', line 288

def set_nsfw(nsfw, reason = nil)
  return unless text?

  set_options(reason, nsfw: nsfw)
  nil
end

#set_topic(topic, reason = nil) ⇒ Object Also known as: topic=



279
280
281
282
283
284
# File 'lib/mij-discord/data/channel.rb', line 279

def set_topic(topic, reason = nil)
  return unless text?

  set_options(reason, topic: topic)
  nil
end

#start_typingObject



359
360
361
362
# File 'lib/mij-discord/data/channel.rb', line 359

def start_typing
  MijDiscord::Core::API::Channel.start_typing(@bot.auth, @id)
  nil
end

#update_data(data) ⇒ Object



243
244
245
246
247
248
# File 'lib/mij-discord/data/channel.rb', line 243

def update_data(data)
  super(data)

  @topic = data.fetch('topic', @topic)
  @nsfw = data.fetch('nsfw', @nsfw)
end

#update_recipient(add: nil, remove: nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mij-discord/data/channel.rb', line 250

def update_recipient(add: nil, remove: nil)
  return unless group?

  unless add.nil?
    user = @bot.cache.put_user(add)
    recipient = Recipient.new(user, self, @bot)
    @recipients << recipient
    return recipient
  end

  unless remove.nil?
    id = remove['id'].to_i
    recipient = @recipients.find {|x| x.id == id }
    return @recipients.delete(recipient)
  end
end