diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2023-09-02 19:12:47 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2023-09-02 19:12:47 -0400 |
commit | e4450c8417624b71d779cb4f41692538f9165e10 (patch) | |
tree | b70826542223ecdf8a7a259f61b0a1abb8a217d8 /node_modules/discord.js/src/structures/ClientPresence.js | |
download | sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.gz sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.bz2 sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.zip |
first commit
Diffstat (limited to 'node_modules/discord.js/src/structures/ClientPresence.js')
-rw-r--r-- | node_modules/discord.js/src/structures/ClientPresence.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/structures/ClientPresence.js b/node_modules/discord.js/src/structures/ClientPresence.js new file mode 100644 index 0000000..6dd72ee --- /dev/null +++ b/node_modules/discord.js/src/structures/ClientPresence.js @@ -0,0 +1,90 @@ +'use strict'; + +const { GatewayOpcodes, ActivityType } = require('discord-api-types/v10'); +const { Presence } = require('./Presence'); +const { DiscordjsTypeError, ErrorCodes } = require('../errors'); + +/** + * Represents the client's presence. + * @extends {Presence} + */ +class ClientPresence extends Presence { + constructor(client, data = {}) { + super(client, Object.assign(data, { status: data.status ?? 'online', user: { id: null } })); + } + + /** + * 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 (presence.shardId === undefined) { + this.client.ws.broadcast({ op: GatewayOpcodes.PresenceUpdate, d: packet }); + } else if (Array.isArray(presence.shardId)) { + for (const shardId of presence.shardId) { + this.client.ws.shards.get(shardId).send({ op: GatewayOpcodes.PresenceUpdate, d: packet }); + } + } else { + this.client.ws.shards.get(presence.shardId).send({ op: GatewayOpcodes.PresenceUpdate, d: packet }); + } + 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 DiscordjsTypeError(ErrorCodes.InvalidType, `activities[${i}].name`, 'string'); + } + + activity.type ??= ActivityType.Playing; + + if (activity.type === ActivityType.Custom && !activity.state) { + activity.state = activity.name; + activity.name = 'Custom Status'; + } + + data.activities.push({ + type: activity.type, + name: activity.name, + state: activity.state, + url: activity.url, + }); + } + } else if (!activities && (status || afk || since) && this.activities.length) { + data.activities.push( + ...this.activities.map(a => ({ + name: a.name, + state: a.state ?? undefined, + type: a.type, + url: a.url ?? undefined, + })), + ); + } + + 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} + */ |