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, timestamp

Constructor Details

#initialize(data, bot, server) ⇒ TextChannel

Returns a new instance of TextChannel.



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

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.



221
222
223
# File 'lib/mij-discord/data/channel.rb', line 221

def last_message_id
  @last_message_id
end

#nsfwObject (readonly) Also known as: nsfw?

Returns the value of attribute nsfw.



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

def nsfw
  @nsfw
end

#owner_idObject (readonly)

Returns the value of attribute owner_id.



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

def owner_id
  @owner_id
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



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

def recipients
  @recipients
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Instance Method Details

#add_group_users(users) ⇒ Object



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

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



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

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



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

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



341
342
343
344
345
346
347
348
349
# File 'lib/mij-discord/data/channel.rb', line 341

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

#inspectObject



418
419
420
# File 'lib/mij-discord/data/channel.rb', line 418

def inspect
  MijDiscord.make_inspect(self, :id, :name, :type, :position, :topic, :nsfw)
end

#invitesObject



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

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

#last_messageObject



277
278
279
# File 'lib/mij-discord/data/channel.rb', line 277

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

#leave_groupObject



411
412
413
414
415
416
# File 'lib/mij-discord/data/channel.rb', line 411

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



361
362
363
364
365
# File 'lib/mij-discord/data/channel.rb', line 361

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

#make_webhook(reason = nil, name:, avatar: :empty, format: :png) ⇒ Object Also known as: webhook



369
370
371
372
373
# File 'lib/mij-discord/data/channel.rb', line 369

def make_webhook(reason = nil, name:, avatar: :empty, format: :png)
  avatar = User.process_avatar(avatar, format, true)
  response = MijDiscord::Core::API::Channel.create_webhook(@bot.auth, @id, name, avatar, reason)
  Webhook.new(JSON.parse(response), @bot)
end

#message(id) ⇒ Object



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

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

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



326
327
328
329
330
# File 'lib/mij-discord/data/channel.rb', line 326

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



273
274
275
# File 'lib/mij-discord/data/channel.rb', line 273

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

#pinned_messagesObject Also known as: pinned



334
335
336
337
# File 'lib/mij-discord/data/channel.rb', line 334

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

#recipientObject



269
270
271
# File 'lib/mij-discord/data/channel.rb', line 269

def recipient
  @recipients&.first
end

#remove_group_users(users) ⇒ Object



402
403
404
405
406
407
408
409
# File 'lib/mij-discord/data/channel.rb', line 402

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



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

def send_file(file, caption: '', tts: false)
  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



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/mij-discord/data/channel.rb', line 299

def send_message(text: '', embed: nil, tts: false)
  embed = case embed
    when nil then nil
    when Hash
      Embed.construct(embed)
    when Embed then embed
    else raise ArgumentError, 'Invalid embed'
  end&.to_hash

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

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



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

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=



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

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

  set_options(reason, topic: topic)
  nil
end

#start_typingObject



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

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

#update_data(data) ⇒ Object



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

def update_data(data)
  super(data)

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

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



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

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

#webhooksObject



356
357
358
359
# File 'lib/mij-discord/data/channel.rb', line 356

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