From 1d1078930132627773ae1b1517b2c806f57f153e Mon Sep 17 00:00:00 2001 From: Faster Speeding Date: Sat, 17 Jun 2023 16:25:06 +0100 Subject: [PATCH 1/2] global name handling bug fixes --- changes/idk.bugfix.md | 1 + hikari/guilds.py | 2 +- hikari/users.py | 7 +++++-- tests/hikari/test_guilds.py | 4 ++++ tests/hikari/test_users.py | 15 +++++++++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 changes/idk.bugfix.md diff --git a/changes/idk.bugfix.md b/changes/idk.bugfix.md new file mode 100644 index 0000000000..9977766713 --- /dev/null +++ b/changes/idk.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>" From 8cf168c17be638eafa6dab73183402c31bc3de31 Mon Sep 17 00:00:00 2001 From: FasterSpeeding Date: Sat, 17 Jun 2023 16:36:25 +0100 Subject: [PATCH 2/2] Rename idk.bugfix.md to 1642.bugfix.md --- changes/{idk.bugfix.md => 1642.bugfix.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changes/{idk.bugfix.md => 1642.bugfix.md} (100%) diff --git a/changes/idk.bugfix.md b/changes/1642.bugfix.md similarity index 100% rename from changes/idk.bugfix.md rename to changes/1642.bugfix.md