Repository: hikari-py/hikari
Pull request #1673 (2023-07-31 14:49:27)

Fix incorrect calculation for the default avatar of migrated users


Patch:
diff --git a/changes/1673.bugfix.md b/changes/1673.bugfix.md
new file mode 100644
index 00000000..e0d8fdcc
--- /dev/null
+++ b/changes/1673.bugfix.md
@@ -0,0 +1,1 @@
+Fix incorrect calculation for the default avatar of migrated users
diff --git a/dev-requirements/flake8.txt b/dev-requirements/flake8.txt
index 7fca0735..d9a8845d 100644
--- a/dev-requirements/flake8.txt
+++ b/dev-requirements/flake8.txt
@@ -1,4 +1,4 @@
-flake8==6.0.0
+flake8==6.1.0
 
 # Plugins
 # Ref: https://github.com/DmytroLitvinov/awesome-flake8-extensions
diff --git a/hikari/internal/routes.py b/hikari/internal/routes.py
index 75b9a634..0db58bbf 100644
--- a/hikari/internal/routes.py
+++ b/hikari/internal/routes.py
@@ -571,7 +571,7 @@ def compile_to_file(
 )
 CDN_GUILD_BANNER: typing.Final[CDNRoute] = CDNRoute("/banners/{guild_id}/{hash}", {PNG, *JPEG_JPG, WEBP, GIF})
 
-CDN_DEFAULT_USER_AVATAR: typing.Final[CDNRoute] = CDNRoute("/embed/avatars/{discriminator}", {PNG}, is_sizable=False)
+CDN_DEFAULT_USER_AVATAR: typing.Final[CDNRoute] = CDNRoute("/embed/avatars/{style}", {PNG}, is_sizable=False)
 CDN_USER_AVATAR: typing.Final[CDNRoute] = CDNRoute("/avatars/{user_id}/{hash}", {PNG, *JPEG_JPG, WEBP, GIF})
 CDN_USER_BANNER: typing.Final[CDNRoute] = CDNRoute("/banners/{user_id}/{hash}", {PNG, *JPEG_JPG, WEBP, GIF})
 CDN_MEMBER_AVATAR: typing.Final[CDNRoute] = CDNRoute(
diff --git a/hikari/users.py b/hikari/users.py
index a993f30f..b58e1562 100644
--- a/hikari/users.py
+++ b/hikari/users.py
@@ -497,11 +497,11 @@ def default_avatar_url(self) -> files.URL:
         """Default avatar URL for this user."""
         if self.discriminator == "0":  # migrated account
             return routes.CDN_DEFAULT_USER_AVATAR.compile_to_file(
-                urls.CDN_URL, discriminator=self.id.created_at.timestamp() % 6, file_format="png"
+                urls.CDN_URL, style=(self.id >> 22) % 6, file_format="png"
             )
 
         return routes.CDN_DEFAULT_USER_AVATAR.compile_to_file(
-            urls.CDN_URL, discriminator=int(self.discriminator) % 5, file_format="png"
+            urls.CDN_URL, style=int(self.discriminator) % 5, file_format="png"
         )
 
     @property
diff --git a/hikari/webhooks.py b/hikari/webhooks.py
index 1080a4a5..261e0191 100644
--- a/hikari/webhooks.py
+++ b/hikari/webhooks.py
@@ -526,7 +526,7 @@ def avatar_url(self) -> typing.Optional[files_.URL]:
     @property
     def default_avatar_url(self) -> files_.URL:
         """Default avatar URL for the user."""
-        return routes.CDN_DEFAULT_USER_AVATAR.compile_to_file(urls.CDN_URL, discriminator=0, file_format="png")
+        return routes.CDN_DEFAULT_USER_AVATAR.compile_to_file(urls.CDN_URL, style=0, file_format="png")
 
     def make_avatar_url(self, ext: str = "png", size: int = 4096) -> typing.Optional[files_.URL]:
         """Generate the avatar URL for this webhook's custom avatar if set.


Test patch:
diff --git a/tests/hikari/internal/test_enums.py b/tests/hikari/internal/test_enums.py
index 46150cb5..05433791 100644
--- a/tests/hikari/internal/test_enums.py
+++ b/tests/hikari/internal/test_enums.py
@@ -202,7 +202,7 @@ class Enum(int, enums.Enum):
 
         returned = Enum(9)
         assert returned == Enum.foo
-        assert type(returned) == Enum
+        assert type(returned) is Enum
 
     def test_call_when_not_member(self):
         class Enum(int, enums.Enum):
@@ -212,7 +212,7 @@ class Enum(int, enums.Enum):
 
         returned = Enum(69)
         assert returned == 69
-        assert type(returned) != Enum
+        assert type(returned) is not Enum
 
     def test_getitem(self):
         class Enum(int, enums.Enum):
@@ -222,7 +222,7 @@ class Enum(int, enums.Enum):
 
         returned = Enum["foo"]
         assert returned == Enum.foo
-        assert type(returned) == Enum
+        assert type(returned) is Enum
 
     def test_contains(self):
         class Enum(int, enums.Enum):
@@ -1252,7 +1252,7 @@ class TestFlag(enums.Flag):
 
         returned = TestFlag["FOO"]
         assert returned == TestFlag.FOO
-        assert type(returned) == TestFlag
+        assert type(returned) is TestFlag
 
     def test_repr(self):
         class TestFlag(enums.Flag):
diff --git a/tests/hikari/test_users.py b/tests/hikari/test_users.py
index 681ca54c..3b5b2a22 100644
--- a/tests/hikari/test_users.py
+++ b/tests/hikari/test_users.py
@@ -254,7 +254,19 @@ def test_default_avatar(self, obj):
         ) as route:
             assert obj.default_avatar_url == "file"
 
-        route.compile_to_file.assert_called_once_with(urls.CDN_URL, discriminator=4, file_format="png")
+        route.compile_to_file.assert_called_once_with(urls.CDN_URL, style=4, file_format="png")
+
+    def test_default_avatar_for_migrated_users(self, obj):
+        obj.id = 377812572784820226
+        obj.avatar_hash = "18dnf8dfbakfdh"
+        obj.discriminator = "0"
+
+        with mock.patch.object(
+            routes, "CDN_DEFAULT_USER_AVATAR", new=mock.Mock(compile_to_file=mock.Mock(return_value="file"))
+        ) as route:
+            assert obj.default_avatar_url == "file"
+
+        route.compile_to_file.assert_called_once_with(urls.CDN_URL, style=0, file_format="png")
 
     def test_banner_url_property(self, obj):
         with mock.patch.object(users.User, "make_banner_url") as make_banner_url: