feat: metadata (SpotifyRPC) #537

hmm
This commit is contained in:
Elysia 2023-02-15 18:54:47 +07:00
parent 0314573c6f
commit 6ec5d808e8
4 changed files with 74 additions and 35 deletions

View File

@ -48,14 +48,16 @@ Rich Presence with Spotify
```js ```js
const Discord = require('discord.js-selfbot-v13'); const Discord = require('discord.js-selfbot-v13');
const r = new Discord.SpotifyRPC(client) const r = new Discord.SpotifyRPC(client)
.setAssetsLargeImage("spotify:ab67616d00001e02768629f8bc5b39b68797d1bb") // Image ID .setAssetsLargeImage("spotify:ab67616d00001e02768629f8bc5b39b68797d1bb") // Image ID
.setAssetsSmallImage("spotify:ab6761610000f178049d8aeae802c96c8208f3b7") // Image ID .setAssetsSmallImage("spotify:ab6761610000f178049d8aeae802c96c8208f3b7") // Image ID
.setAssetsLargeText('未来茶屋 (vol.1)') // Album Name .setAssetsLargeText('未来茶屋 (vol.1)') // Album Name
.setState('Yunomi; Kizuna AI') // Author .setState('Yunomi; Kizuna AI') // Artists
.setDetails('ロボットハート') // Song name .setDetails('ロボットハート') // Song name
.setStartTimestamp(Date.now()) .setStartTimestamp(Date.now())
.setEndTimestamp(Date.now() + 1_000 * (2 * 60 + 56)) // Song length = 2m56s .setEndTimestamp(Date.now() + 1_000 * (2 * 60 + 56)) // Song length = 2m56s
.setSongId('667eE4CFfNtJloC6Lvmgrx'); // Song ID .setSongId('667eE4CFfNtJloC6Lvmgrx') // Song ID
.setAlbumId('6AAmvxoPoDbJAwbatKwMb9') // Album ID
.setArtistIds('2j00CVYTPx6q9ANbmB2keb', '2nKGmC5Mc13ct02xAY8ccS') // Artist IDs
client.user.setActivity(r); client.user.setActivity(r);
``` ```
<img src='https://cdn.discordapp.com/attachments/820557032016969751/994512257914515456/unknown.png'> <img src='https://cdn.discordapp.com/attachments/820557032016969751/994512257914515456/unknown.png'>

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,4 @@
'use strict'; 'use strict';
const crypto = require('crypto');
const { ActivityTypes } = require('../util/Constants'); const { ActivityTypes } = require('../util/Constants');
const { resolvePartialEmoji } = require('../util/Util'); const { resolvePartialEmoji } = require('../util/Util');
@ -586,28 +585,29 @@ class SpotifyRPC extends RichPresence {
* @type {string} * @type {string}
*/ */
this.id = 'spotify:1'; this.id = 'spotify:1';
/**
* Creation date of the activity
* @type {number}
*/
this.created_at = Date.now();
/** /**
* Flags that describe the activity * Flags that describe the activity
* @type {ActivityFlags} * @type {ActivityFlags}
*/ */
this.flags = 48; // Sync + Play (ActivityFlags) this.flags = 48; // Sync + Play (ActivityFlags)
/**
* The game's or Spotify session's id
* @type {?string}
*/
this.session_id = this.client.session_id;
this.secrets = { /**
join: crypto.randomBytes(20).toString('hex'), // SHA1 / SHA128 * @typedef {Object} SpotifyMetadata
spectate: crypto.randomBytes(20).toString('hex'), * @property {string} album_id Album id
match: crypto.randomBytes(20).toString('hex'), * @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 * Set the large image of this activity
* @param {?string} image Spotify song's image ID * @param {?string} image Spotify song's image ID
@ -618,6 +618,7 @@ class SpotifyRPC extends RichPresence {
super.setAssetsLargeImage(`spotify:${image}`); super.setAssetsLargeImage(`spotify:${image}`);
return this; return this;
} }
/** /**
* Set the small image of this activity * Set the small image of this activity
* @param {?string} image Spotify song's image ID * @param {?string} image Spotify song's image ID
@ -628,6 +629,7 @@ class SpotifyRPC extends RichPresence {
super.setAssetsSmallImage(`spotify:${image}`); super.setAssetsSmallImage(`spotify:${image}`);
return this; return this;
} }
/** /**
* Set Spotify song id to sync with * Set Spotify song id to sync with
* @param {string} id Song id * @param {string} id Song id
@ -638,6 +640,43 @@ class SpotifyRPC extends RichPresence {
return this; 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 * Convert the rich presence to a JSON object
* @returns {SpotifyRPC} * @returns {SpotifyRPC}
@ -653,13 +692,9 @@ class SpotifyRPC extends RichPresence {
details: this.details, details: this.details,
party: this.party, party: this.party,
timestamps: this.timestamps || {}, timestamps: this.timestamps || {},
secrets: this.secrets,
assets: this.assets || {}, assets: this.assets || {},
session_id: this.session_id,
sync_id: this.sync_id, sync_id: this.sync_id,
flags: this.flags, flags: this.flags,
id: this.id,
created_at: this.created_at,
metadata: this.metadata, metadata: this.metadata,
}; };
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]); Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]);

16
typings/index.d.ts vendored
View File

@ -304,6 +304,11 @@ export interface ExternalAssets {
external_asset_path: string; external_asset_path: string;
} }
export interface SpotifyMetadata {
album_id: string;
artist_ids: string[];
}
export class SpotifyRPC extends RichPresence { export class SpotifyRPC extends RichPresence {
public constructor(client: Client, data?: object); public constructor(client: Client, data?: object);
public application_id: Snowflake | null; public application_id: Snowflake | null;
@ -314,14 +319,7 @@ export class SpotifyRPC extends RichPresence {
public name: string; public name: string;
public sync_id: string; public sync_id: string;
public id: string; public id: string;
public created_at: Date;
public flags: number; public flags: number;
public secrets: {
join: string;
spectate: string;
match: string;
};
public session_id: string;
public party: { public party: {
id: string | null; id: string | null;
size: [number, number]; size: [number, number];
@ -333,9 +331,13 @@ export class SpotifyRPC extends RichPresence {
} | null; } | null;
public type: ActivityType; public type: ActivityType;
public url: string | null; public url: string | null;
public metadata: SpotifyMetadata;
public setAssetsLargeImage(image?: string): this; public setAssetsLargeImage(image?: string): this;
public setAssetsSmallImage(image?: string): this; public setAssetsSmallImage(image?: string): this;
public setSongId(id: 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 { export class CustomStatus {