Skip to content

Commit

Permalink
global name handling bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Jun 17, 2023
1 parent 3b37546 commit 1d10789
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
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}"


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

0 comments on commit 1d10789

Please sign in to comment.