From 9ed1b59df6acb6356d5950b43d04885d5e692887 Mon Sep 17 00:00:00 2001 From: advaith Date: Sat, 12 Aug 2023 04:31:29 -0700 Subject: [PATCH] feat(ClientPresence): allow setting activity state (#9743) * feat(ClientPresence): allow setting activity state * fix: add to map * feat: use name as fallback state --- packages/discord.js/src/structures/ClientPresence.js | 12 ++++++++++-- packages/discord.js/src/structures/ClientUser.js | 2 ++ packages/discord.js/typings/index.d.ts | 3 ++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/structures/ClientPresence.js b/packages/discord.js/src/structures/ClientPresence.js index 5b448d78643b..6dd72ee94817 100644 --- a/packages/discord.js/src/structures/ClientPresence.js +++ b/packages/discord.js/src/structures/ClientPresence.js @@ -1,6 +1,6 @@ 'use strict'; -const { GatewayOpcodes } = require('discord-api-types/v10'); +const { GatewayOpcodes, ActivityType } = require('discord-api-types/v10'); const { Presence } = require('./Presence'); const { DiscordjsTypeError, ErrorCodes } = require('../errors'); @@ -51,11 +51,18 @@ class ClientPresence extends Presence { if (typeof activity.name !== 'string') { throw new DiscordjsTypeError(ErrorCodes.InvalidType, `activities[${i}].name`, 'string'); } - activity.type ??= 0; + + 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, }); } @@ -63,6 +70,7 @@ class ClientPresence extends Presence { data.activities.push( ...this.activities.map(a => ({ name: a.name, + state: a.state ?? undefined, type: a.type, url: a.url ?? undefined, })), diff --git a/packages/discord.js/src/structures/ClientUser.js b/packages/discord.js/src/structures/ClientUser.js index 3e1a2b10ef92..b93904cf48d4 100644 --- a/packages/discord.js/src/structures/ClientUser.js +++ b/packages/discord.js/src/structures/ClientUser.js @@ -99,6 +99,7 @@ class ClientUser extends User { * Options for setting activities * @typedef {Object} ActivitiesOptions * @property {string} name Name of the activity + * @property {string} [state] State of the activity * @property {ActivityType} [type] Type of the activity * @property {string} [url] Twitch / YouTube stream URL */ @@ -150,6 +151,7 @@ class ClientUser extends User { * Options for setting an activity. * @typedef {Object} ActivityOptions * @property {string} name Name of the activity + * @property {string} [state] State of the activity * @property {string} [url] Twitch / YouTube stream URL * @property {ActivityType} [type] Type of the activity * @property {number|number[]} [shardId] Shard Id(s) to have the activity set on diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 9b13c11eb558..169aa439c25a 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4323,8 +4323,9 @@ export type ActivitiesOptions = Omit; export interface ActivityOptions { name: string; + state?: string; url?: string; - type?: Exclude; + type?: ActivityType; shardId?: number | readonly number[]; }