2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Base = require('./Base');
|
|
|
|
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
2022-03-24 10:55:32 +00:00
|
|
|
const { Error } = require('../errors');
|
|
|
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
|
|
|
const UserFlags = require('../util/UserFlags');
|
|
|
|
const { default: Collection } = require('@discordjs/collection');
|
2022-03-25 15:57:21 +00:00
|
|
|
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
|
2022-04-12 05:46:08 +00:00
|
|
|
const { Relationship } = require('../util/Constants');
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a user on Discord.
|
|
|
|
* @implements {TextBasedChannel}
|
|
|
|
* @extends {Base}
|
|
|
|
*/
|
|
|
|
class User extends Base {
|
2022-04-03 04:53:09 +00:00
|
|
|
constructor(client, data) {
|
|
|
|
super(client);
|
2022-03-23 11:36:58 +00:00
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
/**
|
|
|
|
* The user's id
|
|
|
|
* @type {Snowflake}
|
|
|
|
*/
|
|
|
|
this.id = data.id;
|
2022-03-23 11:36:58 +00:00
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
this.bot = null;
|
2022-03-23 11:36:58 +00:00
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
this.system = null;
|
2022-03-23 11:36:58 +00:00
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
this.flags = null;
|
2022-03-23 11:36:58 +00:00
|
|
|
|
|
|
|
// Code written by https://github.com/aiko-chan-ai
|
2022-04-02 15:22:00 +00:00
|
|
|
this.connectedAccounts = [];
|
2022-03-23 11:36:58 +00:00
|
|
|
this.premiumSince = null;
|
|
|
|
this.premiumGuildSince = null;
|
|
|
|
this.mutualGuilds = new Collection();
|
2022-04-03 04:53:09 +00:00
|
|
|
this.applications = null;
|
|
|
|
this._patch(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
_patch(data) {
|
|
|
|
if ('username' in data) {
|
|
|
|
/**
|
|
|
|
* The username of the user
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.username = data.username;
|
|
|
|
} else {
|
|
|
|
this.username ??= null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('bot' in data) {
|
|
|
|
/**
|
|
|
|
* Whether or not the user is a bot
|
|
|
|
* @type {?boolean}
|
|
|
|
*/
|
|
|
|
this.bot = Boolean(data.bot);
|
|
|
|
if (this.bot == true) {
|
|
|
|
this.applications = new ApplicationCommandManager(
|
|
|
|
this.client,
|
|
|
|
undefined,
|
|
|
|
this,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (!this.partial && typeof this.bot !== 'boolean') {
|
|
|
|
this.bot = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('discriminator' in data) {
|
|
|
|
/**
|
|
|
|
* A discriminator based on username for the user
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.discriminator = data.discriminator;
|
|
|
|
} else {
|
|
|
|
this.discriminator ??= null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('avatar' in data) {
|
|
|
|
/**
|
|
|
|
* The user avatar's hash
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.avatar = data.avatar;
|
|
|
|
} else {
|
|
|
|
this.avatar ??= null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('banner' in data) {
|
|
|
|
/**
|
|
|
|
* The user banner's hash
|
|
|
|
* <info>The user must be force fetched for this property to be present or be updated</info>
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.banner = data.banner;
|
|
|
|
} else if (this.banner !== null) {
|
|
|
|
this.banner ??= undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('accent_color' in data) {
|
|
|
|
/**
|
|
|
|
* The base 10 accent color of the user's banner
|
|
|
|
* <info>The user must be force fetched for this property to be present or be updated</info>
|
|
|
|
* @type {?number}
|
|
|
|
*/
|
|
|
|
this.accentColor = data.accent_color;
|
|
|
|
} else if (this.accentColor !== null) {
|
|
|
|
this.accentColor ??= undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('system' in data) {
|
|
|
|
/**
|
|
|
|
* Whether the user is an Official Discord System user (part of the urgent message system)
|
|
|
|
* @type {?boolean}
|
|
|
|
*/
|
|
|
|
this.system = Boolean(data.system);
|
|
|
|
} else if (!this.partial && typeof this.system !== 'boolean') {
|
|
|
|
this.system = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('public_flags' in data) {
|
|
|
|
/**
|
|
|
|
* The flags for this user
|
|
|
|
* @type {?UserFlags}
|
|
|
|
*/
|
|
|
|
this.flags = new UserFlags(data.public_flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-12 05:46:08 +00:00
|
|
|
* Check relationship status
|
2022-04-03 04:53:09 +00:00
|
|
|
* @readonly
|
|
|
|
*/
|
2022-04-12 05:46:08 +00:00
|
|
|
get relationships() {
|
|
|
|
const i = this.client.relationships.cache.get(this.id) ?? 0;
|
|
|
|
return Relationship[parseInt(i)];
|
2022-04-03 04:53:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:32:11 +00:00
|
|
|
/**
|
|
|
|
* Check note
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get note() {
|
|
|
|
return this.client.user.notes.get(this.id);
|
|
|
|
}
|
|
|
|
|
2022-03-23 11:36:58 +00:00
|
|
|
// Code written by https://github.com/aiko-chan-ai
|
|
|
|
_ProfilePatch(data) {
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
if (data.connected_accounts.length > 0)
|
2022-04-02 15:22:00 +00:00
|
|
|
this.connectedAccounts = data.connected_accounts;
|
2022-03-23 11:36:58 +00:00
|
|
|
|
|
|
|
if ('premium_since' in data) {
|
|
|
|
const date = new Date(data.premium_since);
|
|
|
|
this.premiumSince = date.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('premium_guild_since' in data) {
|
|
|
|
const date = new Date(data.premium_guild_since);
|
|
|
|
this.premiumGuildSince = date.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mutualGuilds = new Collection(
|
|
|
|
data.mutual_guilds.map((obj) => [obj.id, obj]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get profile from Discord, if client is in a server with the target.
|
|
|
|
* <br>Code written by https://github.com/aiko-chan-ai
|
|
|
|
*/
|
|
|
|
async getProfile() {
|
|
|
|
if (this.client.bot) throw new Error('INVALID_BOT_METHOD');
|
|
|
|
try {
|
|
|
|
const data = await this.client.api.users(this.id).profile.get();
|
|
|
|
this._ProfilePatch(data);
|
|
|
|
return this;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Friends the user and send Request [If no request]
|
|
|
|
* @returns {Promise<User>} the user object
|
|
|
|
*/
|
|
|
|
async setFriend() {
|
2022-04-12 05:46:08 +00:00
|
|
|
return this.client.relationships.addFriend(this);
|
2022-03-23 11:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Friend Request to the user
|
|
|
|
* @returns {Promise<User>} the user object
|
|
|
|
*/
|
|
|
|
async sendFriendRequest() {
|
2022-04-14 14:32:11 +00:00
|
|
|
return this.client.relationships.sendFriendRequest(
|
|
|
|
this.username,
|
|
|
|
this.discriminator,
|
|
|
|
);
|
2022-03-23 11:36:58 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Blocks the user
|
|
|
|
* @returns {Promise<User>} the user object
|
|
|
|
*/
|
|
|
|
async setBlock() {
|
2022-04-12 05:46:08 +00:00
|
|
|
return this.client.relationships.addBlocked(this);
|
2022-03-23 11:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the user from your blocks list
|
|
|
|
* @returns {Promise<User>} the user object
|
|
|
|
*/
|
|
|
|
async unBlock() {
|
2022-04-12 05:46:08 +00:00
|
|
|
return this.client.relationships.deleteBlocked(this);
|
2022-03-23 11:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the user from your friends list
|
|
|
|
* @returns {Promise<User>} the user object
|
|
|
|
*/
|
2022-04-12 05:46:08 +00:00
|
|
|
unFriend() {
|
|
|
|
return this.client.relationships.deleteFriend(this);
|
2022-03-23 11:36:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
/**
|
|
|
|
* Whether this User is a partial
|
|
|
|
* @type {boolean}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get partial() {
|
|
|
|
return typeof this.username !== 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The timestamp the user was created at
|
|
|
|
* @type {number}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get createdTimestamp() {
|
|
|
|
return SnowflakeUtil.timestampFrom(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The time the user was created at
|
|
|
|
* @type {Date}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get createdAt() {
|
|
|
|
return new Date(this.createdTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A link to the user's avatar.
|
|
|
|
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
|
|
|
* @returns {?string}
|
|
|
|
*/
|
|
|
|
avatarURL({ format, size, dynamic } = {}) {
|
|
|
|
if (!this.avatar) return null;
|
|
|
|
return this.client.rest.cdn.Avatar(
|
|
|
|
this.id,
|
|
|
|
this.avatar,
|
|
|
|
format,
|
|
|
|
size,
|
|
|
|
dynamic,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A link to the user's default avatar
|
|
|
|
* @type {string}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get defaultAvatarURL() {
|
|
|
|
return this.client.rest.cdn.DefaultAvatar(this.discriminator % 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A link to the user's avatar if they have one.
|
|
|
|
* Otherwise a link to their default avatar will be returned.
|
|
|
|
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
displayAvatarURL(options) {
|
|
|
|
return this.avatarURL(options) ?? this.defaultAvatarURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The hexadecimal version of the user accent color, with a leading hash
|
|
|
|
* <info>The user must be force fetched for this property to be present</info>
|
|
|
|
* @type {?string}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get hexAccentColor() {
|
|
|
|
if (typeof this.accentColor !== 'number') return this.accentColor;
|
|
|
|
return `#${this.accentColor.toString(16).padStart(6, '0')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A link to the user's banner.
|
|
|
|
* <info>This method will throw an error if called before the user is force fetched.
|
|
|
|
* See {@link User#banner} for more info</info>
|
|
|
|
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
|
|
|
* @returns {?string}
|
|
|
|
*/
|
|
|
|
bannerURL({ format, size, dynamic } = {}) {
|
|
|
|
if (typeof this.banner === 'undefined')
|
|
|
|
throw new Error('USER_BANNER_NOT_FETCHED');
|
|
|
|
if (!this.banner) return null;
|
|
|
|
return this.client.rest.cdn.Banner(
|
|
|
|
this.id,
|
|
|
|
this.banner,
|
|
|
|
format,
|
|
|
|
size,
|
|
|
|
dynamic,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Discord "tag" (e.g. `hydrabolt#0001`) for this user
|
|
|
|
* @type {?string}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get tag() {
|
|
|
|
return typeof this.username === 'string'
|
|
|
|
? `${this.username}#${this.discriminator}`
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DM between the client's user and this user
|
|
|
|
* @type {?DMChannel}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get dmChannel() {
|
|
|
|
return this.client.users.dmChannel(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a DM channel between the client and the user.
|
|
|
|
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
|
|
|
* @returns {Promise<DMChannel>}
|
|
|
|
*/
|
|
|
|
createDM(force = false) {
|
|
|
|
return this.client.users.createDM(this.id, force);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
|
|
|
|
* @returns {Promise<DMChannel>}
|
|
|
|
*/
|
|
|
|
deleteDM() {
|
|
|
|
return this.client.users.deleteDM(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the user is equal to another.
|
|
|
|
* It compares id, username, discriminator, avatar, banner, accent color, and bot flags.
|
|
|
|
* It is recommended to compare equality by using `user.id === user2.id` unless you want to compare all properties.
|
|
|
|
* @param {User} user User to compare with
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
equals(user) {
|
|
|
|
return (
|
|
|
|
user &&
|
|
|
|
this.id === user.id &&
|
|
|
|
this.username === user.username &&
|
|
|
|
this.discriminator === user.discriminator &&
|
|
|
|
this.avatar === user.avatar &&
|
|
|
|
this.flags?.bitfield === user.flags?.bitfield &&
|
|
|
|
this.banner === user.banner &&
|
|
|
|
this.accentColor === user.accentColor
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the user with an API user object
|
|
|
|
* @param {APIUser} user The API user object to compare
|
|
|
|
* @returns {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_equals(user) {
|
|
|
|
return (
|
|
|
|
user &&
|
|
|
|
this.id === user.id &&
|
|
|
|
this.username === user.username &&
|
|
|
|
this.discriminator === user.discriminator &&
|
|
|
|
this.avatar === user.avatar &&
|
|
|
|
this.flags?.bitfield === user.public_flags &&
|
|
|
|
('banner' in user ? this.banner === user.banner : true) &&
|
|
|
|
('accent_color' in user ? this.accentColor === user.accent_color : true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches this user's flags.
|
|
|
|
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
|
|
|
* @returns {Promise<UserFlags>}
|
|
|
|
*/
|
|
|
|
fetchFlags(force = false) {
|
|
|
|
return this.client.users.fetchFlags(this.id, { force });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches this user.
|
|
|
|
* @param {boolean} [force=true] Whether to skip the cache check and request the API
|
|
|
|
* @returns {Promise<User>}
|
|
|
|
*/
|
|
|
|
fetch(force = true) {
|
|
|
|
return this.client.users.fetch(this.id, { force });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When concatenated with a string, this automatically returns the user's mention instead of the User object.
|
|
|
|
* @returns {string}
|
|
|
|
* @example
|
|
|
|
* // Logs: Hello from <@123456789012345678>!
|
|
|
|
* console.log(`Hello from ${user}!`);
|
|
|
|
*/
|
|
|
|
toString() {
|
|
|
|
return `<@${this.id}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON(...props) {
|
|
|
|
const json = super.toJSON(
|
|
|
|
{
|
|
|
|
createdTimestamp: true,
|
|
|
|
defaultAvatarURL: true,
|
|
|
|
hexAccentColor: true,
|
|
|
|
tag: true,
|
|
|
|
},
|
|
|
|
...props,
|
|
|
|
);
|
|
|
|
json.avatarURL = this.avatarURL();
|
|
|
|
json.displayAvatarURL = this.displayAvatarURL();
|
|
|
|
json.bannerURL = this.banner ? this.bannerURL() : this.banner;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set note to user
|
|
|
|
* @param {String<User.note>} note Note to set
|
|
|
|
* @returns {Promise<User.note>}
|
|
|
|
*/
|
|
|
|
async setNote(note = null) {
|
|
|
|
await this.client.api.users['@me'].notes(id).put({ data: { note } });
|
|
|
|
return (this.note = note);
|
|
|
|
}
|
2022-03-23 11:36:58 +00:00
|
|
|
|
2022-04-03 04:53:09 +00:00
|
|
|
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
|
|
|
/* eslint-disable no-empty-function */
|
|
|
|
send() {}
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextBasedChannel.applyToClass(User);
|
|
|
|
|
|
|
|
module.exports = User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @external APIUser
|
|
|
|
* @see {@link https://discord.com/developers/docs/resources/user#user-object}
|
|
|
|
*/
|