2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Presence } = require('./Presence');
|
|
|
|
const { TypeError } = require('../errors');
|
2022-03-24 10:55:32 +00:00
|
|
|
const { ActivityTypes, Opcodes } = require('../util/Constants');
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the client's presence.
|
|
|
|
* @extends {Presence}
|
|
|
|
*/
|
|
|
|
class ClientPresence extends Presence {
|
|
|
|
constructor(client, data = {}) {
|
2022-03-24 10:55:32 +00:00
|
|
|
super(client, Object.assign(data, { status: data.status ?? 'online', user: { id: null } }));
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the client's presence
|
|
|
|
* @param {PresenceData} presence The data to set the presence to
|
|
|
|
* @returns {ClientPresence}
|
|
|
|
*/
|
|
|
|
set(presence) {
|
|
|
|
const packet = this._parse(presence);
|
|
|
|
this._patch(packet);
|
|
|
|
if (typeof presence.shardId === 'undefined') {
|
2022-03-24 10:55:32 +00:00
|
|
|
this.client.ws.broadcast({ op: Opcodes.STATUS_UPDATE, d: packet });
|
2022-03-19 10:37:45 +00:00
|
|
|
} else if (Array.isArray(presence.shardId)) {
|
|
|
|
for (const shardId of presence.shardId) {
|
2022-03-24 10:55:32 +00:00
|
|
|
this.client.ws.shards.get(shardId).send({ op: Opcodes.STATUS_UPDATE, d: packet });
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-24 10:55:32 +00:00
|
|
|
this.client.ws.shards.get(presence.shardId).send({ op: Opcodes.STATUS_UPDATE, d: packet });
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses presence data into a packet ready to be sent to Discord
|
|
|
|
* @param {PresenceData} presence The data to parse
|
|
|
|
* @returns {APIPresence}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_parse({ status, since, afk, activities }) {
|
|
|
|
const data = {
|
|
|
|
activities: [],
|
|
|
|
afk: typeof afk === 'boolean' ? afk : false,
|
|
|
|
since: typeof since === 'number' && !Number.isNaN(since) ? since : null,
|
|
|
|
status: status ?? this.status,
|
|
|
|
};
|
|
|
|
if (activities?.length) {
|
|
|
|
for (const [i, activity] of activities.entries()) {
|
|
|
|
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string');
|
|
|
|
activity.type ??= 0;
|
2022-03-21 13:12:36 +00:00
|
|
|
data.activities.push(activity);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
} else if (!activities && (status || afk || since) && this.activities.length) {
|
|
|
|
data.activities.push(
|
2022-04-16 10:44:43 +00:00
|
|
|
...this.activities.map(a =>
|
|
|
|
Object.assign(a, {
|
|
|
|
name: a.name,
|
|
|
|
type: a.type,
|
|
|
|
url: a.url ?? undefined,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientPresence;
|
|
|
|
|
|
|
|
/* eslint-disable max-len */
|
|
|
|
/**
|
|
|
|
* @external APIPresence
|
|
|
|
* @see {@link https://discord.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields}
|
|
|
|
*/
|