mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
* Fix guild being a guild and not an AbstractHandler in PresenceUpdate * update the default guild to be a Guild, and not GuildMember * getters return null instead of undefined * fix lint
256 lines
6.3 KiB
JavaScript
256 lines
6.3 KiB
JavaScript
const Util = require('../util/Util');
|
|
const { ActivityTypes, ActivityFlags } = require('../util/Constants');
|
|
|
|
/**
|
|
* Activity sent in a message.
|
|
* @typedef {Object} MessageActivity
|
|
* @property {string} [partyID] Id of the party represented in activity
|
|
* @property {number} [type] Type of activity sent
|
|
*/
|
|
|
|
/**
|
|
* Represents a user's presence.
|
|
*/
|
|
class Presence {
|
|
constructor(client, data = {}) {
|
|
Object.defineProperty(this, 'client', { value: client });
|
|
this.userID = data.user.id;
|
|
this.guild = data.guild;
|
|
this.patch(data);
|
|
}
|
|
|
|
/**
|
|
* The user of this presence
|
|
* @type {?User}
|
|
*/
|
|
get user() {
|
|
return this.client.users.get(this.userID) || null;
|
|
}
|
|
|
|
/**
|
|
* The member of this presence
|
|
* @type {?GuildMember}
|
|
*/
|
|
get member() {
|
|
return this.guild.members.get(this.userID) || null;
|
|
}
|
|
|
|
patch(data) {
|
|
/**
|
|
* The status of the presence:
|
|
*
|
|
* * **`online`** - user is online
|
|
* * **`offline`** - user is offline or invisible
|
|
* * **`idle`** - user is AFK
|
|
* * **`dnd`** - user is in Do Not Disturb
|
|
* @type {string}
|
|
*/
|
|
this.status = data.status || this.status || 'offline';
|
|
|
|
const activity = data.game || data.activity;
|
|
/**
|
|
* The activity of the presence
|
|
* @type {?Activity}
|
|
*/
|
|
this.activity = activity ? new Activity(this, activity) : null;
|
|
|
|
return this;
|
|
}
|
|
|
|
_clone() {
|
|
const clone = Object.assign(Object.create(this), this);
|
|
if (this.activity) clone.activity = this.activity._clone();
|
|
return clone;
|
|
}
|
|
|
|
/**
|
|
* Whether this presence is equal to another.
|
|
* @param {Presence} presence The presence to compare with
|
|
* @returns {boolean}
|
|
*/
|
|
equals(presence) {
|
|
return this === presence || (
|
|
presence &&
|
|
this.status === presence.status &&
|
|
this.activity ? this.activity.equals(presence.activity) : !presence.activity
|
|
);
|
|
}
|
|
|
|
toJSON() {
|
|
return Util.flatten(this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Represents an activity that is part of a user's presence.
|
|
*/
|
|
class Activity {
|
|
constructor(presence, data) {
|
|
Object.defineProperty(this, 'presence', { value: presence });
|
|
|
|
/**
|
|
* The name of the activity being played
|
|
* @type {string}
|
|
*/
|
|
this.name = data.name;
|
|
|
|
/**
|
|
* The type of the activity status
|
|
* @type {ActivityType}
|
|
*/
|
|
this.type = ActivityTypes[data.type];
|
|
|
|
/**
|
|
* If the activity is being streamed, a link to the stream
|
|
* @type {?string}
|
|
*/
|
|
this.url = data.url || null;
|
|
|
|
/**
|
|
* Details about the activity
|
|
* @type {?string}
|
|
*/
|
|
this.details = data.details || null;
|
|
|
|
/**
|
|
* State of the activity
|
|
* @type {?string}
|
|
*/
|
|
this.state = data.state || null;
|
|
|
|
/**
|
|
* Application ID associated with this activity
|
|
* @type {?Snowflake}
|
|
*/
|
|
this.applicationID = data.application_id || null;
|
|
|
|
/**
|
|
* Timestamps for the activity
|
|
* @type {?Object}
|
|
* @prop {?Date} start When the activity started
|
|
* @prop {?Date} end When the activity will end
|
|
*/
|
|
this.timestamps = data.timestamps ? {
|
|
start: data.timestamps.start ? new Date(Number(data.timestamps.start)) : null,
|
|
end: data.timestamps.end ? new Date(Number(data.timestamps.end)) : null,
|
|
} : null;
|
|
|
|
/**
|
|
* Party of the activity
|
|
* @type {?Object}
|
|
* @prop {?string} id ID of the party
|
|
* @prop {number[]} size Size of the party as `[current, max]`
|
|
*/
|
|
this.party = data.party || null;
|
|
|
|
/**
|
|
* Assets for rich presence
|
|
* @type {?RichPresenceAssets}
|
|
*/
|
|
this.assets = data.assets ? new RichPresenceAssets(this, data.assets) : null;
|
|
|
|
this.syncID = data.sync_id;
|
|
this._flags = data.flags;
|
|
}
|
|
|
|
get flags() {
|
|
const flags = [];
|
|
for (const [name, flag] of Object.entries(ActivityFlags)) {
|
|
if ((this._flags & flag) === flag) flags.push(name);
|
|
}
|
|
return flags;
|
|
}
|
|
|
|
/**
|
|
* Whether this activity is equal to another activity.
|
|
* @param {Activity} activity The activity to compare with
|
|
* @returns {boolean}
|
|
*/
|
|
equals(activity) {
|
|
return this === activity || (
|
|
activity &&
|
|
this.name === activity.name &&
|
|
this.type === activity.type &&
|
|
this.url === activity.url
|
|
);
|
|
}
|
|
|
|
/**
|
|
* When concatenated with a string, this automatically returns the activities' name instead of the Activity object.
|
|
* @returns {string}
|
|
*/
|
|
toString() {
|
|
return this.name;
|
|
}
|
|
|
|
_clone() {
|
|
return Object.assign(Object.create(this), this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assets for a rich presence
|
|
*/
|
|
class RichPresenceAssets {
|
|
constructor(activity, assets) {
|
|
Object.defineProperty(this, 'activity', { value: activity });
|
|
|
|
/**
|
|
* Hover text for the large image
|
|
* @type {?string}
|
|
*/
|
|
this.largeText = assets.large_text || null;
|
|
|
|
/**
|
|
* Hover text for the small image
|
|
* @type {?string}
|
|
*/
|
|
this.smallText = assets.small_text || null;
|
|
|
|
/**
|
|
* ID of the large image asset
|
|
* @type {?Snowflake}
|
|
*/
|
|
this.largeImage = assets.large_image || null;
|
|
|
|
/**
|
|
* ID of the small image asset
|
|
* @type {?Snowflake}
|
|
*/
|
|
this.smallImage = assets.small_image || null;
|
|
}
|
|
|
|
/**
|
|
* Gets the URL of the small image asset
|
|
* @param {Object} [options] Options for the image url
|
|
* @param {string} [options.format] Format of the image
|
|
* @param {number} [options.size] Size of the image
|
|
* @returns {?string} The small image URL
|
|
*/
|
|
smallImageURL({ format, size } = {}) {
|
|
if (!this.smallImage) return null;
|
|
return this.activity.presence.client.rest.cdn
|
|
.AppAsset(this.activity.applicationID, this.smallImage, { format, size });
|
|
}
|
|
|
|
/**
|
|
* Gets the URL of the large image asset
|
|
* @param {Object} [options] Options for the image url
|
|
* @param {string} [options.format] Format of the image
|
|
* @param {number} [options.size] Size of the image
|
|
* @returns {?string} The large image URL
|
|
*/
|
|
largeImageURL({ format, size } = {}) {
|
|
if (!this.largeImage) return null;
|
|
if (/^spotify:/.test(this.largeImage)) {
|
|
return `https://i.scdn.co/image/${this.largeImage.slice(8)}`;
|
|
}
|
|
return this.activity.presence.client.rest.cdn
|
|
.AppAsset(this.activity.applicationID, this.largeImage, { format, size });
|
|
}
|
|
}
|
|
|
|
exports.Presence = Presence;
|
|
exports.Activity = Activity;
|
|
exports.RichPresenceAssets = RichPresenceAssets;
|