ClientUser.js (5454B)
1 'use strict'; 2 3 const DataResolver = require('../util/DataResolver'); 4 const Structures = require('../util/Structures'); 5 6 /** 7 * Represents the logged in client's Discord user. 8 * @extends {User} 9 */ 10 class ClientUser extends Structures.get('User') { 11 constructor(client, data) { 12 super(client, data); 13 this._typing = new Map(); 14 } 15 16 _patch(data) { 17 super._patch(data); 18 19 if ('verified' in data) { 20 /** 21 * Whether or not this account has been verified 22 * @type {boolean} 23 */ 24 this.verified = data.verified; 25 } 26 27 if ('mfa_enabled' in data) { 28 /** 29 * If the bot's {@link ClientApplication#owner Owner} has MFA enabled on their account 30 * @type {?boolean} 31 */ 32 this.mfaEnabled = typeof data.mfa_enabled === 'boolean' ? data.mfa_enabled : null; 33 } else if (typeof this.mfaEnabled === 'undefined') { 34 this.mfaEnabled = null; 35 } 36 37 if (data.token) this.client.token = data.token; 38 } 39 40 /** 41 * ClientUser's presence 42 * @type {Presence} 43 * @readonly 44 */ 45 get presence() { 46 return this.client.presence; 47 } 48 49 edit(data) { 50 return this.client.api 51 .users('@me') 52 .patch({ data }) 53 .then(newData => { 54 this.client.token = newData.token; 55 const { updated } = this.client.actions.UserUpdate.handle(newData); 56 if (updated) return updated; 57 return this; 58 }); 59 } 60 61 /** 62 * Sets the username of the logged in client. 63 * <info>Changing usernames in Discord is heavily rate limited, with only 2 requests 64 * every hour. Use this sparingly!</info> 65 * @param {string} username The new username 66 * @returns {Promise<ClientUser>} 67 * @example 68 * // Set username 69 * client.user.setUsername('discordjs') 70 * .then(user => console.log(`My new username is ${user.username}`)) 71 * .catch(console.error); 72 */ 73 setUsername(username) { 74 return this.edit({ username }); 75 } 76 77 /** 78 * Sets the avatar of the logged in client. 79 * @param {BufferResolvable|Base64Resolvable} avatar The new avatar 80 * @returns {Promise<ClientUser>} 81 * @example 82 * // Set avatar 83 * client.user.setAvatar('./avatar.png') 84 * .then(user => console.log(`New avatar set!`)) 85 * .catch(console.error); 86 */ 87 async setAvatar(avatar) { 88 return this.edit({ avatar: await DataResolver.resolveImage(avatar) }); 89 } 90 91 /** 92 * Data resembling a raw Discord presence. 93 * @typedef {Object} PresenceData 94 * @property {PresenceStatusData} [status] Status of the user 95 * @property {boolean} [afk] Whether the user is AFK 96 * @property {Object} [activity] Activity the user is playing 97 * @property {Object|string} [activity.application] An application object or application id 98 * @property {string} [activity.application.id] The id of the application 99 * @property {string} [activity.name] Name of the activity 100 * @property {ActivityType|number} [activity.type] Type of the activity 101 * @property {string} [activity.url] Stream url 102 * @property {?number|number[]} [shardID] Shard Id(s) to have the activity set on 103 */ 104 105 /** 106 * Sets the full presence of the client user. 107 * @param {PresenceData} data Data for the presence 108 * @returns {Promise<Presence>} 109 * @example 110 * // Set the client user's presence 111 * client.user.setPresence({ activity: { name: 'with discord.js' }, status: 'idle' }) 112 * .then(console.log) 113 * .catch(console.error); 114 */ 115 setPresence(data) { 116 return this.client.presence.set(data); 117 } 118 119 /** 120 * A user's status. Must be one of: 121 * * `online` 122 * * `idle` 123 * * `invisible` 124 * * `dnd` (do not disturb) 125 * @typedef {string} PresenceStatusData 126 */ 127 128 /** 129 * Sets the status of the client user. 130 * @param {PresenceStatusData} status Status to change to 131 * @param {?number|number[]} [shardID] Shard ID(s) to have the activity set on 132 * @returns {Promise<Presence>} 133 * @example 134 * // Set the client user's status 135 * client.user.setStatus('idle') 136 * .then(console.log) 137 * .catch(console.error); 138 */ 139 setStatus(status, shardID) { 140 return this.setPresence({ status, shardID }); 141 } 142 143 /** 144 * Options for setting an activity 145 * @typedef ActivityOptions 146 * @type {Object} 147 * @property {string} [url] Twitch stream URL 148 * @property {ActivityType|number} [type] Type of the activity 149 * @property {?number|number[]} [shardID] Shard Id(s) to have the activity set on 150 */ 151 152 /** 153 * Sets the activity the client user is playing. 154 * @param {string|ActivityOptions} [name] Activity being played, or options for setting the activity 155 * @param {ActivityOptions} [options] Options for setting the activity 156 * @returns {Promise<Presence>} 157 * @example 158 * // Set the client user's activity 159 * client.user.setActivity('discord.js', { type: 'WATCHING' }) 160 * .then(presence => console.log(`Activity set to ${presence.activities[0].name}`)) 161 * .catch(console.error); 162 */ 163 setActivity(name, options = {}) { 164 if (!name) return this.setPresence({ activity: null, shardID: options.shardID }); 165 166 const activity = Object.assign({}, options, typeof name === 'object' ? name : { name }); 167 return this.setPresence({ activity, shardID: activity.shardID }); 168 } 169 170 /** 171 * Sets/removes the AFK flag for the client user. 172 * @param {boolean} afk Whether or not the user is AFK 173 * @returns {Promise<Presence>} 174 */ 175 setAFK(afk) { 176 return this.setPresence({ afk }); 177 } 178 } 179 180 module.exports = ClientUser;