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

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
# File 'lib/mij-discord/data/message.rb', line 48

def initialize(data, bot)
  @bot = bot

  @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?

attr_reader :nonce



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

#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

#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?

attr_reader :reactions



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



164
165
166
167
# File 'lib/mij-discord/data/message.rb', line 164

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

def my_reactions

@reactions.select(&:me)

end



140
141
142
143
144
# File 'lib/mij-discord/data/message.rb', line 140

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



169
170
171
172
# File 'lib/mij-discord/data/message.rb', line 169

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

#delete_reaction(reaction, user: nil) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/mij-discord/data/message.rb', line 154

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



116
117
118
119
120
# File 'lib/mij-discord/data/message.rb', line 116

def edit(text: '', embed: nil)
  response = MijDiscord::Core::API::Channel.edit_message(@bot.auth, @channel.id, @id,
    text, [], embed&.to_h)
  @channel.cache.put_message(JSON.parse(response), update: true)
end

#pinObject



122
123
124
125
# File 'lib/mij-discord/data/message.rb', line 122

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

#reacted_with(reaction) ⇒ Object



148
149
150
151
152
# File 'lib/mij-discord/data/message.rb', line 148

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



108
109
110
# File 'lib/mij-discord/data/message.rb', line 108

def reply(text: '', embed: nil, tts: false)
  @channel.send_message(text: text, embed: embed, tts: tts)
end

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



112
113
114
# File 'lib/mij-discord/data/message.rb', line 112

def reply_file(file, caption: '', tts: false)
  @channel.send_file(file, caption: caption, tts: tts)
end

#unpinObject



127
128
129
130
# File 'lib/mij-discord/data/message.rb', line 127

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

#update_data(data) ⇒ Object



71
72
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
# File 'lib/mij-discord/data/message.rb', line 71

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']

  # @reactions = []
  # if (reactions = data['reactions'])
  #   reactions.each {|x| @reactions << Reaction.new(x) }
  # end

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

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

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

  @embeds = []
  if (embeds = data['embeds'])
    embeds.each {|x| @embeds << Embed.new(x) }
  end
end

#webhook?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mij-discord/data/message.rb', line 132

def webhook?
  !@webhook_id.nil?
end