Class: MijDiscord::Data::Message
- Inherits:
-
Object
- Object
- MijDiscord::Data::Message
- Includes:
- IDObject
- Defined in:
- lib/mij-discord/data/message.rb
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#author ⇒ Object
(also: #user)
readonly
Returns the value of attribute author.
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#content ⇒ Object
(also: #text, #to_s)
readonly
Returns the value of attribute content.
-
#edited ⇒ Object
(also: #edited?)
readonly
Returns the value of attribute edited.
-
#edited_timestamp ⇒ Object
(also: #edit_timestamp)
readonly
Returns the value of attribute edited_timestamp.
-
#embeds ⇒ Object
readonly
Returns the value of attribute embeds.
-
#mention_everyone ⇒ Object
readonly
Returns the value of attribute mention_everyone.
-
#nonce ⇒ Object
readonly
Returns the value of attribute nonce.
-
#pinned ⇒ Object
(also: #pinned?)
readonly
Returns the value of attribute pinned.
-
#reactions ⇒ Object
readonly
Returns the value of attribute reactions.
-
#role_mentions ⇒ Object
readonly
Returns the value of attribute role_mentions.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#tts ⇒ Object
(also: #tts?)
readonly
Returns the value of attribute tts.
-
#user_mentions ⇒ Object
readonly
Returns the value of attribute user_mentions.
-
#webhook_id ⇒ Object
readonly
Returns the value of attribute webhook_id.
Attributes included from IDObject
Instance Method Summary collapse
- #clear_reactions ⇒ Object
- #create_reaction(reaction) ⇒ Object (also: #add_reaction)
- #delete ⇒ Object
- #delete_reaction(reaction, user: nil) ⇒ Object
- #edit(text: '', embed: nil) ⇒ Object
-
#initialize(data, bot) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
- #my_reactions ⇒ Object
- #pin ⇒ Object
- #reacted_with(reaction) ⇒ Object
- #reply(text: '', embed: nil, tts: false) ⇒ Object
- #reply_file(file, caption: '', tts: false) ⇒ Object
- #unpin ⇒ Object
- #update_data(data) ⇒ Object
- #update_reaction(add: nil, remove: nil, clear: false) ⇒ Object
- #webhook? ⇒ Boolean
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 ( = data['author']) if ['discriminator'] == '0000' @author = @bot.cache.put_user() elsif @channel.private? @author = @channel.recipient else member = @channel.server.member(['id']) @author = member || @bot.user(['id']) end end update_data(data) end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
29 30 31 |
# File 'lib/mij-discord/data/message.rb', line 29 def @attachments end |
#author ⇒ Object (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 end |
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
7 8 9 |
# File 'lib/mij-discord/data/message.rb', line 7 def bot @bot end |
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
16 17 18 |
# File 'lib/mij-discord/data/message.rb', line 16 def channel @channel end |
#content ⇒ Object (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 |
#edited ⇒ Object (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_timestamp ⇒ Object (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 end |
#embeds ⇒ Object (readonly)
Returns the value of attribute embeds.
31 32 33 |
# File 'lib/mij-discord/data/message.rb', line 31 def @embeds end |
#mention_everyone ⇒ Object (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 |
#nonce ⇒ Object (readonly)
Returns the value of attribute nonce.
38 39 40 |
# File 'lib/mij-discord/data/message.rb', line 38 def nonce @nonce end |
#pinned ⇒ Object (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 |
#reactions ⇒ Object (readonly)
Returns the value of attribute reactions.
33 34 35 |
# File 'lib/mij-discord/data/message.rb', line 33 def reactions @reactions end |
#role_mentions ⇒ Object (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 |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
18 19 20 |
# File 'lib/mij-discord/data/message.rb', line 18 def @timestamp end |
#tts ⇒ Object (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_mentions ⇒ Object (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_id ⇒ Object (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_reactions ⇒ Object
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 |
#delete ⇒ Object
212 213 214 215 |
# File 'lib/mij-discord/data/message.rb', line 212 def delete MijDiscord::Core::API::Channel.(@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 = case when nil then nil when Hash MijDiscord::Data::Embed.construct() when MijDiscord::Data::Embed then else raise ArgumentError, 'Invalid embed' end&.to_hash response = MijDiscord::Core::API::Channel.(@bot.auth, @channel.id, @id, text, [], ) @channel.cache.(JSON.parse(response), update: true) end |
#inspect ⇒ Object
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_reactions ⇒ Object
179 180 181 |
# File 'lib/mij-discord/data/message.rb', line 179 def my_reactions @reactions.select(&:me) end |
#pin ⇒ Object
165 166 167 168 |
# File 'lib/mij-discord/data/message.rb', line 165 def pin MijDiscord::Core::API::Channel.(@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.(text: text, 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: , tts: tts) end |
#unpin ⇒ Object
170 171 172 173 |
# File 'lib/mij-discord/data/message.rb', line 170 def unpin MijDiscord::Core::API::Channel.(@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 ( = data['attachments']) @attachments = .map {|x| Attachment.new(x, self) } end @attachments ||= [] if ( = data['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
175 176 177 |
# File 'lib/mij-discord/data/message.rb', line 175 def webhook? !@webhook_id.nil? end |