mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
199 lines
6.1 KiB
JavaScript
199 lines
6.1 KiB
JavaScript
const Structures = require('../util/Structures');
|
|
const DataResolver = require('../util/DataResolver');
|
|
|
|
/**
|
|
* Represents the logged in client's Discord user.
|
|
* @extends {User}
|
|
*/
|
|
class ClientUser extends Structures.get('User') {
|
|
_patch(data) {
|
|
super._patch(data);
|
|
|
|
/**
|
|
* Whether or not this account has been verified
|
|
* @type {boolean}
|
|
*/
|
|
this.verified = data.verified;
|
|
|
|
this._typing = new Map();
|
|
|
|
/**
|
|
* 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;
|
|
|
|
if (data.token) this.client.token = data.token;
|
|
}
|
|
|
|
/**
|
|
* ClientUser's presence
|
|
* @readonly
|
|
* @type {Presence}
|
|
*/
|
|
get presence() {
|
|
return this.client.presence;
|
|
}
|
|
|
|
edit(data) {
|
|
return this.client.api.users('@me').patch({ data })
|
|
.then(newData => {
|
|
this.client.token = newData.token;
|
|
return this.client.actions.UserUpdate.handle(newData).updated;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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 {PresenceStatus} [status] Status of the user
|
|
* @property {boolean} [afk] Whether the user is AFK
|
|
* @property {Object} [activity] Activity the user is playing
|
|
* @property {Object|string} [activity.application] An application object or application id
|
|
* @property {string} [activity.application.id] The id of the application
|
|
* @property {string} [activity.name] Name of the activity
|
|
* @property {ActivityType|number} [activity.type] Type of the activity
|
|
* @property {string} [activity.url] Stream url
|
|
*/
|
|
|
|
/**
|
|
* 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} PresenceStatus
|
|
*/
|
|
|
|
/**
|
|
* Sets the status of the client user.
|
|
* @param {PresenceStatus} status Status to change to
|
|
* @returns {Promise<Presence>}
|
|
* @example
|
|
* // Set the client user's status
|
|
* client.user.setStatus('idle')
|
|
* .then(console.log)
|
|
* .catch(console.error);
|
|
*/
|
|
setStatus(status) {
|
|
return this.setPresence({ status });
|
|
}
|
|
|
|
/**
|
|
* Options for setting an activity
|
|
* @typedef ActivityOptions
|
|
* @type {Object}
|
|
* @property {string} [url] Twitch stream URL
|
|
* @property {ActivityType|number} [type] Type of the activity
|
|
*/
|
|
|
|
/**
|
|
* 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.activity.name}`))
|
|
* .catch(console.error);
|
|
*/
|
|
setActivity(name, options = {}) {
|
|
if (!name) return this.setPresence({ activity: null });
|
|
|
|
const activity = Object.assign({}, options, typeof name === 'object' ? name : { name });
|
|
return this.setPresence({ activity });
|
|
}
|
|
|
|
/**
|
|
* 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 });
|
|
}
|
|
|
|
/**
|
|
* An object containing either a user or access token, and an optional nickname.
|
|
* @typedef {Object} GroupDMRecipientOptions
|
|
* @property {UserResolvable} [user] User to add to the Group DM
|
|
* @property {string} [accessToken] Access token to use to add a user to the Group DM
|
|
* (only available if a bot is creating the DM)
|
|
* @property {string} [nick] Permanent nickname (only available if a bot is creating the DM)
|
|
* @property {string} [id] If no user resolvable is provided and you want to assign nicknames
|
|
* you must provide user ids instead
|
|
*/
|
|
|
|
/**
|
|
* Creates a Group DM.
|
|
* @param {GroupDMRecipientOptions[]} recipients The recipients
|
|
* @returns {Promise<GroupDMChannel>}
|
|
* @example
|
|
* // Create a Group DM with a token provided from OAuth
|
|
* client.user.createGroupDM([{
|
|
* user: '66564597481480192',
|
|
* accessToken: token
|
|
* }])
|
|
* .then(console.log)
|
|
* .catch(console.error);
|
|
*/
|
|
createGroupDM(recipients) {
|
|
const data = this.bot ? {
|
|
access_tokens: recipients.map(u => u.accessToken),
|
|
nicks: recipients.reduce((o, r) => {
|
|
if (r.nick) o[r.user ? r.user.id : r.id] = r.nick;
|
|
return o;
|
|
}, {}),
|
|
} : { recipients: recipients.map(u => this.client.users.resolveID(u.user || u.id)) };
|
|
return this.client.api.users('@me').channels.post({ data })
|
|
.then(res => this.client.channels.add(res));
|
|
}
|
|
}
|
|
|
|
module.exports = ClientUser;
|