-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: make Message#author lazy #204
Open
biqqles
wants to merge
5
commits into
shardlab:main
Choose a base branch
from
biqqles:lazy-author
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
"<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>" | ||
"<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{author} channel=#{@channel}>" | ||
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 | ||
swarley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
biqqles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Recipient.new(@bot.user(author_id), @channel, @bot) | ||
else | ||
author = @channel.server.member(author_id) || @bot.user(author_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, we should use ensure_member/ensure_user There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a very quick check,
data['member']
doesn't seem to exist anymore so I ignored the code relating to it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data['member']
is present when messages are received via a gateway event