/
User.pm6
112 lines (82 loc) · 2.53 KB
/
User.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use API::Discord::Object;
use API::Discord::Endpoints;
unit class API::Discord::User does API::Discord::Object is export;
=begin pod
=head1 NAME
API::Discord::User - Represents Discord user
=head1 DESCRIPTION
Represents a Discord user, usually sent to use via the websocket. See
L<https://discordapp.com/developers/docs/resources/user>.
Users cannot be created or deleted.
See also L<API::Discord::Object>.
=head1 PROMISES
=head2 guilds
Resolves to a list of L<API::Discord::Guild> objects
=head2 dms
Resolves to a list of L<API::Discord::Channel> objects (direct messages)
=end pod
has Promise $!dms-promise;
has Promise $!guilds-promise;
#| Use real-id if you want to compare the user's numeric ID. This lets us put
#| '@me' in id itself, for endpoints
has $.real-id;
has $.id;
has $.username;
has $.discriminator;
has $.avatar; # The actual image
has $.avatar-hash; # The URL bit for the CDN
has $.is-bot;
has $.is-mfa-enabled;
has $.is-verified;
has $.email;
has $.locale;
submethod TWEAK() {
$!real-id //= $!id;
}
method guilds($force?) returns Promise {
if $force or not $!guilds-promise {
$!guilds-promise = start {
my @guilds;
my $e = endpoint-for( self, 'get-guilds' ) ;
my $p = await $.api.rest.get($e);
@guilds = (await $p.body).map( { $!api.inflate-guild($_) } );
@guilds;
}
}
$!guilds-promise
}
method dms($force?) returns Promise {
if $force or not $!dms-promise {
$!dms-promise = start {
my @dms;
my $e = endpoint-for( self, 'get-dms' ) ;
my $p = await $.api.rest.get($e);
@dms = (await $p.body).map: $!api.inflate-channel(*);
@dms;
}
}
$!guilds-promise
}
=begin pod
=head1 METHODS
=head2 create-dm
Creates a DM with the provided C<$user>. This is an L<API::Discord::Channel>
object; the method returns a Promise that resolves to this.
=end pod
method create-dm($user) returns Promise {
start {
my $body = { recipient_id => $user.id };
my $ret = await $.api.post: endpoint-for(self, 'create-dm'), body => $body;
my $dm = await $ret.body;
$.api.inflate-channel($dm);
}
}
# to-json might not be necessary
method to-json {}
method from-json ($json) {
my %constructor = $json<id username discriminator email locale real-id>:kv;
%constructor<avatar-hash is-bot is-mfa-enabled is-verified>
= $json<avatar bot mfa_enabled verified>;
%constructor<api> = $json<_api>;
return self.new(|%constructor);
}