ClientUser.js
5.4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const User = require('./User');
const DataResolver = require('../util/DataResolver');
/**
* Represents the logged in client's Discord user.
* @extends {User}
*/
class ClientUser extends User {
_patch(data) {
super._patch(data);
if ('verified' in data) {
/**
* Whether or not this account has been verified
* @type {boolean}
*/
this.verified = data.verified;
}
if ('mfa_enabled' in data) {
/**
* If the bot's {@link ClientApplication#owner Owner} has MFA enabled on their account
* @type {?boolean}
*/
this.mfaEnabled = typeof data.mfa_enabled === 'boolean' ? data.mfa_enabled : null;
} else {
this.mfaEnabled ??= null;
}
if ('token' in data) this.client.token = data.token;
}
/**
* Represents the client user's presence
* @type {ClientPresence}
* @readonly
*/
get presence() {
return this.client.presence;
}
/**
* Data used to edit the logged in client
* @typedef {Object} ClientUserEditData
* @property {string} [username] The new username
* @property {?(BufferResolvable|Base64Resolvable)} [avatar] The new avatar
*/
/**
* Edits the logged in client.
* @param {ClientUserEditData} data The new data
* @returns {Promise<ClientUser>}
*/
async edit(data) {
if (typeof data.avatar !== 'undefined') data.avatar = await DataResolver.resolveImage(data.avatar);
const newData = await this.client.api.users('@me').patch({ data });
this.client.token = newData.token;
const { updated } = this.client.actions.UserUpdate.handle(newData);
return updated ?? this;
}
/**
* Sets the username of the logged in client.
* <info>Changing usernames in Discord is heavily rate limited, with only 2 requests
* every hour. Use this sparingly!</info>
* @param {string} username The new username
* @returns {Promise<ClientUser>}
* @example
* // Set username
* client.user.setUsername('discordjs')
* .then(user => console.log(`My new username is ${user.username}`))
* .catch(console.error);
*/
setUsername(username) {
return this.edit({ username });
}
/**
* Sets the avatar of the logged in client.
* @param {?(BufferResolvable|Base64Resolvable)} avatar The new avatar
* @returns {Promise<ClientUser>}
* @example
* // Set avatar
* client.user.setAvatar('./avatar.png')
* .then(user => console.log(`New avatar set!`))
* .catch(console.error);
*/
setAvatar(avatar) {
return this.edit({ avatar });
}
/**
* Options for setting activities
* @typedef {Object} ActivitiesOptions
* @property {string} [name] Name of the activity
* @property {ActivityType|number} [type] Type of the activity
* @property {string} [url] Twitch / YouTube stream URL
*/
/**
* Data resembling a raw Discord presence.
* @typedef {Object} PresenceData
* @property {PresenceStatusData} [status] Status of the user
* @property {boolean} [afk] Whether the user is AFK
* @property {ActivitiesOptions[]} [activities] Activity the user is playing
* @property {number|number[]} [shardId] Shard id(s) to have the activity set on
*/
/**
* Sets the full presence of the client user.
* @param {PresenceData} data Data for the presence
* @returns {ClientPresence}
* @example
* // Set the client user's presence
* client.user.setPresence({ activities: [{ name: 'with discord.js' }], status: 'idle' });
*/
setPresence(data) {
return this.client.presence.set(data);
}
/**
* A user's status. Must be one of:
* * `online`
* * `idle`
* * `invisible`
* * `dnd` (do not disturb)
* @typedef {string} PresenceStatusData
*/
/**
* Sets the status of the client user.
* @param {PresenceStatusData} status Status to change to
* @param {number|number[]} [shardId] Shard id(s) to have the activity set on
* @returns {ClientPresence}
* @example
* // Set the client user's status
* client.user.setStatus('idle');
*/
setStatus(status, shardId) {
return this.setPresence({ status, shardId });
}
/**
* Options for setting an activity.
* @typedef {Object} ActivityOptions
* @property {string} [name] Name of the activity
* @property {string} [url] Twitch / YouTube stream URL
* @property {ActivityType|number} [type] Type of the activity
* @property {number|number[]} [shardId] Shard Id(s) to have the activity set on
*/
/**
* Sets the activity the client user is playing.
* @param {string|ActivityOptions} [name] Activity being played, or options for setting the activity
* @param {ActivityOptions} [options] Options for setting the activity
* @returns {ClientPresence}
* @example
* // Set the client user's activity
* client.user.setActivity('discord.js', { type: 'WATCHING' });
*/
setActivity(name, options = {}) {
if (!name) return this.setPresence({ activities: [], shardId: options.shardId });
const activity = Object.assign({}, options, typeof name === 'object' ? name : { name });
return this.setPresence({ activities: [activity], shardId: activity.shardId });
}
/**
* Sets/removes the AFK flag for the client user.
* @param {boolean} [afk=true] Whether or not the user is AFK
* @param {number|number[]} [shardId] Shard Id(s) to have the AFK flag set on
* @returns {ClientPresence}
*/
setAFK(afk = true, shardId) {
return this.setPresence({ afk, shardId });
}
}
module.exports = ClientUser;