Skip to content

Commit

Permalink
Add avatar-url with experimental use of Subset::Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Altreus committed Jul 23, 2020
1 parent 8422914 commit 3759257
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion META6.json
Expand Up @@ -13,7 +13,8 @@
"Cro::WebSocket",
"Data::Dump",
"Object::Delayed",
"URI::Encode"
"URI::Encode",
"Subset::Helper"
],
"test-depends": [
"Test::META"
Expand Down
17 changes: 17 additions & 0 deletions lib/API/Discord/User.rakumod
@@ -1,6 +1,7 @@
use Object::Delayed;
use API::Discord::Object;
use API::Discord::Endpoints;
use Subset::Helper;

unit class API::Discord::User does API::Discord::Object is export;

Expand Down Expand Up @@ -60,6 +61,22 @@ submethod TWEAK() {
$!real-id //= $!id;
}

my %image-formats = JPEG => '.jpeg', PNG => '.png', WebP => '.webp', GIF => '.gif';
subset ReallyInt of Numeric where subset-is { $_.Int == $_ };
subset PowerOfTwo of Int where subset-is { log($_, 2) ~~ ReallyInt };
subset ImageFormat of Str where subset-is %image-formats{*}:exists;

method avatar-url(
PowerOfTwo :$desired-size where 16 <= * <= 4096,
ImageFormat :$format = 'PNG'
) {
my $url = $.api.cdn-url ~ '/avatars/' ~ $.real-id ~ '/' ~ $.avatar-hash ~ %image-formats{$format};
if $desired-size {
$url ~= '?size=' ~ $desired-size
}
return $url;
}

method guilds returns Promise {
if not $!guilds-promise {
$!guilds-promise = start {
Expand Down

0 comments on commit 3759257

Please sign in to comment.