Skip to content

Commit

Permalink
feat(ClientPresence): allow setting activity state (#9743)
Browse files Browse the repository at this point in the history
* feat(ClientPresence): allow setting activity state

* fix: add to map

* feat: use name as fallback state
  • Loading branch information
advaith1 authored Aug 12, 2023
1 parent 0a9a3ed commit 9ed1b59
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/discord.js/src/structures/ClientPresence.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -51,18 +51,26 @@ 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,
});
}
} 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,
})),
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/structures/ClientUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4323,8 +4323,9 @@ export type ActivitiesOptions = Omit<ActivityOptions, 'shardId'>;

export interface ActivityOptions {
name: string;
state?: string;
url?: string;
type?: Exclude<ActivityType, ActivityType.Custom>;
type?: ActivityType;
shardId?: number | readonly number[];
}

Expand Down

0 comments on commit 9ed1b59

Please sign in to comment.