forked from abalabahaha/eris
/
User.js
156 lines (138 loc) · 5.06 KB
/
User.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"use strict";
const Base = require("./Base");
const {CDN_URL} = require("../rest/Endpoints");
const {ImageSizeBoundaries, ImageFormats} = require("../Constants");
/**
* Represents a user
* @prop {String} id The ID of the user
* @prop {Number} createdAt Timestamp of the user's creation
* @prop {String} mention A string that mentions the user
* @prop {String} defaultAvatar The hash for the default avatar of a user if there is no avatar set
* @prop {Number} createdAt Timestamp of user creation
* @prop {Boolean} bot Whether the user is an OAuth bot or not
* @prop {String} username The username of the user
* @prop {String} discriminator The discriminator of the user
* @prop {String?} avatar The hash of the user's avatar, or null if no avatar
* @prop {String} defaultAvatarURL The URL of the user's default avatar
* @prop {String} avatarURL The URL of the user's avatar which can be either a JPG or GIF
* @prop {String} staticAvatarURL The URL of the user's avatar (always a JPG)
* @prop {Boolean} system Whether the user is an official Discord system user (e.g. urgent messages)
*/
class User extends Base {
constructor(data, client) {
super(data.id);
if(!client) {
this._missingClientError = new Error("Missing client in constructor"); // Preserve constructor callstack
}
this._client = client;
this.bot = !!data.bot;
this.system = !!data.system;
this.update(data);
}
update(data) {
if(data.avatar !== undefined) {
this.avatar = data.avatar;
}
if(data.username !== undefined) {
this.username = data.username;
}
if(data.discriminator !== undefined) {
this.discriminator = data.discriminator;
}
}
get mention() {
return `<@${this.id}>`;
}
get defaultAvatar() {
return this.discriminator % 5;
}
get defaultAvatarURL() {
return `${CDN_URL}/embed/avatars/${this.defaultAvatar}.png`;
}
get staticAvatarURL() {
if(this._missingClientError) {
throw this._missingClientError;
}
return this.avatar ? `${CDN_URL}/avatars/${this.id}/${this.avatar}.${this._client.options.defaultImageFormat}?size=${this._client.options.defaultImageSize}` : this.defaultAvatarURL;
}
get avatarURL() {
if(this._missingClientError) {
throw this._missingClientError;
}
return this.avatar ? `${CDN_URL}/avatars/${this.id}/${this.avatar}.${this.avatar.startsWith("a_") ? "gif" : this._client.options.defaultImageFormat}?size=${this._client.options.defaultImageSize}` : this.defaultAvatarURL;
}
/**
* Get the user's avatar with the given format and size
* @arg {String} [format] The filetype of the avatar ("jpg", "png", "gif", or "webp")
* @arg {Number} [size] The size of the avatar (any power of two between 16 and 2048)
*/
dynamicAvatarURL(format, size) {
if(!this.avatar) {
return this.defaultAvatarURL;
}
if(!format || !ImageFormats.includes(format.toLowerCase())) {
format = this.avatar.startsWith("a_") ? "gif" : this._client.options.defaultImageFormat;
}
if(
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this._client.options.defaultImageSize;
}
return `${CDN_URL}/avatars/${this.id}/${this.avatar}.${format}?size=${size}`;
}
/**
* Get a DM channel with the user, or create one if it does not exist
* @returns {Promise<PrivateChannel>}
*/
getDMChannel() {
return this._client.getDMChannel.call(this._client, this.id);
}
/**
* Create a relationship with the user (user accounts only)
* @arg {Boolean} [block=false] If true, block the user. Otherwise, add the user as a friend
* @returns {Promise}
*/
addRelationship(block) {
return this._client.addRelationship.call(this._client, this.id, block);
}
/**
* Remove a relationship with the user (user accounts only)
* @returns {Promise}
*/
removeRelationship() {
return this._client.removeRelationship.call(this._client, this.id);
}
/**
* Get profile data for the user (user accounts only)
* @returns {Promise<Object>} The user's profile data.
*/
getProfile() {
return this._client.getUserProfile.call(this._client, this.id);
}
/**
* Edit the current user's note for the user (user accounts only)
* @arg {String} note The note
* @returns {Promise}
*/
editNote(note) {
return this._client.editUserNote.call(this._client, this.id, note);
}
/**
* Delete the current user's note for another user (user accounts only)
*/
deleteNote() {
return this._client.deleteUserNote.call(this._client, this.id);
}
toJSON(props = []) {
return super.toJSON([
"avatar",
"bot",
"discriminator",
"username",
...props
]);
}
}
module.exports = User;