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(Presence/Game): multiple activities and custom status #3747

Merged
merged 2 commits into from
Feb 1, 2020
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
57 changes: 49 additions & 8 deletions src/structures/Presence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ActivityFlags, Endpoints } = require('../util/Constants');
const ReactionEmoji = require('./ReactionEmoji');

/**
* The status of this presence:
Expand Down Expand Up @@ -30,18 +31,35 @@ class Presence {
*/
Object.defineProperty(this, 'client', { value: client });

this.update(data);
}

update(data) {
/**
* The status of this presence:
* @type {PresenceStatus}
*/
this.status = data.status || 'offline';
this.status = data.status || this.status || 'offline';

/**
* The game that the user is playing
* @type {?Game}
* @deprecated
*/
this.game = data.game ? new Game(data.game, this) : null;

if (data.activities) {
/**
* The activities of this presence
* @type {Game[]}
*/
this.activities = data.activities.map(activity => new Game(activity, this));
} else if (data.activity || data.game) {
this.activities = [new Game(data.activity || data.game, this)];
} else {
this.activities = [];
}

/**
* The devices this presence is on
* @type {?Object}
Expand All @@ -52,12 +70,6 @@ class Presence {
this.clientStatus = data.client_status || null;
}

update(data) {
this.status = data.status || this.status;
this.game = data.game ? new Game(data.game, this) : null;
this.clientStatus = data.client_status || null;
}

/**
* Whether this presence is equal to another
* @param {Presence} presence The presence to compare with
Expand All @@ -67,7 +79,8 @@ class Presence {
return this === presence || (
presence &&
this.status === presence.status &&
(this.game ? this.game.equals(presence.game) : !presence.game) &&
this.activities.length === presence.activities.length &&
this.activities.every((activity, index) => activity.equals(presence.activities[index])) &&
this.clientStatus.web === presence.clientStatus.web &&
this.clientStatus.mobile === presence.clientStatus.mobile &&
this.clientStatus.desktop === presence.clientStatus.desktop
Expand Down Expand Up @@ -147,10 +160,38 @@ class Game {
*/
this.assets = data.assets ? new RichPresenceAssets(this, data.assets) : null;

if (data.emoji) {
/**
* Emoji for a custom activity
* <warn>There is no `reaction` property for this emoji.</warn>
* @type {?ReactionEmoji}
*/
this.emoji = new ReactionEmoji({ message: { client: this.presence.client } }, data.emoji);
this.emoji.reaction = null;
} else {
this.emoji = null;
}


/**
* Creation date of the activity
* @type {number}
*/
this.createdTimestamp = new Date(data.created_at).getTime();

this.syncID = data.sync_id;
this._flags = data.flags;
}

/**
* The time the activity was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}

get flags() {
const flags = [];
for (const [name, flag] of Object.entries(ActivityFlags)) {
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ declare module 'discord.js' {
public applicationID: string;
public assets: RichPresenceAssets;
public details: string;
public emoji: Omit<ReactionEmoji, 'reaction'> | null;
public name: string;
public readonly streaming: boolean;
public party: {
Expand Down Expand Up @@ -1010,6 +1011,7 @@ declare module 'discord.js' {

export class Presence {
constructor(data: object, client: Client);
public activities: Game[];
public readonly client: Client;
public game: Game;
public status: PresenceStatusData;
Expand Down