Skip to content

Commit

Permalink
Add User avatar_decoration (#1019)
Browse files Browse the repository at this point in the history
* Add User::$avatar_decoration

* fix null return

---------

Co-authored-by: Alexander Maassen <[email protected]>
  • Loading branch information
SQKo and key2peace authored Jul 22, 2023
1 parent b08aa75 commit 960a852
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions src/Discord/Parts/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,27 @@
*
* @since 2.0.0
*
* @property string $id The unique identifier of the user.
* @property string $username The username of the user.
* @property string $discriminator The discriminator of the user.
* @property string|null $global_name The user's display name, if it is set. For bots, this is the application name.
* @property string $displayname The the display name of the client.
* @property ?string $avatar The avatar URL of the user.
* @property string|null $avatar_hash The avatar hash of the user.
* @property bool|null $bot Whether the user is a bot.
* @property bool|null $system Whether the user is a Discord system user.
* @property bool|null $mfa_enabled Whether MFA is enabled.
* @property ?string|null $banner The banner URL of the user.
* @property string|null $banner_hash The banner hash of the user.
* @property ?int|null $accent_color The user's banner color encoded as an integer representation of hexadecimal color code.
* @property string|null $locale User locale.
* @property bool|null $verified Whether the user is verified.
* @property ?string|null $email User email.
* @property int|null $flags User flags.
* @property int|null $premium_type Type of nitro subscription.
* @property int|null $public_flags Public flags on the user.
*
* @property string $id The unique identifier of the user.
* @property string $username The username of the user.
* @property string $discriminator The discriminator of the user.
* @property string|null $global_name The user's display name, if it is set. For bots, this is the application name.
* @property string $displayname The the display name of the client.
* @property ?string $avatar The avatar URL of the user.
* @property string|null $avatar_hash The avatar hash of the user.
* @property bool|null $bot Whether the user is a bot.
* @property bool|null $system Whether the user is a Discord system user.
* @property bool|null $mfa_enabled Whether MFA is enabled.
* @property ?string|null $banner The banner URL of the user.
* @property string|null $banner_hash The banner hash of the user.
* @property ?int|null $accent_color The user's banner color encoded as an integer representation of hexadecimal color code.
* @property string|null $locale User locale.
* @property bool|null $verified Whether the user is verified.
* @property ?string|null $email User email.
* @property int|null $flags User flags.
* @property int|null $premium_type Type of nitro subscription.
* @property int|null $public_flags Public flags on the user.
* @property int|null $avatar_decoration The user's avatar decoration URL.
* @property int|null $avatar_decoration_hash The user's avatar decoration hash. *
* @method ExtendedPromiseInterface<Message> sendMessage(MessageBuilder $builder)
*/
class User extends Part
Expand Down Expand Up @@ -82,6 +83,7 @@ class User extends Part
'discriminator',
'global_name',
'avatar',
'avatar_decoration',
'bot',
'system',
'mfa_enabled',
Expand Down Expand Up @@ -206,6 +208,45 @@ protected function getAvatarHashAttribute(): ?string
return $this->attributes['avatar'];
}

/**
* Returns the avatar decoration URL for the client.
*
* @param string|null $format The image format.
* @param int $size The size of the image.
*
* @return string|null The URL to the clients avatar decoration.
*/
public function getAvatarDecorationAttribute(?string $format = null, int $size = 288): ?string
{
if (! isset($this->attributes['avatar_decoration'])) {
return null;
}

if (isset($format)) {
$allowed = ['png', 'jpg', 'webp'];

if (! in_array(strtolower($format), $allowed)) {
$format = 'png';
}
} elseif (strpos($this->attributes['avatar_decoration'], 'a_') === 0) {
$format = 'png';
} else {
$format = 'png';
}

return "https://cdn.discordapp.com/avatar-decorations/{$this->id}/{$this->attributes['avatar_decoration']}.{$format}?size={$size}";
}

/**
* Returns the avatar decoration hash for the client.
*
* @return ?string The client avatar decoration's hash.
*/
protected function getAvatarDecorationHashAttribute(): ?string
{
return $this->attributes['avatar_decoration'];
}

/**
* Returns the banner URL for the client.
*
Expand Down

0 comments on commit 960a852

Please sign in to comment.