diff --git a/changes/1642.bugfix.md b/changes/1642.bugfix.md new file mode 100644 index 0000000000..9977766713 --- /dev/null +++ b/changes/1642.bugfix.md @@ -0,0 +1 @@ +`Member.__str__` will now return `Member.nickname` if set. `PartialUserImpl.__str__` will now return `PartialUserImpl.global_name` if set. diff --git a/hikari/guilds.py b/hikari/guilds.py index 599cab81d3..2673890260 100644 --- a/hikari/guilds.py +++ b/hikari/guilds.py @@ -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) diff --git a/hikari/users.py b/hikari/users.py index a993f30f53..5fccb16fe0 100644 --- a/hikari/users.py +++ b/hikari/users.py @@ -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}" diff --git a/tests/hikari/test_guilds.py b/tests/hikari/test_guilds.py index 1c07054b0c..67de729b84 100644 --- a/tests/hikari/test_guilds.py +++ b/tests/hikari/test_guilds.py @@ -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 diff --git a/tests/hikari/test_users.py b/tests/hikari/test_users.py index 681ca54c53..6e9f0b3fe8 100644 --- a/tests/hikari/test_users.py +++ b/tests/hikari/test_users.py @@ -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>"