Skip to content

feat: further username system updates #1044

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

Merged
merged 10 commits into from
Jun 12, 2023
2 changes: 1 addition & 1 deletion changelog/1025.feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Add support for new username system - see the official `help article <https://di
- :meth:`Guild.get_member_named`
- |commands| :class:`~ext.commands.UserConverter` and :class:`~ext.commands.MemberConverter`, now largely matching the behavior of :meth:`Guild.get_member_named`
- Update ``str(user)`` and ``str(member)`` to not include ``#0`` discriminator of migrated users.
- Adjust :attr:`User.default_avatar` to account for new default avatar handling.
- Adjust :attr:`User.default_avatar` to account for new default avatar handling, also adding :attr:`DefaultAvatar.fuchsia`.
7 changes: 7 additions & 0 deletions changelog/1044.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Add support for new username system - see the official `help article <https://dis.gd/app-usernames>`__ for details. Existing functionality is kept backwards-compatible while the migration is still ongoing.
- Add :attr:`User.global_name`, and update attributes/methods to account for it:
- :attr:`User.display_name` and :attr:`Member.display_name`
- :meth:`Guild.get_member_named`
- |commands| :class:`~ext.commands.UserConverter` and :class:`~ext.commands.MemberConverter`, now largely matching the behavior of :meth:`Guild.get_member_named`
- Update ``str(user)`` and ``str(member)`` to not include ``#0`` discriminator of migrated users.
- Adjust :attr:`User.default_avatar` to account for new default avatar handling, also adding :attr:`DefaultAvatar.fuchsia`.
2 changes: 0 additions & 2 deletions disnake/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ class User(Snowflake, Protocol):
The user's global display name, if set.
This takes precedence over :attr:`.name` when shown.

For bots, this is the application name.

.. versionadded:: 2.9

avatar: :class:`~disnake.Asset`
Expand Down
1 change: 1 addition & 0 deletions disnake/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class DefaultAvatar(Enum):
green = 2
orange = 3
red = 4
fuchsia = 5

def __str__(self) -> str:
return self.name
Expand Down
3 changes: 1 addition & 2 deletions disnake/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,7 @@ def get_member_named(self, name: str, /) -> Optional[Member]:
2. Lookup by global name.
3. Lookup by username.

While the migration away from discriminators is still ongoing,
the name can have an optional discriminator argument, e.g. "Jake#0001",
The name can have an optional discriminator argument, e.g. "Jake#0001",
in which case it will be treated as a username + discriminator combo
(note: this only works with usernames, not nicknames).

Expand Down
2 changes: 1 addition & 1 deletion disnake/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def tag(self) -> str:

@property
def discriminator(self) -> str:
""":class:`str`:The user's discriminator.
""":class:`str`: The user's discriminator.

.. note::
This is being phased out by Discord; the username system is moving away from ``username#discriminator``
Expand Down
18 changes: 4 additions & 14 deletions disnake/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .asset import Asset
from .colour import Colour
from .enums import DefaultAvatar, Locale, try_enum
from .enums import Locale, try_enum
from .flags import PublicUserFlags
from .utils import MISSING, _assetbytes_to_base64_data, snowflake_time

Expand Down Expand Up @@ -157,11 +157,11 @@ def default_avatar(self) -> Asset:
Added handling for users migrated to the new username system without discriminators.
"""
if self.discriminator == "0":
num = self.id >> 22
index = (self.id >> 22) % 6
else:
# legacy behavior
num = int(self.discriminator)
return Asset._from_default_avatar(self._state, num % len(DefaultAvatar))
index = int(self.discriminator) % 5
return Asset._from_default_avatar(self._state, index)

@property
def display_avatar(self) -> Asset:
Expand Down Expand Up @@ -308,18 +308,10 @@ class ClientUser(BaseUser):
discriminator: :class:`str`
The user's discriminator.

.. note::
This is being phased out by Discord; the username system is moving away from ``username#discriminator``
to users having a globally unique username.
The value of a single zero (``"0"``) indicates that the user has been migrated to the new system.
See the `help article <https://dis.gd/app-usernames>`__ for details.

global_name: Optional[:class:`str`]
The user's global display name, if set.
This takes precedence over :attr:`.name` when shown.

For bots, this is the application name.

.. versionadded:: 2.9

bot: :class:`bool`
Expand Down Expand Up @@ -464,8 +456,6 @@ class User(BaseUser, disnake.abc.Messageable):
The user's global display name, if set.
This takes precedence over :attr:`.name` when shown.

For bots, this is the application name.

.. versionadded:: 2.9

bot: :class:`bool`
Expand Down
8 changes: 7 additions & 1 deletion docs/api/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ DefaultAvatar

.. class:: DefaultAvatar

Represents the default avatar of a Discord :class:`User`
Represents the default avatar of a Discord :class:`User`.

.. attribute:: blurple

Expand All @@ -152,6 +152,12 @@ DefaultAvatar

Represents the default avatar with the color red.
See also :attr:`Colour.red`
.. attribute:: fuchsia

Represents the default avatar with the color fuchsia.
See also :attr:`Colour.fuchsia`

.. versionadded:: 2.9

Events
------
Expand Down