Skip to content
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

global name handling bug fixes #1642

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/idk.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`Member.__str__` will now return `Member.nickname` if set. `PartialUserImpl.__str__` will now return `PartialUserImpl.global_name` if set.
2 changes: 1 addition & 1 deletion hikari/guilds.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ async def edit(
)

def __str__(self) -> str:
return str(self.user)
return self.nickname or str(self.user)

def __hash__(self) -> int:
return hash(self.user)
Expand Down
7 changes: 5 additions & 2 deletions hikari/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,11 @@ def mention(self) -> str:
def __str__(self) -> str:
if self.username is undefined.UNDEFINED or self.discriminator is undefined.UNDEFINED:
return f"Partial user ID {self.id}"
elif self.discriminator == "0": # migrated account
return self.username

# migrated account
if self.discriminator == "0":
return self.global_name or self.username

return f"{self.username}#{self.discriminator}"
Comment on lines +719 to 722
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.discriminator == "0":
return self.global_name or self.username
return f"{self.username}#{self.discriminator}"
if self.discriminator == "0":
return self.global_name or self.username
return self.global_name or f"{self.username}#{self.discriminator}"



Expand Down
4 changes: 4 additions & 0 deletions tests/hikari/test_guilds.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,12 @@ def model(self, mock_user):
)

def test_str_operator(self, model, mock_user):
model.nickname = None
assert str(model) == str(mock_user)

def test_str_operator_when_nickname_set(self, model, mock_user):
assert str(model) == "davb"

def test_app_property(self, model, mock_user):
assert model.app is mock_user.app

Expand Down
15 changes: 15 additions & 0 deletions tests/hikari/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,25 @@ def obj(self):
def test_str_operator(self, obj):
assert str(obj) == "thomm.o#8637"

def test_str_operator_when_migrated(self, obj: users.PartialUserImpl):
obj.discriminator = "0"
assert str(obj) == "thomm.o"

def test_str_operator_when_migrated_and_global_name_set(self, obj: users.PartialUserImpl):
obj.discriminator = "0"
obj.global_name = "hello"
assert str(obj) == "hello"

def test_str_operator_when_partial(self, obj):
obj.username = undefined.UNDEFINED
assert str(obj) == "Partial user ID 123"

def test_str_operator_when_migrated_and_partial(self, obj):
obj.username = undefined.UNDEFINED
obj.discriminator = "0"
obj.global_name = "0"
assert str(obj) == "Partial user ID 123"

def test_mention_property(self, obj):
assert obj.mention == "<@123>"

Expand Down