From cf11132275062785cdbd5f6386ef92534836fc86 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:42:27 +0700 Subject: [PATCH] fix: missing all other status --- src/structures/ClientPresence.js | 12 ++++++------ src/structures/Presence.js | 17 +++++++++++++++-- src/structures/RichPresence.js | 13 +++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/structures/ClientPresence.js b/src/structures/ClientPresence.js index c721d78..e073856 100644 --- a/src/structures/ClientPresence.js +++ b/src/structures/ClientPresence.js @@ -1,7 +1,6 @@ 'use strict'; const { Presence } = require('./Presence'); -const { CustomStatus } = require('./RichPresence'); const { TypeError } = require('../errors'); const { Opcodes, ActivityTypes } = require('../util/Constants'); @@ -21,7 +20,8 @@ class ClientPresence extends Presence { */ set(presence) { const packet = this._parse(presence); - this._patch(packet); + // Parse with custom class + this._patch(packet, true); if (typeof presence.shardId === 'undefined') { this.client.ws.broadcast({ op: Opcodes.STATUS_UPDATE, d: packet }); } else if (Array.isArray(presence.shardId)) { @@ -31,6 +31,8 @@ class ClientPresence extends Presence { } else { this.client.ws.shards.get(presence.shardId).send({ op: Opcodes.STATUS_UPDATE, d: packet }); } + // Parse with default class + // this._patch(packet, false); return this; } @@ -49,7 +51,7 @@ class ClientPresence extends Presence { }; if (activities?.length) { for (const [i, activity] of activities.entries()) { - if (!(activity instanceof CustomStatus) && typeof activity.name !== 'string') { + if (![ActivityTypes.CUSTOM, 'CUSTOM'].includes(activity.type) && typeof activity.name !== 'string') { throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string'); } activity.type ??= 0; @@ -63,9 +65,7 @@ class ClientPresence extends Presence { data.activities.push( ...this.activities.map(a => Object.assign(a, { - name: a.name, - type: ActivityTypes[a.type], - url: a.url ?? undefined, + type: typeof a.type === 'number' ? a.type : ActivityTypes[a.type], }), ), ); diff --git a/src/structures/Presence.js b/src/structures/Presence.js index b9e0c0b..516436c 100644 --- a/src/structures/Presence.js +++ b/src/structures/Presence.js @@ -2,6 +2,7 @@ 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'); @@ -76,7 +77,7 @@ class Presence extends Base { return this.guild.members.resolve(this.userId); } - _patch(data) { + _patch(data, fromClient) { if ('status' in data) { /** * The status of this presence @@ -92,7 +93,19 @@ class Presence extends Base { * The activities of this presence * @type {Activity[]} */ - this.activities = data.activities.map(activity => new Activity(this, activity)); + this.activities = data.activities.map(activity => { + if (fromClient === true) { + if ([ActivityTypes.CUSTOM, 'CUSTOM'].includes(activity.type)) { + return new CustomStatus(activity, this); + } else if (activity.id == 'spotify:1') { + return new SpotifyRPC(this.client, activity, this); + } else { + return new RichPresence(this.client, activity, this); + } + } else { + return new Activity(this, activity); + } + }); } else { this.activities ??= []; } diff --git a/src/structures/RichPresence.js b/src/structures/RichPresence.js index 87fff59..4fc1a0d 100644 --- a/src/structures/RichPresence.js +++ b/src/structures/RichPresence.js @@ -19,8 +19,10 @@ class CustomStatus { /** * @param {CustomStatus|CustomStatusOptions} [data={}] CustomStatus to clone or raw data + * @param {Presence} [presence] The presence this activity is part of */ - constructor(data = {}) { + constructor(data = {}, presence) { + Object.defineProperty(this, 'presence', { value: presence }); this.name = 'Custom Status'; /** * The emoji to be displayed @@ -96,9 +98,11 @@ 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) { + constructor(client = {}, data = {}, IPC = false, presence) { Object.defineProperty(this, 'client', { value: client }); + Object.defineProperty(this, 'presence', { value: presence }); /** * The activity's name * @type {string} @@ -552,10 +556,11 @@ 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 = {}) { + constructor(client, options = {}, presence) { if (!client) throw new Error('Client must be set'); - super(client, options); + super(client, options, false, presence); this.setup(options); } /**