Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ClientPresence): allow setting activity state #9743

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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