ClientUser.js
5.19 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
'use strict';
const DataResolver = require('../util/DataResolver');
const Structures = require('../util/Structures');
/**
* Represents the logged in client's Discord user.
* @extends {User}
*/
class ClientUser extends Structures.get('User') {
constructor(client, data) {
super(client, data);
this._typing = new Map();
}
_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 if (typeof this.mfaEnabled === 'undefined') {
this.mfaEnabled = null;
}
if (data.token) this.client.token = data.token;
}
/**
* ClientUser's presence
* @type {Presence}
* @readonly
*/
get presence() {
return this.client.presence;
}
edit(data) {
return this.client.api
.users('@me')
.patch({ data })
.then(newData => {
this.client.token = newData.token;
const { updated } = this.client.actions.UserUpdate.handle(newData);
if (updated) return updated;
return 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);
*/
async setAvatar(avatar) {
return this.edit({ avatar: await DataResolver.resolveImage(avatar) });
}
/**
* 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 {Object} [activity] Activity the user is playing
* @property {string} [activity.name] Name of the activity
* @property {ActivityType|number} [activity.type] Type of the activity
* @property {string} [activity.url] Twitch / YouTube stream URL
* @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 {Promise<Presence>}
* @example
* // Set the client user's presence
* client.user.setPresence({ activity: { name: 'with discord.js' }, status: 'idle' })
* .then(console.log)
* .catch(console.error);
*/
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 {Promise<Presence>}
* @example
* // Set the client user's status
* client.user.setStatus('idle')
* .then(console.log)
* .catch(console.error);
*/
setStatus(status, shardID) {
return this.setPresence({ status, shardID });
}
/**
* Options for setting an activity.
* @typedef ActivityOptions
* @type {Object}
* @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 {Promise<Presence>}
* @example
* // Set the client user's activity
* client.user.setActivity('discord.js', { type: 'WATCHING' })
* .then(presence => console.log(`Activity set to ${presence.activities[0].name}`))
* .catch(console.error);
*/
setActivity(name, options = {}) {
if (!name) return this.setPresence({ activity: null, shardID: options.shardID });
const activity = Object.assign({}, options, typeof name === 'object' ? name : { name });
return this.setPresence({ activity, shardID: activity.shardID });
}
/**
* Sets/removes the AFK flag for the client user.
* @param {boolean} afk Whether or not the user is AFK
* @returns {Promise<Presence>}
*/
setAFK(afk) {
return this.setPresence({ afk });
}
}
module.exports = ClientUser;