feat: new RPC

This commit is contained in:
Elysia 2024-01-31 20:54:42 +07:00
parent 585b71a1af
commit 987aa3a1fb
8 changed files with 870 additions and 968 deletions

View File

@ -8,7 +8,7 @@ client.on('ready', async () => {
'367827983903490050',
'https://assets.ppy.sh/beatmaps/1550633/covers/list.jpg', // Required if the image you use is not in Discord
);
const status = new RichPresence()
const status = new RichPresence(client)
.setApplicationId('367827983903490050')
.setType('PLAYING')
.setURL('https://www.youtube.com/watch?v=5icFcPkVzMg')
@ -26,7 +26,7 @@ client.on('ready', async () => {
.setAssetsSmallText('click the circles')
.addButton('Beatmap', 'https://osu.ppy.sh/beatmapsets/1391659#osu/2873429');
// Custom Status
const custom = new CustomStatus().setEmoji('😋').setState('yum');
const custom = new CustomStatus(client).setEmoji('😋').setState('yum');
// Spotify
const spotify = new SpotifyRPC(client)
.setAssetsLargeImage('spotify:ab67616d00001e02768629f8bc5b39b68797d1bb') // Image ID

View File

@ -150,9 +150,9 @@ exports.WelcomeScreen = require('./structures/WelcomeScreen');
exports.WebSocket = require('./WebSocket');
exports.CustomStatus = require('./structures/RichPresence').CustomStatus;
exports.RichPresence = require('./structures/RichPresence').RichPresence;
exports.SpotifyRPC = require('./structures/RichPresence').SpotifyRPC;
exports.CustomStatus = require('./structures/Presence').CustomStatus;
exports.RichPresence = require('./structures/Presence').RichPresence;
exports.SpotifyRPC = require('./structures/Presence').SpotifyRPC;
exports.WebEmbed = require('./structures/WebEmbed');
exports.DiscordAuthWebsocket = require('./util/RemoteAuth');
exports.PurchasedFlags = require('./util/PurchasedFlags');

View File

@ -3,7 +3,7 @@
const { Collection } = require('@discordjs/collection');
const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors/DJSError');
const { CustomStatus } = require('../structures/RichPresence');
const { CustomStatus } = require('../structures/Presence');
const { ActivityTypes } = require('../util/Constants');
/**

View File

@ -22,7 +22,7 @@ class ClientPresence extends Presence {
*/
set(presence) {
const packet = this._parse(presence);
this._patch(packet, true);
this._patch(packet);
if (typeof presence.shardId === 'undefined') {
this.client.ws.broadcast({ op: Opcodes.STATUS_UPDATE, d: packet });
} else if (Array.isArray(presence.shardId)) {

View File

@ -1,8 +1,7 @@
'use strict';
const { randomUUID } = require('node:crypto');
const Base = require('./Base');
const { Emoji } = require('./Emoji');
const { CustomStatus, SpotifyRPC, RichPresence } = require('./RichPresence');
const ActivityFlags = require('../util/ActivityFlags');
const { ActivityTypes } = require('../util/Constants');
const Util = require('../util/Util');
@ -77,7 +76,7 @@ class Presence extends Base {
return this.guild.members.resolve(this.userId);
}
_patch(data, fromClient) {
_patch(data) {
if ('status' in data) {
/**
* The status of this presence
@ -91,19 +90,15 @@ class Presence extends Base {
if ('activities' in data) {
/**
* The activities of this presence (Always `Activity[]` if not ClientUser)
* @type {Activity[]|CustomStatus[]|RichPresence[]|SpotifyRPC[]}
* @type {CustomStatus[]|RichPresence[]|SpotifyRPC[]}
*/
this.activities = data.activities.map(activity => {
if (fromClient === true) {
if ([ActivityTypes.CUSTOM, 'CUSTOM'].includes(activity.type)) {
return new CustomStatus(activity, this);
return new CustomStatus(this.client, activity);
} else if (activity.id == 'spotify:1') {
return new SpotifyRPC(this.client, activity, this);
return new SpotifyRPC(this.client, activity);
} else {
return new RichPresence(this.client, activity, false, this);
}
} else {
return new Activity(this, activity);
return new RichPresence(this.client, activity);
}
});
} else {
@ -184,78 +179,163 @@ class Activity {
*/
Object.defineProperty(this, 'presence', { value: presence });
this._patch(data);
}
_patch(data = {}) {
if ('id' in data) {
/**
* The activity's id
* @type {string}
*/
this.id = data.id;
}
if ('name' in data) {
/**
* The activity's name
* @type {string}
*/
this.name = data.name;
}
if ('type' in data) {
/**
* The activity status's type
* @type {ActivityType}
*/
this.type = typeof data.type === 'number' ? ActivityTypes[data.type] : data.type;
}
if ('url' in data) {
/**
* If the activity is being streamed, a link to the stream
* @type {?string}
*/
this.url = data.url ?? null;
this.url = data.url;
} else {
this.url = null;
}
if ('created_at' in data) {
/**
* Details about the activity
* Creation date of the activity
* @type {number}
*/
this.createdTimestamp = data.created_at;
}
if ('session_id' in data) {
/**
* The game's or Spotify session's id
* @type {?string}
*/
this.details = data.details ?? null;
this.sessionId = data.session_id;
} else {
this.sessionId = null;
}
if ('platform' in data) {
/**
* State of the activity
* @type {?string}
* The platform the game is being played on
* @type {?ActivityPlatform}
*/
this.state = data.state ?? null;
/**
* The id of the application associated with this activity
* @type {?Snowflake}
*/
this.applicationId = data.application_id ?? null;
this.platform = data.platform;
} else {
this.platform = null;
}
if ('timestamps' in data && data.timestamps) {
/**
* Represents timestamps of an activity
* @typedef {Object} ActivityTimestamps
* @property {?Date} start When the activity started
* @property {?Date} end When the activity will end
* @property {?number} start When the activity started
* @property {?number} end When the activity will end
*/
/**
* Timestamps for the activity
* @type {?ActivityTimestamps}
*/
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,
this.timestamps = {
start: data.timestamps.start ? new Date(data.timestamps.start).getTime() : null,
end: data.timestamps.end ? new Date(data.timestamps.end).getTime() : null,
};
} else {
this.timestamps = null;
}
: null;
if ('application_id' in data) {
/**
* The id of the application associated with this activity
* @type {?Snowflake}
*/
this.applicationId = data.application_id;
} else {
this.applicationId = null;
}
if ('details' in data) {
/**
* Details about the activity
* @type {?string}
*/
this.details = data.details;
} else {
this.details = null;
}
if ('state' in data) {
/**
* State of the activity
* @type {?string}
*/
this.state = data.state;
} else {
this.state = null;
}
if ('sync_id' in data) {
/**
* The Spotify song's id
* @type {?string}
*/
this.syncId = data.sync_id ?? null;
this.syncId = data.sync_id;
} else {
this.syncId = null;
}
if ('flags' in data) {
/**
* The platform the game is being played on
* @type {?ActivityPlatform}
* Flags that describe the activity
* @type {Readonly<ActivityFlags>}
*/
this.platform = data.platform ?? null;
this.flags = new ActivityFlags(data.flags).freeze();
} else {
this.flags = new ActivityFlags().freeze();
}
if ('buttons' in data) {
/**
* The labels of the buttons of this rich presence
* @type {string[]}
*/
this.buttons = data.buttons;
} else {
this.buttons = [];
}
if ('emoji' in data && data.emoji) {
/**
* Emoji for a custom activity
* @type {?EmojiIdentifierResolvable}
*/
this.emoji = Util.resolvePartialEmoji(data.emoji);
} else {
this.emoji = null;
}
if ('party' in data) {
/**
* Represents a party of an activity
* @typedef {Object} ActivityParty
@ -267,43 +347,16 @@ class Activity {
* Party of the activity
* @type {?ActivityParty}
*/
this.party = data.party ?? null;
this.party = data.party;
} else {
this.party = null;
}
/**
* Assets for rich presence
* @type {?RichPresenceAssets}
*/
this.assets = data.assets ? new RichPresenceAssets(this, data.assets) : null;
/**
* Flags that describe the activity
* @type {Readonly<ActivityFlags>}
*/
this.flags = new ActivityFlags(data.flags).freeze();
/**
* Emoji for a custom activity
* @type {?Emoji}
*/
this.emoji = data.emoji ? new Emoji(presence.client, data.emoji) : null;
/**
* The game's or Spotify session's id
* @type {?string}
*/
this.sessionId = data.session_id ?? null;
/**
* The labels of the buttons of this rich presence
* @type {string[]}
*/
this.buttons = data.buttons ?? [];
/**
* Creation date of the activity
* @type {number}
*/
this.createdTimestamp = data.created_at;
this.assets = new RichPresenceAssets(this, data.assets);
}
/**
@ -345,6 +398,10 @@ class Activity {
_clone() {
return Object.assign(Object.create(this), this);
}
toJSON(...props) {
return Util.flatten(this, ...props);
}
}
/**
@ -360,29 +417,49 @@ class RichPresenceAssets {
*/
Object.defineProperty(this, 'activity', { value: activity });
this._patch(assets);
}
_patch(assets = {}) {
if ('large_text' in assets) {
/**
* Hover text for the large image
* @type {?string}
*/
this.largeText = assets.large_text ?? null;
this.largeText = assets.large_text;
} else {
this.largeText = null;
}
if ('small_text' in assets) {
/**
* Hover text for the small image
* @type {?string}
*/
this.smallText = assets.small_text ?? null;
this.smallText = assets.small_text;
} else {
this.smallText = null;
}
if ('large_image' in assets) {
/**
* The large image asset's id
* @type {?Snowflake}
*/
this.largeImage = assets.large_image ?? null;
this.largeImage = assets.large_image;
} else {
this.largeImage = null;
}
if ('small_image' in assets) {
/**
* The small image asset's id
* @type {?Snowflake}
*/
this.smallImage = assets.small_image ?? null;
this.smallImage = assets.small_image;
} else {
this.smallImage = null;
}
}
/**
@ -397,6 +474,12 @@ class RichPresenceAssets {
switch (platform) {
case 'mp':
return `https://media.discordapp.net/${id}`;
case 'spotify':
return `https://i.scdn.co/image/${id}`;
case 'youtube':
return `https://i.ytimg.com/vi/${id}/hqdefault_live.jpg`;
case 'twitch':
return `https://static-cdn.jtvnw.net/previews-ttv/live_user_${id}.png`;
default:
return null;
}
@ -436,8 +519,545 @@ class RichPresenceAssets {
size,
});
}
static parseImage(image) {
if (typeof image != 'string') {
image = null;
} else if (URL.canParse(image) && ['http:', 'https:'].includes(new URL(image).protocol)) {
// Discord URL:
image = image
.replace('https://cdn.discordapp.com/', 'mp:')
.replace('http://cdn.discordapp.com/', 'mp:')
.replace('https://media.discordapp.net/', 'mp:')
.replace('http://media.discordapp.net/', 'mp:');
//
if (!image.startsWith('mp:')) {
throw new Error('INVALID_URL');
}
} else if (/^[0-9]{17,19}$/.test(image)) {
// ID Assets
} else if (['mp:', 'youtube:', 'spotify:', 'twitch:'].some(v => image.startsWith(v))) {
// Image
} else if (image.startsWith('external/')) {
image = `mp:${image}`;
}
return image;
}
toJSON() {
if (!this.largeImage && !this.largeText && !this.smallImage && !this.smallText) return null;
return {
large_image: RichPresenceAssets.parseImage(this.largeImage),
large_text: this.largeText,
small_image: RichPresenceAssets.parseImage(this.smallImage),
small_text: this.smallText,
};
}
/**
* @typedef {string} RichPresenceImage
* Support:
* - cdn.discordapp.com
* - media.discordapp.net
* - Assets ID (https://discord.com/api/v9/oauth2/applications/{application_id}/assets)
* - Media Proxy (mp:external/{hash})
* - Twitch (twitch:{username})
* - YouTube (youtube:{video_id})
* - Spotify (spotify:{image_id})
*/
/**
* Set the large image of this activity
* @param {?RichPresenceImage} image The large image asset's id
* @returns {RichPresenceAssets}
*/
setLargeImage(image) {
image = RichPresenceAssets.parseImage(image);
this.largeImage = image;
return this;
}
/**
* Set the small image of this activity
* @param {?RichPresenceImage} image The small image asset's id
* @returns {RichPresenceAssets}
*/
setSmallImage(image) {
image = RichPresenceAssets.parseImage(image);
this.smallImage = image;
return this;
}
/**
* Hover text for the large image
* @param {string} text Assets text
* @returns {RichPresenceAssets}
*/
setLargeText(text) {
this.largeText = text;
return this;
}
/**
* Hover text for the small image
* @param {string} text Assets text
* @returns {RichPresenceAssets}
*/
setSmallText(text) {
this.smallText = text;
return this;
}
}
class CustomStatus extends Activity {
/**
* @typedef {Object} CustomStatusOptions
* @property {string} [state] The state to be displayed
* @property {EmojiIdentifierResolvable} [emoji] The emoji to be displayed
*/
/**
* @param {Client} client Discord Client
* @param {CustomStatus|CustomStatusOptions} [data={}] CustomStatus to clone or raw data
*/
constructor(client, data = {}) {
super(client.presence, {
name: 'Custom Status',
type: ActivityTypes.CUSTOM,
...data,
});
}
/**
* Set the emoji of this activity
* @param {EmojiIdentifierResolvable} emoji The emoji to be displayed
* @returns {CustomStatus}
*/
setEmoji(emoji) {
this.emoji = Util.resolvePartialEmoji(emoji);
return this;
}
/**
* Set state of this activity
* @param {string | null} state The state to be displayed
* @returns {CustomStatus}
*/
setState(state) {
if (typeof state == 'string' && state.length > 128) throw new Error('State must be less than 128 characters');
this.state = state;
return this;
}
/**
* Returns an object that can be used to set the status
* @returns {CustomStatus}
*/
toJSON() {
if (!this.emoji & !this.state) throw new Error('CustomStatus must have at least one of emoji or state');
return {
name: this.name,
emoji: this.emoji,
type: this.type,
state: this.state,
};
}
}
class RichPresence extends Activity {
/**
* @param {Client} client Discord client
* @param {RichPresence} [data={}] RichPresence to clone or raw data
*/
constructor(client, data = {}) {
super(client.presence, { type: 0, ...data });
this.setup(data);
}
/**
* Sets the status from a JSON object
* @param {RichPresence} data data
* @private
*/
setup(data = {}) {
this.secrets = 'secrets' in data ? data.secrets : {};
this.metadata = 'metadata' in data ? data.metadata : {};
}
/**
* Set the large image of this activity
* @param {?RichPresenceImage} image The large image asset's id
* @returns {RichPresence}
* @deprecated
*/
setAssetsLargeImage(image) {
this.assets.setLargeImage(image);
return this;
}
/**
* Set the small image of this activity
* @param {?RichPresenceImage} image The small image asset's id
* @returns {RichPresence}
* @deprecated
*/
setAssetsSmallImage(image) {
this.assets.setSmallImage(image);
return this;
}
/**
* Hover text for the large image
* @param {string} text Assets text
* @returns {RichPresence}
* @deprecated
*/
setAssetsLargeText(text) {
this.assets.setLargeText(text);
return this;
}
/**
* Hover text for the small image
* @param {string} text Assets text
* @returns {RichPresence}
* @deprecated
*/
setAssetsSmallText(text) {
this.assets.setSmallText(text);
return this;
}
/**
* Set the name of the activity
* @param {?string} name The activity's name
* @returns {RichPresence}
*/
setName(name) {
this.name = name;
return this;
}
/**
* If the activity is being streamed, a link to the stream
* @param {?string} url URL of the stream
* @returns {RichPresence}
*/
setURL(url) {
if (typeof url == 'string' && !URL.canParse(url)) throw new Error('URL must be a valid URL');
this.url = url;
return this;
}
/**
* The activity status's type
* @param {?ActivityTypes} type The type of activity
* @returns {RichPresence}
*/
setType(type) {
this.type = typeof type == 'number' ? type : ActivityTypes[type];
return this;
}
/**
* Set the application id of this activity
* @param {?Snowflake} id Bot's id
* @returns {RichPresence}
*/
setApplicationId(id) {
this.applicationId = id;
return this;
}
/**
* Set the state of the activity
* @param {?string} state The state of the activity
* @returns {RichPresence}
*/
setState(state) {
this.state = state;
return this;
}
/**
* Set the details of the activity
* @param {?string} details The details of the activity
* @returns {RichPresence}
*/
setDetails(details) {
this.details = details;
return this;
}
/**
* @typedef {Object} RichParty
* @property {string} id The id of the party
* @property {number} max The maximum number of members in the party
* @property {number} current The current number of members in the party
*/
/**
* Set the party of this activity
* @param {?RichParty} party The party to be displayed
* @returns {RichPresence}
*/
setParty(party) {
if (typeof party == 'object') {
if (!party.max || typeof party.max != 'number') throw new Error('Party must have max number');
if (!party.current || typeof party.current != 'number') throw new Error('Party must have current');
if (party.current > party.max) throw new Error('Party current must be less than max number');
if (!party.id || typeof party.id != 'string') party.id = randomUUID();
this.party = {
size: [party.current, party.max],
id: party.id,
};
} else {
this.party = null;
}
return this;
}
/**
* Sets the start timestamp of the activity
* @param {Date|number|null} timestamp The timestamp of the start of the activity
* @returns {RichPresence}
*/
setStartTimestamp(timestamp) {
if (!this.timestamps) this.timestamps = {};
if (timestamp instanceof Date) timestamp = timestamp.getTime();
this.timestamps.start = timestamp;
return this;
}
/**
* Sets the end timestamp of the activity
* @param {Date|number|null} timestamp The timestamp of the end of the activity
* @returns {RichPresence}
*/
setEndTimestamp(timestamp) {
if (!this.timestamps) this.timestamps = {};
if (timestamp instanceof Date) timestamp = timestamp.getTime();
this.timestamps.end = timestamp;
return this;
}
/**
* @typedef {object} RichButton
* @property {string} name The name of the button
* @property {string} url The url of the button
*/
/**
* Set the buttons of the rich presence
* @param {...?RichButton} button A list of buttons to set
* @returns {RichPresence}
*/
setButtons(...button) {
if (button.length == 0) {
this.buttons = [];
delete this.metadata.button_urls;
return this;
} else if (button.length > 2) {
throw new Error('RichPresence can only have up to 2 buttons');
}
this.buttons = [];
this.metadata.button_urls = [];
button.flat(2).forEach(b => {
if (b.name && b.url) {
this.buttons.push(b.name);
if (!URL.canParse(b.url)) throw new Error('Button url must be a valid url');
this.metadata.button_urls.push(b.url);
} else {
throw new Error('Button must have name and url');
}
});
return this;
}
/**
* Add a button to the rich presence
* @param {string} name The name of the button
* @param {string} url The url of the button
* @returns {RichPresence}
*/
addButton(name, url) {
if (!name || !url) {
throw new Error('Button must have name and url');
}
if (typeof name !== 'string') throw new Error('Button name must be a string');
if (!URL.canParse(url)) throw new Error('Button url must be a valid url');
this.buttons.push(name);
if (Array.isArray(this.metadata.button_urls)) this.metadata.button_urls.push(url);
else this.metadata.button_urls = [url];
return this;
}
/**
* Convert the rich presence to a JSON object
* @returns {Object}
*/
toJSON(...props) {
return super.toJSON(
{
applicationId: 'application_id',
sessionId: 'session_id',
syncId: 'sync_id',
createdTimestamp: 'created_at',
},
...props,
);
}
/**
* @typedef {Object} ExternalAssets
* @property {?string} url Orginal url of the image
* @property {?string} external_asset_path Proxy url of the image (Using to RPC)
*/
/**
* Get Assets from a RichPresence (Util)
* @param {Client} client Discord Client
* @param {Snowflake} applicationId Application id
* @param {string} image1 URL image 1 (not from Discord)
* @param {string} image2 URL image 2 (not from Discord)
* @returns {ExternalAssets[]}
*/
static async getExternal(client, applicationId, image1 = '', image2 = '') {
if (!client || !client.token || !client.api) throw new Error('Client must be set');
// Check if applicationId is discord snowflake (17 , 18, 19 numbers)
if (!/^[0-9]{17,19}$/.test(applicationId)) {
throw new Error('Application id must be a Discord Snowflake');
}
// Check if large_image is a valid url
if (image1 && image1.length > 0 && !URL.canParse(image1)) {
throw new Error('Image 1 must be a valid url');
}
// Check if small_image is a valid url
if (image2 && image2.length > 0 && !URL.canParse(image2)) {
throw new Error('Image 2 must be a valid url');
}
const data_ = [];
if (image1) data_.push(image1);
if (image2) data_.push(image2);
const res = await client.api.applications[applicationId]['external-assets'].post({
data: {
urls: data_,
},
});
return res;
}
/**
* 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);
}
}
/**
* @extends {RichPresence}
*/
class SpotifyRPC extends RichPresence {
/**
* Create a new RichPresence (Spotify style)
* @param {Client} client Discord Client
* @param {SpotifyRPC} [options] Options for the Spotify RPC
*/
constructor(client, options = {}) {
super(client, {
name: 'Spotify',
type: ActivityTypes.LISTENING,
party: {
id: `spotify:${client.user.id}`,
},
id: 'spotify:1',
flags: 48, // Sync + Play (ActivityFlags)
...options,
});
this.setup(options);
}
/**
* Sets the status from a JSON object
* @param {SpotifyRPC} options data
* @private
*/
setup(options) {
/**
* @typedef {Object} SpotifyMetadata
* @property {string} album_id The Spotify ID of the album of the song being played
* @property {Array<string>} artist_ids The Spotify IDs of the artists of the song being played
* @property {string} context_uri The Spotify URI of the current player context
*/
/**
* Spotify metadata
* @type {SpotifyMetadata}
*/
this.metadata = {
album_id: options.metadata?.album_id || null,
artist_ids: options.metadata?.artist_ids || [],
context_uri: options.metadata?.context_uri || null,
};
}
/**
* Set Spotify song id to sync with
* @param {string} id Song id
* @returns {SpotifyRPC}
*/
setSongId(id) {
this.syncId = id;
return this;
}
/**
* Add the artist id
* @param {string} id Artist id
* @returns {SpotifyRPC}
*/
addArtistId(id) {
if (!this.metadata.artist_ids) this.metadata.artist_ids = [];
this.metadata.artist_ids.push(id);
return this;
}
/**
* Set the artist ids
* @param {string | Array<string>} ids Artist ids
* @returns {SpotifyRPC}
*/
setArtistIds(...ids) {
if (!ids?.length) {
this.metadata.artist_ids = [];
return this;
}
if (!this.metadata.artist_ids) this.metadata.artist_ids = [];
ids.flat(2).forEach(id => this.metadata.artist_ids.push(id));
return this;
}
/**
* Set the album id
* @param {string} id Album id
* @returns {SpotifyRPC}
*/
setAlbumId(id) {
this.metadata.album_id = id;
this.metadata.context_uri = `spotify:album:${id}`;
return this;
}
}
exports.Presence = Presence;
exports.Activity = Activity;
exports.RichPresenceAssets = RichPresenceAssets;
exports.CustomStatus = CustomStatus;
exports.RichPresence = RichPresence;
exports.SpotifyRPC = SpotifyRPC;

View File

@ -1,702 +0,0 @@
'use strict';
const { randomUUID } = require('node:crypto');
const { ActivityTypes } = require('../util/Constants');
const { resolvePartialEmoji } = require('../util/Util');
// eslint-disable-next-line
const checkUrl = url => {
try {
return new URL(url);
} catch {
return false;
}
};
class CustomStatus {
/**
* @typedef {Object} CustomStatusOptions
* @property {string} [state] The state to be displayed
* @property {EmojiIdentifierResolvable} [emoji] The emoji to be displayed
*/
/**
* @param {CustomStatus|CustomStatusOptions} [data={}] CustomStatus to clone or raw data
* @param {Presence} [presence] The presence this activity is part of
*/
constructor(data = {}, presence) {
Object.defineProperty(this, 'presence', { value: presence });
this.name = 'Custom Status';
/**
* The emoji to be displayed
* @type {?EmojiIdentifierResolvable}
*/
this.emoji = null;
this.type = ActivityTypes.CUSTOM;
/**
* The state to be displayed
* @type {?string}
*/
this.state = null;
this.setup(data);
}
/**
* Sets the status from a JSON object
* @param {CustomStatus|CustomStatusOptions} data CustomStatus to clone or raw data
* @private
*/
setup(data) {
this.emoji = data.emoji ? resolvePartialEmoji(data.emoji) : null;
this.state = data.state;
}
/**
* Set the emoji of this activity
* @param {EmojiIdentifierResolvable} emoji The emoji to be displayed
* @returns {CustomStatus}
*/
setEmoji(emoji) {
this.emoji = resolvePartialEmoji(emoji);
return this;
}
/**
* Set state of this activity
* @param {string | null} state The state to be displayed
* @returns {CustomStatus}
*/
setState(state) {
if (typeof state == 'string' && state.length > 128) throw new Error('State must be less than 128 characters');
this.state = state;
return this;
}
/**
* Returns an object that can be used to set the status
* @returns {CustomStatus}
*/
toJSON() {
if (!this.emoji & !this.state) throw new Error('CustomStatus must have at least one of emoji or state');
return {
name: this.name,
emoji: this.emoji,
type: this.type,
state: this.state,
};
}
/**
* 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);
}
}
class RichPresence {
/**
* @param {Client} [client] Discord client
* @param {RichPresence} [data={}] RichPresence to clone or raw data
* @param {boolean} [IPC=false] Whether to use IPC (RPC for Discord Apps)
* @param {Presence} [presence] The presence this activity is part of
*/
constructor(client = {}, data = {}, IPC = false, presence) {
Object.defineProperty(this, 'client', { value: client });
Object.defineProperty(this, 'presence', { value: presence });
/**
* The activity's name
* @type {string}
*/
this.name = null;
/**
* The activity status's type
* @type {ActivityType}
*/
this.type = ActivityTypes.PLAYING;
/**
* If the activity is being streamed, a link to the stream
* @type {?string}
*/
this.url = null;
/**
* The id of the application associated with this activity
* @type {?Snowflake}
*/
this.application_id = null;
/**
* State of the activity
* @type {?string}
*/
this.state = null;
/**
* Details about the activity
* @type {?string}
*/
this.details = null;
/**
* Party of the activity
* @type {?ActivityParty}
*/
this.party = null;
/**
* Timestamps for the activity
* @type {?ActivityTimestamps}
*/
this.timestamps = null;
/**
* Assets for rich presence
* @type {?RichPresenceAssets}
*/
this.assets = null;
/**
* The labels of the buttons of this rich presence
* @type {string[]}
*/
this.buttons = null;
this.ipc = IPC;
this.setup(data);
}
/**
* Sets the status from a JSON object
* @param {RichPresence} data data
* @private
*/
setup(data) {
this.name = data.name;
this.type = typeof data.type != 'number' ? ActivityTypes[data.type?.toUpperCase()] : data.type;
this.application_id = data.application_id;
this.url = data.url;
this.state = data.state;
this.details = data.details;
this.party = data.party;
this.timestamps = data.timestamps;
this.created_at = data.created_at;
this.secrets = data.secrets;
this.assets = data.assets;
this.buttons = data.buttons;
this.metadata = data.metadata;
}
/**
* @typedef {string} RichPresenceImage
* Support:
* - cdn.discordapp.com
* - media.discordapp.net
* - Asset ID (From https://discord.com/api/v9/oauth2/applications/:id/assets)
* - ExternalAssets (mp:external/)
*/
/**
* Set the large image of this activity
* @param {?RichPresenceImage} image The large image asset's id
* @returns {RichPresence}
*/
setAssetsLargeImage(image) {
if (!(this.assets instanceof Object)) this.assets = {};
if (typeof image != 'string') {
image = null;
} else if (['http:', 'https:'].includes(checkUrl(image)?.protocol)) {
// Discord URL:
image = image
.replace('https://cdn.discordapp.com/', 'mp:')
.replace('http://cdn.discordapp.com/', 'mp:')
.replace('https://media.discordapp.net/', 'mp:')
.replace('http://media.discordapp.net/', 'mp:');
//
if (!image.startsWith('mp:') && !this.ipc) {
throw new Error('INVALID_URL');
}
} else if (/^[0-9]{17,19}$/.test(image)) {
// ID Assets
} else if (image.startsWith('mp:') || image.startsWith('youtube:') || image.startsWith('spotify:')) {
// Image
} else if (image.startsWith('external/')) {
image = `mp:${image}`;
}
this.assets.large_image = image;
return this;
}
/**
* Set the small image of this activity
* @param {?RichPresenceImage} image The small image asset's id
* @returns {RichPresence}
*/
setAssetsSmallImage(image) {
if (!(this.assets instanceof Object)) this.assets = {};
if (typeof image != 'string') {
image = null;
} else if (['http:', 'https:'].includes(checkUrl(image)?.protocol)) {
// Discord URL:
image = image
.replace('https://cdn.discordapp.com/', 'mp:')
.replace('http://cdn.discordapp.com/', 'mp:')
.replace('https://media.discordapp.net/', 'mp:')
.replace('http://media.discordapp.net/', 'mp:');
//
if (!image.startsWith('mp:') && !this.ipc) {
throw new Error('INVALID_URL');
}
} else if (/^[0-9]{17,19}$/.test(image)) {
// ID Assets
} else if (image.startsWith('mp:') || image.startsWith('youtube:') || image.startsWith('spotify:')) {
// Image
} else if (image.startsWith('external/')) {
image = `mp:${image}`;
}
this.assets.small_image = image;
return this;
}
/**
* Hover text for the large image
* @param {string} text Assets text
* @returns {RichPresence}
*/
setAssetsLargeText(text) {
if (typeof this.assets !== 'object') this.assets = {};
this.assets.large_text = text;
return this;
}
/**
* Hover text for the small image
* @param {string} text Assets text
* @returns {RichPresence}
*/
setAssetsSmallText(text) {
if (typeof this.assets !== 'object') this.assets = {};
this.assets.small_text = text;
return this;
}
/**
* Set the name of the activity
* @param {?string} name The activity's name
* @returns {RichPresence}
*/
setName(name) {
this.name = name;
return this;
}
/**
* If the activity is being streamed, a link to the stream
* @param {?string} url URL of the stream
* @returns {RichPresence}
*/
setURL(url) {
if (typeof url == 'string' && !checkUrl(url)) throw new Error('URL must be a valid URL');
if (typeof url != 'string') url = null;
this.url = url;
return this;
}
/**
* The activity status's type
* @param {?ActivityTypes} type The type of activity
* @returns {RichPresence}
*/
setType(type) {
this.type = ActivityTypes[type];
if (typeof this.type == 'string') this.type = ActivityTypes[this.type];
if (typeof this.type != 'number') throw new Error('Type must be a valid ActivityType');
return this;
}
/**
* Set the application id of this activity
* @param {?Snowflake} id Bot's id
* @returns {RichPresence}
*/
setApplicationId(id) {
this.application_id = id;
return this;
}
/**
* Set the state of the activity
* @param {?string} state The state of the activity
* @returns {RichPresence}
*/
setState(state) {
this.state = state;
return this;
}
/**
* Set the details of the activity
* @param {?string} details The details of the activity
* @returns {RichPresence}
*/
setDetails(details) {
this.details = details;
return this;
}
/**
* @typedef {Object} RichParty
* @property {string} id The id of the party
* @property {number} max The maximum number of members in the party
* @property {number} current The current number of members in the party
*/
/**
* Set the party of this activity
* @param {?RichParty} party The party to be displayed
* @returns {RichPresence}
*/
setParty(party) {
if (typeof party == 'object') {
if (!party.max || typeof party.max != 'number') throw new Error('Party must have max number');
if (!party.current || typeof party.current != 'number') throw new Error('Party must have current');
if (party.current > party.max) throw new Error('Party current must be less than max number');
if (!party.id || typeof party.id != 'string') party.id = randomUUID();
this.party = {
size: [party.current, party.max],
id: party.id,
};
} else {
this.party = null;
}
return this;
}
/**
* Sets the start timestamp of the activity
* @param {?number} timestamp The timestamp of the start of the activity
* @returns {RichPresence}
*/
setStartTimestamp(timestamp) {
if (!this.timestamps) this.timestamps = {};
this.timestamps.start = timestamp;
return this;
}
/**
* Sets the end timestamp of the activity
* @param {?number} timestamp The timestamp of the end of the activity
* @returns {RichPresence}
*/
setEndTimestamp(timestamp) {
if (!this.timestamps) this.timestamps = {};
this.timestamps.end = timestamp;
return this;
}
/**
* @typedef {object} RichButton
* @property {string} name The name of the button
* @property {string} url The url of the button
*/
/**
* Set the buttons of the rich presence
* @param {...?RichButton} button A list of buttons to set
* @returns {RichPresence}
*/
setButtons(...button) {
if (button.length == 0) {
this.buttons = null;
delete this.metadata;
return this;
} else if (button.length > 2) {
throw new Error('RichPresence can only have up to 2 buttons');
}
this.buttons = [];
this.metadata = {
button_urls: [],
};
button.flat(2).forEach(b => {
if (b.name && b.url) {
this.buttons.push(b.name);
if (!checkUrl(b.url)) throw new Error('Button url must be a valid url');
this.metadata.button_urls.push(b.url);
} else {
throw new Error('Button must have name and url');
}
});
return this;
}
/**
* Add a button to the rich presence
* @param {string} name The name of the button
* @param {string} url The url of the button
* @returns {RichPresence}
*/
addButton(name, url) {
if (!name || !url) {
throw new Error('Button must have name and url');
}
if (typeof name !== 'string') throw new Error('Button name must be a string');
if (!checkUrl(url)) throw new Error('Button url must be a valid url');
if (!this.buttons) {
this.buttons = [];
this.metadata = {
button_urls: [],
};
}
this.buttons.push(name);
this.metadata.button_urls.push(url);
return this;
}
/**
* Convert the rich presence to a JSON object
* @returns {Object}
*/
toJSON() {
/**
* * Verify Timestamps
*/
if (this.timestamps?.start || this.timestamps?.end) {
if (this.timestamps?.start instanceof Date) {
this.timestamps.start = Math.round(this.timestamps?.start.getTime());
}
if (this.timestamps.end instanceof Date) {
this.timestamps.end = Math.round(this.timestamps.end.getTime());
}
if (this.timestamps.start > 2147483647000) {
throw new RangeError('timestamps.start must fit into a unix timestamp');
}
if (this.timestamps.end > 2147483647000) {
throw new RangeError('timestamps.end must fit into a unix timestamp');
}
}
const obj = {
name: this.name,
type: this.type || 0, // PLAYING
application_id: this.application_id,
url: this.url,
state: this.state,
details: this.details,
party: this.party,
timestamps: this.timestamps || {},
secrets: this.secrets,
assets: this.assets || {},
buttons: this.buttons,
metadata: this.metadata,
};
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]);
if (!this.ipc) {
return obj;
} else {
delete obj.application_id;
delete obj.name;
delete obj.url;
obj.type = 0;
if (obj.buttons?.length) {
obj.buttons = obj.buttons.map((b, i) => ({ label: b, url: obj.metadata.button_urls[i] }));
delete obj.metadata;
}
return obj;
}
}
/**
* Get Assets from a RichPresence (Util)
* @param {Client} client Discord Client
* @param {Snowflake} applicationId Application id
* @param {string} image1 URL image 1 (not from Discord)
* @param {string} image2 URL image 2 (not from Discord)
* @returns {ExternalAssets[]}
*/
static async getExternal(client, applicationId, image1 = '', image2 = '') {
const checkURL = url => {
try {
// eslint-disable-next-line no-new
new URL(url);
return true;
} catch (e) {
return false;
}
};
if (!client || !client.token || !client.api) throw new Error('Client must be set');
// Check if applicationId is discord snowflake (17 , 18, 19 numbers)
if (!/^[0-9]{17,19}$/.test(applicationId)) {
throw new Error('Application id must be a Discord Snowflake');
}
// Check if large_image is a valid url
if (image1 && image1.length > 0 && !checkURL(image1)) {
throw new Error('Image 1 must be a valid url');
}
// Check if small_image is a valid url
if (image2 && image2.length > 0 && !checkURL(image2)) {
throw new Error('Image 2 must be a valid url');
}
const data_ = [];
if (image1) data_.push(image1);
if (image2) data_.push(image2);
const res = await client.api.applications[applicationId]['external-assets'].post({
data: {
urls: data_,
},
});
return res;
}
/**
* 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);
}
}
/**
* @extends {RichPresence}
*/
class SpotifyRPC extends RichPresence {
/**
* Create a new RichPresence (Spotify style)
* @param {Client} client Discord Client
* @param {SpotifyRPC} options Options for the Spotify RPC
* @param {Presence} presence Presence
*/
constructor(client, options = {}, presence) {
if (!client) throw new Error('Client must be set');
super(client, options, false, presence);
this.setup(options);
}
/**
* Sets the status from a JSON object
* @param {SpotifyRPC} options data
* @private
*/
setup(options) {
this.name = options.name || 'Spotify';
this.type = ActivityTypes.LISTENING;
this.party = {
id: `spotify:${this.client.user.id}`,
};
/**
* The Spotify song's id
* @type {?string}
*/
this.sync_id = options.sync_id;
/**
* The activity's id
* @type {string}
*/
this.id = 'spotify:1';
/**
* Flags that describe the activity
* @type {ActivityFlags}
*/
this.flags = 48; // Sync + Play (ActivityFlags)
/**
* @typedef {Object} SpotifyMetadata
* @property {string} album_id Album id
* @property {Array<string>} artist_ids Artist ids
*/
/**
* Spotify metadata
* @type {SpotifyMetadata}
*/
this.metadata = {
album_id: options.metadata?.album_id || null,
artist_ids: options.metadata?.artist_ids || [],
context_uri: null,
};
}
/**
* Set the large image of this activity
* @param {?string} image Spotify song's image ID
* @returns {SpotifyRPC}
*/
setAssetsLargeImage(image) {
if (image.startsWith('spotify:')) image = image.replace('spotify:', '');
super.setAssetsLargeImage(`spotify:${image}`);
return this;
}
/**
* Set the small image of this activity
* @param {?string} image Spotify song's image ID
* @returns {RichPresence}
*/
setAssetsSmallImage(image) {
if (image.startsWith('spotify:')) image = image.replace('spotify:', '');
super.setAssetsSmallImage(`spotify:${image}`);
return this;
}
/**
* Set Spotify song id to sync with
* @param {string} id Song id
* @returns {SpotifyRPC}
*/
setSongId(id) {
this.sync_id = id;
return this;
}
/**
* Add the artist id
* @param {string} id Artist id
* @returns {SpotifyRPC}
*/
addArtistId(id) {
if (!this.metadata.artist_ids) this.metadata.artist_ids = [];
this.metadata.artist_ids.push(id);
return this;
}
/**
* Set the artist ids
* @param {string | Array<string>} ids Artist ids
* @returns {SpotifyRPC}
*/
setArtistIds(...ids) {
if (!ids?.length) {
this.metadata.artist_ids = [];
return this;
}
if (!this.metadata.artist_ids) this.metadata.artist_ids = [];
ids.flat(2).forEach(id => this.metadata.artist_ids.push(id));
return this;
}
/**
* Set the album id
* @param {string} id Album id
* @returns {SpotifyRPC}
*/
setAlbumId(id) {
this.metadata.album_id = id;
this.metadata.context_uri = `spotify:album:${id}`;
return this;
}
/**
* Convert the rich presence to a JSON object
* @returns {SpotifyRPC}
*/
toJSON() {
if (!this.sync_id) throw new Error('Song id is required');
const obj = {
name: this.name,
type: this.type,
application_id: this.application_id,
url: this.url,
state: this.state,
details: this.details,
party: this.party,
timestamps: this.timestamps || {},
assets: this.assets || {},
sync_id: this.sync_id,
flags: this.flags,
metadata: this.metadata,
};
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]);
return obj;
}
}
/**
* @typedef {Object} ExternalAssets
* @property {?string} url Orginal url of the image
* @property {?string} external_asset_path Proxy url of the image (Using to RPC)
*/
module.exports = {
CustomStatus,
RichPresence,
SpotifyRPC,
};

View File

@ -795,6 +795,19 @@ class Util extends null {
.catch(reject);
});
}
/**
* Lazily evaluates a callback function (yea it's v14 :yay:)
* @param {Function} cb The callback to lazily evaluate
* @returns {Function}
* @example
* const User = lazy(() => require('./User'));
* const user = new (User())(client, data);
*/
static lazy(cb) {
let defaultValue;
return () => (defaultValue ??= cb());
}
}
module.exports = Util;

105
typings/index.d.ts vendored
View File

@ -173,28 +173,16 @@ export interface RichButton {
url: string;
}
export class RichPresence {
public constructor(client?: Client, data?: object, IPC?: boolean);
public application_id: Snowflake | null;
public assets: RichPresenceAssets | null;
public buttons: string[];
public details: string | null;
public name: string;
public party: {
id: string | null;
size: [number, number];
} | null;
public state: string | null;
public timestamps: {
start: Date | null;
end: Date | null;
} | null;
public type: ActivityType;
public url: string | null;
public ipc: boolean;
export class RichPresence extends Activity {
public constructor(client: Client, data?: object);
public metadata: RichPresenceMetadata;
/** @deprecated */
public setAssetsLargeImage(image?: string): this;
/** @deprecated */
public setAssetsLargeText(text?: string): this;
/** @deprecated */
public setAssetsSmallImage(image?: string): this;
/** @deprecated */
public setAssetsSmallText(text?: string): this;
public setName(name?: string): this;
public setURL(url?: string): this;
@ -203,8 +191,8 @@ export class RichPresence {
public setDetails(details?: string): this;
public setState(state?: string): this;
public setParty(party?: { max: number; current: number; id?: string }): this;
public setStartTimestamp(timestamp?: Date): this;
public setEndTimestamp(timestamp?: Date): this;
public setStartTimestamp(timestamp: Date | number | null): this;
public setEndTimestamp(timestamp: Date | number | null): this;
public setButtons(...button: RichButton[]): this;
public addButton(name: string, url: string): this;
public static getExternal(
@ -243,62 +231,39 @@ export interface ExternalAssets {
external_asset_path: string;
}
export interface SpotifyMetadata {
album_id: string;
artist_ids: string[];
export interface RichPresenceMetadata {
album_id?: string;
artist_ids?: string[];
context_uri?: string;
button_urls?: string[];
}
export class SpotifyRPC extends RichPresence {
public constructor(client: Client, data?: object);
public application_id: Snowflake | null;
public client: Client;
public assets: RichPresenceAssets | null;
public buttons: string[];
public details: string | null;
public name: string;
public sync_id: string;
public id: string;
public flags: number;
public party: {
id: string | null;
size: [number, number];
} | null;
public state: string | null;
public timestamps: {
start: Date | null;
end: Date | null;
} | null;
public type: ActivityType;
public url: string | null;
public metadata: SpotifyMetadata;
public setAssetsLargeImage(image?: string): this;
public setAssetsSmallImage(image?: string): this;
public setSongId(id: string): this;
public addArtistId(id: string): this;
public setArtistIds(...ids: string[]): this;
public setAlbumId(id: string): this;
}
export class CustomStatus {
public constructor(data?: object);
public emoji: EmojiIdentifierResolvable;
public state: string;
export class CustomStatus extends Activity {
public constructor(client: Client, data?: object);
public setEmoji(emoji?: EmojiIdentifierResolvable): this;
public setState(state: string): this;
public toJSON(): object;
public toString(): string;
public toJSON(): unknown;
}
export class Activity {
private constructor(presence: Presence, data?: RawActivityData);
public constructor(presence: Presence, data?: RawActivityData);
public readonly presence: Presence;
public applicationId: Snowflake | null;
public assets: RichPresenceAssets | null;
public assets: RichPresenceAssets;
public buttons: string[];
public readonly createdAt: Date;
public createdTimestamp: number;
public details: string | null;
public emoji: Emoji | null;
public emoji: EmojiIdentifierResolvable | null;
public flags: Readonly<ActivityFlags>;
public id: string;
public name: string;
@ -311,8 +276,8 @@ export class Activity {
public state: string | null;
public syncId: string | null;
public timestamps: {
start: Date | null;
end: Date | null;
start: number | null;
end: number | null;
} | null;
public type: ActivityType;
public url: string | null;
@ -1586,7 +1551,7 @@ export class HTTPError extends Error {
}
// tslint:disable-next-line:no-empty-interface - Merge RateLimitData into RateLimitError to not have to type it again
export interface RateLimitError extends RateLimitData {}
export interface RateLimitError extends RateLimitData { }
export class RateLimitError extends Error {
private constructor(data: RateLimitData);
public name: 'RateLimitError';
@ -2481,6 +2446,12 @@ export class RichPresenceAssets {
public smallText: string | null;
public largeImageURL(options?: StaticImageURLOptions): string | null;
public smallImageURL(options?: StaticImageURLOptions): string | null;
public static parseImage(image: string): string | null;
public toJSON(): unknown;
public setLargeImage(image?: string): this;
public setLargeText(text?: string): this;
public setSmallImage(image?: string): this;
public setSmallText(text?: string): this;
}
export class Role extends Base {
@ -4935,9 +4906,9 @@ export interface AutoModerationRuleCreateOptions {
reason?: string;
}
export interface AutoModerationRuleEditOptions extends Partial<Omit<AutoModerationRuleCreateOptions, 'triggerType'>> {}
export interface AutoModerationRuleEditOptions extends Partial<Omit<AutoModerationRuleCreateOptions, 'triggerType'>> { }
export interface AutoModerationTriggerMetadataOptions extends Partial<AutoModerationTriggerMetadata> {}
export interface AutoModerationTriggerMetadataOptions extends Partial<AutoModerationTriggerMetadata> { }
export interface AutoModerationActionOptions {
type: AutoModerationActionType | AutoModerationActionTypes;
@ -6006,8 +5977,8 @@ export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLo
INVITE: Invite;
MESSAGE: TActionType extends 'MESSAGE_BULK_DELETE' ? Guild | { id: Snowflake } : User;
INTEGRATION: Integration;
CHANNEL: NonThreadGuildBasedChannel | { id: Snowflake; [x: string]: unknown };
THREAD: ThreadChannel | { id: Snowflake; [x: string]: unknown };
CHANNEL: NonThreadGuildBasedChannel | { id: Snowflake;[x: string]: unknown };
THREAD: ThreadChannel | { id: Snowflake;[x: string]: unknown };
STAGE_INSTANCE: StageInstance;
STICKER: Sticker;
GUILD_SCHEDULED_EVENT: GuildScheduledEvent;
@ -6850,18 +6821,18 @@ export type Partialize<
partial: true;
} & {
[K in keyof Omit<T, 'client' | 'id' | 'partial' | E>]: K extends N ? null : K extends M ? T[K] | null : T[K];
};
};
export interface PartialDMChannel extends Partialize<DMChannel, null, null, 'lastMessageId'> {
lastMessageId: undefined;
}
export interface PartialGuildMember extends Partialize<GuildMember, 'joinedAt' | 'joinedTimestamp'> {}
export interface PartialGuildMember extends Partialize<GuildMember, 'joinedAt' | 'joinedTimestamp'> { }
export interface PartialMessage
extends Partialize<Message, 'type' | 'system' | 'pinned' | 'tts', 'content' | 'cleanContent' | 'author'> {}
extends Partialize<Message, 'type' | 'system' | 'pinned' | 'tts', 'content' | 'cleanContent' | 'author'> { }
export interface PartialMessageReaction extends Partialize<MessageReaction, 'count'> {}
export interface PartialMessageReaction extends Partialize<MessageReaction, 'count'> { }
export interface PartialOverwriteData {
id: Snowflake | number;
@ -6876,7 +6847,7 @@ export interface PartialRoleData extends RoleData {
export type PartialTypes = 'USER' | 'CHANNEL' | 'GUILD_MEMBER' | 'MESSAGE' | 'REACTION' | 'GUILD_SCHEDULED_EVENT';
export interface PartialUser extends Partialize<User, 'username' | 'tag' | 'discriminator'> {}
export interface PartialUser extends Partialize<User, 'username' | 'tag' | 'discriminator'> { }
export type PresenceStatusData = ClientPresenceStatus | 'invisible';