Class: MijDiscord::Data::Message

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/mij-discord/data/message.rb

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, #hash, synthesize, timestamp

Constructor Details

#initialize(data, bot) ⇒ Message

Returns a new instance of Message.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



29
30
31
# File 'lib/mij-discord/data/message.rb', line 29

def attachments
  @attachments
end

#authorObject (readonly) Also known as: user

Returns the value of attribute author.



13
14
15
# File 'lib/mij-discord/data/message.rb', line 13

def author
  @author
end

#botObject (readonly)

Returns the value of attribute bot.



7
8
9
# File 'lib/mij-discord/data/message.rb', line 7

def bot
  @bot
end

#channelObject (readonly)

Returns the value of attribute channel.



16
17
18
# File 'lib/mij-discord/data/message.rb', line 16

def channel
  @channel
end

#contentObject (readonly) Also known as: text, to_s

Returns the value of attribute content.



9
10
11
# File 'lib/mij-discord/data/message.rb', line 9

def content
  @content
end

#editedObject (readonly) Also known as: edited?

Returns the value of attribute edited.



40
41
42
# File 'lib/mij-discord/data/message.rb', line 40

def edited
  @edited
end

#edited_timestampObject (readonly) Also known as: edit_timestamp

Returns the value of attribute edited_timestamp.



20
21
22
# File 'lib/mij-discord/data/message.rb', line 20

def edited_timestamp
  @edited_timestamp
end

#embedsObject (readonly)

Returns the value of attribute embeds.



31
32
33
# File 'lib/mij-discord/data/message.rb', line 31

def embeds
  @embeds
end

#mention_everyoneObject (readonly)

Returns the value of attribute mention_everyone.



27
28
29
# File 'lib/mij-discord/data/message.rb', line 27

def mention_everyone
  @mention_everyone
end

#nonceObject (readonly)

Returns the value of attribute nonce.



38
39
40
# File 'lib/mij-discord/data/message.rb', line 38

def nonce
  @nonce
end

#pinnedObject (readonly) Also known as: pinned?

Returns the value of attribute pinned.



43
44
45
# File 'lib/mij-discord/data/message.rb', line 43

def pinned
  @pinned
end

#reactionsObject (readonly)

Returns the value of attribute reactions.



33
34
35
# File 'lib/mij-discord/data/message.rb', line 33

def reactions
  @reactions
end

#role_mentionsObject (readonly)

Returns the value of attribute role_mentions.



25
26
27
# File 'lib/mij-discord/data/message.rb', line 25

def role_mentions
  @role_mentions
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



18
19
20
# File 'lib/mij-discord/data/message.rb', line 18

def timestamp
  @timestamp
end

#ttsObject (readonly) Also known as: tts?

Returns the value of attribute tts.



35
36
37
# File 'lib/mij-discord/data/message.rb', line 35

def tts
  @tts
end

#user_mentionsObject (readonly)

Returns the value of attribute user_mentions.



23
24
25
# File 'lib/mij-discord/data/message.rb', line 23

def user_mentions
  @user_mentions
end

#webhook_idObject (readonly)

Returns the value of attribute webhook_id.



46
47
48
# File 'lib/mij-discord/data/message.rb', line 46

def webhook_id
  @webhook_id
end

Instance Method Details

#clear_reactionsObject



207
208
209
210
# 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) ⇒ Object Also known as: add_reaction



183
184
185
186
187
# 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

#deleteObject



212
213
214
215
# 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) ⇒ Object



197
198
199
200
201
202
203
204
205
# 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) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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

#inspectObject



217
218
219
220
221
# 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_reactionsObject



179
180
181
# File 'lib/mij-discord/data/message.rb', line 179

def my_reactions
  @reactions.select(&:me)
end

#pinObject



165
166
167
168
# 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) ⇒ Object



191
192
193
194
195
# 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) ⇒ Object



142
143
144
# 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) ⇒ Object



146
147
148
# 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

#unpinObject



170
171
172
173
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/mij-discord/data/message.rb', line 175

def webhook?
  !@webhook_id.nil?
end