class MijDiscord::Data::TextChannel

Attributes

last_message_id[R]
nsfw[R]
nsfw?[R]
owner_id[R]
recipients[R]
topic[R]

Public Class Methods

new(data, bot, server) click to toggle source
Calls superclass method MijDiscord::Data::Channel::new
# 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

Public Instance Methods

add_group_users(users) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
history(amount, before: nil, after: nil, around: nil)
Alias for: message_history
inspect() click to toggle source
# File lib/mij-discord/data/channel.rb, line 418
def inspect
  MijDiscord.make_inspect(self, :id, :name, :type, :position, :topic, :nsfw)
end
invite(reason = nil, max_age: 0, max_uses: 0, temporary: false, unique: false)
Alias for: make_invite
invites() click to toggle source
# 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_message() click to toggle source
# File lib/mij-discord/data/channel.rb, line 277
def last_message
  @last_message_id ? @cache.get_message(@last_message_id) : nil
end
leave_group() click to toggle source
# 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) click to toggle source
# 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
Also aliased as: invite
make_webhook(reason = nil, name:, avatar: :empty, format: :png) click to toggle source
# 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
Also aliased as: webhook
message(id) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: history
nsfw=(nsfw, reason = nil)
Alias for: set_nsfw
owner() click to toggle source
# File lib/mij-discord/data/channel.rb, line 273
def owner
  @owner_id ? @bot.cache.get_user(@owner_id) : nil
end
pinned()
Alias for: pinned_messages
pinned_messages() click to toggle source
# 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
Also aliased as: pinned
recipient() click to toggle source
# File lib/mij-discord/data/channel.rb, line 269
def recipient
  @recipients&.first
end
remove_group_users(users) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: nsfw=
set_topic(topic, reason = nil) click to toggle source
# 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
Also aliased as: topic=
start_typing() click to toggle source
# File lib/mij-discord/data/channel.rb, line 377
def start_typing
  MijDiscord::Core::API::Channel.start_typing(@bot.auth, @id)
  nil
end
topic=(topic, reason = nil)
Alias for: set_topic
update_data(data) click to toggle source
Calls superclass method MijDiscord::Data::Channel#update_data
# 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) click to toggle source
# 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
webhook(reason = nil, name:, avatar: :empty, format: :png)
Alias for: make_webhook
webhooks() click to toggle source
# 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