class MijDiscord::Data::Message

Attributes

attachments[R]
author[R]
bot[R]
channel[R]
content[R]
edit_timestamp[R]
edited[R]
edited?[R]
edited_timestamp[R]
embeds[R]
mention_everyone[R]
nonce[R]
pinned[R]
pinned?[R]
reactions[R]
role_mentions[R]
text[R]
timestamp[R]
to_s[R]
tts[R]
tts?[R]
user[R]
user_mentions[R]
webhook_id[R]

Public Class Methods

new(data, bot) click to toggle source
# File lib/mij-discord/data/message.rb, line 48
def initialize(data, bot)
  @bot = bot

  data = data.first if data.is_a?(Array)

  @id = data['id'].to_i
  @channel = @bot.channel(data['channel_id'])

  @nonce = data['nonce']
  @webhook_id = data['webhook_id']&.to_i

  if (author = data['author'])
    if author['discriminator'] == '0000'
      @author = @bot.cache.put_user(author)
    elsif @channel.private?
      @author = @channel.recipient
    else
      member = @channel.server.member(author['id'])
      @author = member || @bot.user(author['id'])
    end
  end

  update_data(data)
end

Public Instance Methods

add_reaction(reaction)
Alias for: create_reaction
clear_reactions() click to toggle source
# File lib/mij-discord/data/message.rb, line 207
def clear_reactions
  MijDiscord::Core::API::Channel.delete_all_reactions(@bot.auth, @channel.id, @id)
  nil
end
create_reaction(reaction) click to toggle source
# File lib/mij-discord/data/message.rb, line 183
def create_reaction(reaction)
  emoji = reaction.respond_to?(:reaction) ? reaction.reaction : reaction
  MijDiscord::Core::API::Channel.create_reaction(@bot.auth, @channel.id, @id, emoji)
  nil
end
Also aliased as: add_reaction
delete() click to toggle source
# File lib/mij-discord/data/message.rb, line 212
def delete
  MijDiscord::Core::API::Channel.delete_message(@bot.auth, @channel.id, @id)
  nil
end
delete_reaction(reaction, user: nil) click to toggle source
# File lib/mij-discord/data/message.rb, line 197
def delete_reaction(reaction, user: nil)
  emoji = reaction.respond_to?(:reaction) ? reaction.reaction : reaction
  if user.nil?
    MijDiscord::Core::API::Channel.delete_own_reaction(@bot.auth, @channel.id, @id, emoji)
  else
    MijDiscord::Core::API::Channel.delete_user_reaction(@bot.auth, @channel.id, @id, emoji, user.to_id)
  end
  nil
end
edit(text: '', embed: nil) click to toggle source
# File lib/mij-discord/data/message.rb, line 150
def edit(text: '', embed: nil)
  raise MijDiscord::Errors::MessageTooLong if text.length > 2000

  embed = case embed
    when nil then nil
    when Hash
      MijDiscord::Data::Embed.construct(embed)
    when MijDiscord::Data::Embed then embed
    else raise ArgumentError, 'Invalid embed'
  end&.to_hash

  response = MijDiscord::Core::API::Channel.edit_message(@bot.auth, @channel.id, @id, text, [], embed)
  @channel.cache.put_message(JSON.parse(response), update: true)
end
inspect() click to toggle source
# File lib/mij-discord/data/message.rb, line 217
def inspect
  MijDiscord.make_inspect(self,
    :id, :content, :author, :channel, :timestamp, :edited, :pinned, :edited_timestamp,
    :user_mentions, :role_mentions, :attachments, :embeds, :tts, :webhook_id)
end
my_reactions() click to toggle source
# File lib/mij-discord/data/message.rb, line 179
def my_reactions
  @reactions.select(&:me)
end
pin() click to toggle source
# File lib/mij-discord/data/message.rb, line 165
def pin
  MijDiscord::Core::API::Channel.pin_message(@bot.auth, @channel.id, @id)
  nil
end
reacted_with(reaction) click to toggle source
# File lib/mij-discord/data/message.rb, line 191
def reacted_with(reaction)
  emoji = reaction.respond_to?(:reaction) ? reaction.reaction : reaction
  response = MijDiscord::Core::API::Channel.get_reactions(@bot.auth, @channel.id, @id, emoji)
  JSON.parse(response).map {|x| @bot.cache.put_user(x) }
end
reply(text: '', embed: nil, tts: false) click to toggle source
# File lib/mij-discord/data/message.rb, line 142
def reply(text: '', embed: nil, tts: false)
  @channel.send_message(text: text, embed: embed, tts: tts)
end
reply_file(file, caption: '', tts: false) click to toggle source
# File lib/mij-discord/data/message.rb, line 146
def reply_file(file, caption: '', tts: false)
  @channel.send_file(file, caption: caption, tts: tts)
end
unpin() click to toggle source
# File lib/mij-discord/data/message.rb, line 170
def unpin
  MijDiscord::Core::API::Channel.unpin_message(@bot.auth, @channel.id, @id)
  nil
end
update_data(data) click to toggle source
# File lib/mij-discord/data/message.rb, line 73
def update_data(data)
  @content = data.fetch('content', @content)
  @pinned = data.fetch('pinned', @pinned)
  @tts = data.fetch('tts', @tts)

  @timestamp = Time.parse(data['timestamp']).utc if data['timestamp']
  @edited_timestamp = Time.parse(data['edited_timestamp']).utc if data['edited_timestamp']
  @edited = !!@edited_timestamp

  @mention_everyone = !!data['mention_everyone']

  if (reactions = data['reactions'])
    @reactions = reactions.map {|x| Reaction.new(x, self) }
  end
  @reactions ||= []

  if (mentions = data['mentions'])
    @user_mentions = mentions.map {|x| @bot.cache.put_user(x) }
  end
  @user_mentions ||= []

  if @channel.text? && (mentions = data['mention_roles'])
    @role_mentions = mentions.map {|x| @channel.server.role(x) }
  end
  @role_mentions ||= []

  if (attachments = data['attachments'])
    @attachments = attachments.map {|x| Attachment.new(x, self) }
  end
  @attachments ||= []

  if (embeds = data['embeds'])
    @embeds = embeds.map {|x| Embed.new(x) }
  end
  @embeds ||= []
end
update_reaction(add: nil, remove: nil, clear: false) click to toggle source
# File lib/mij-discord/data/message.rb, line 110
def update_reaction(add: nil, remove: nil, clear: false)
  @reactions.clear if clear

  unless add.nil?
    id = add['emoji']['id'].to_i
    name = add['emoji']['name']
    userid = add['user_id'].to_i

    if (emoji = @reactions.find {|x| id.zero? ? (x.name == name) : (x.id == id) })
      emoji.update_data(count: emoji.count + 1, me: @bot.profile == userid ? true : nil)
    else
      emoji = Reaction.new(add, self)
      emoji.update_data(me: @bot.profile == userid)
      @reactions << emoji
    end
  end

  unless remove.nil?
    id = remove['emoji']['id'].to_i
    name = remove['emoji']['name']
    userid = remove['user_id'].to_i

    if (emoji = @reactions.find {|x| id.zero? ? (x.name == name) : (x.id == id) })
      emoji.update_data(count: emoji.count - 1, me: @bot.profile == userid ? false : nil)
      @reactions.delete(emoji) if emoji.count < 1
    else
      # WTF? How did this happen?
      MijDiscord::LOGGER.warn('Events') { 'MESSAGE_REACTION_REMOVE triggered on message with no reactions!' }
    end
  end
end
webhook?() click to toggle source
# File lib/mij-discord/data/message.rb, line 175
def webhook?
  !@webhook_id.nil?
end