diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index cb9f6827c..372e3d804 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -10,12 +10,6 @@ class Message alias_method :text, :content alias_method :to_s, :content - # @return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a - # {User} for old messages when the author has left the server since then) - attr_reader :author - alias_method :user, :author - alias_method :writer, :author - # @return [Channel] the channel in which this message was sent. attr_reader :channel @@ -92,34 +86,7 @@ def initialize(data, bot) @server = @channel.server - @author = if data['author'] - if data['author']['discriminator'] == ZERO_DISCRIM - # This is a webhook user! It would be pointless to try to resolve a member here, so we just create - # a User and return that instead. - Discordrb::LOGGER.debug("Webhook user: #{data['author']['id']}") - User.new(data['author'], @bot) - elsif @channel.private? - # Turn the message user into a recipient - we can't use the channel recipient - # directly because the bot may also send messages to the channel - Recipient.new(bot.user(data['author']['id'].to_i), @channel, bot) - else - member = @channel.server.member(data['author']['id'].to_i) - - if member - member.update_data(data['member']) if data['member'] - else - Discordrb::LOGGER.debug("Member with ID #{data['author']['id']} not cached (possibly left the server).") - member = if data['member'] - member_data = data['author'].merge(data['member']) - Member.new(member_data, @server, bot) - else - @bot.ensure_user(data['author']) - end - end - - member - end - end + @author_data = data['author'] @webhook_id = data['webhook_id'].to_i if data['webhook_id'] @@ -356,7 +323,7 @@ def delete_all_reactions # The inspect method is overwritten to give more useful output def inspect - "" + "" end # @return [String] a URL that a user can use to navigate to this message in the client @@ -408,5 +375,32 @@ def to_message end alias_method :message, :to_message + + # The user who sent this message. Will be a {Member} most of the time, a {User} in the case that the + # user has left the server, or a {Recipient} if the message was sent in a private channel. + # @return [Member, User, Recipient] + def author + author_id = @author_data['id'].to_i + return unless author_id + + if @author_data['discriminator'] == ZERO_DISCRIM + # This is a webhook user! It would be pointless to try to resolve a member here, so we just create + # a User and return that instead. + LOGGER.debug("Webhook user: #{author_id}") + User.new(data['author'], @bot) + elsif @channel.private? + # Turn the message user into a recipient - we can't use the channel recipient + # directly because the bot may also send messages to the channel + Recipient.new(@bot.ensure_user(@author_data), @channel, @bot) + else + author = @channel.server.member(author_id) || @bot.ensure_user(@author_data) + author.tap do + LOGGER.debug("Member with ID #{author_id} not available (possibly left the server).") if author.is_a? User + end + end + end + + alias_method :user, :author + alias_method :writer, :author end end