Skip to content

Commit

Permalink
feat(Presence/Game): multiple activities and custom status (#3747)
Browse files Browse the repository at this point in the history
* feat(Presence): add activities

* feat(Game): add created* and emoji
  • Loading branch information
SpaceEEC authored Feb 1, 2020
1 parent ccd6043 commit 17b8b23
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
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 @@ -439,6 +439,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 @@ -1011,6 +1012,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

0 comments on commit 17b8b23

Please # to comment.