From cd066849ad14a61229a95ff7796886ee1560388e Mon Sep 17 00:00:00 2001 From: Isabella Date: Wed, 29 Nov 2017 22:29:37 -0600 Subject: [PATCH] feature(ClientUser): deprecate ClientUser#setGame in favour of ClientUser#setActivity (#2127) * feature(ClientUser): backported ClientUser#setPresence * fix ternary * deprecation stuff --- src/structures/ClientUser.js | 28 ++++++++++++++++++++++++++-- src/structures/Guild.js | 1 + src/util/Constants.js | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index ef3398bd3..75b26a7ee 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -3,6 +3,7 @@ const Collection = require('../util/Collection'); const ClientUserSettings = require('./ClientUserSettings'); const ClientUserGuildSettings = require('./ClientUserGuildSettings'); const Constants = require('../util/Constants'); +const util = require('util'); /** * Represents the logged in client's Discord user. @@ -165,6 +166,7 @@ class ClientUser extends User { * @property {Object} [game] Game the user is playing * @property {string} [game.name] Name of the game * @property {string} [game.url] Twitch stream URL + * @property {?ActivityType|number} [game.type] Type of the activity */ /** @@ -199,7 +201,10 @@ class ClientUser extends User { if (data.game) { game = data.game; - game.type = game.url ? 1 : 0; + game.type = game.url && typeof game.type === 'undefined' ? 1 : game.type || 0; + if (typeof game.type === 'string') { + game.type = Constants.ActivityTypes.indexOf(game.type.toUpperCase()); + } } else if (typeof data.game !== 'undefined') { game = null; } @@ -243,8 +248,9 @@ class ClientUser extends User { /** * Sets the game the client user is playing. * @param {?string} game Game being played - * @param {string} [streamingURL] Twitch stream URL + * @param {?string} [streamingURL] Twitch stream URL * @returns {Promise} + * @deprecated */ setGame(game, streamingURL) { if (!game) return this.setPresence({ game: null }); @@ -256,6 +262,21 @@ class ClientUser extends User { }); } + /** + * Sets the activity the client user is playing. + * @param {?string} name Activity being played + * @param {Object} [options] Options for setting the activity + * @param {string} [options.url] Twitch stream URL + * @param {ActivityType|number} [options.type] Type of the activity + * @returns {Promise} + */ + setActivity(name, { url, type } = {}) { + if (!name) return this.setPresence({ activity: null }); + return this.setPresence({ + game: { name, type, url }, + }); + } + /** * Sets/removes the AFK flag for the client user. * @param {boolean} afk Whether or not the user is AFK @@ -352,4 +373,7 @@ class ClientUser extends User { } } +ClientUser.prototype.setGame = + util.deprecate(ClientUser.prototype.setGame, 'ClientUser#setGame: use ClientUser#setActivity instead'); + module.exports = ClientUser; diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 81ed613cf..3b6844e6d 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1214,6 +1214,7 @@ class Guild { * @name Guild#defaultChannel * @type {TextChannel} * @readonly + * @deprecated */ Object.defineProperty(Guild.prototype, 'defaultChannel', { get: util.deprecate(function defaultChannel() { diff --git a/src/util/Constants.js b/src/util/Constants.js index 7df35b926..70f5947d2 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -350,6 +350,21 @@ exports.Events = { DEBUG: 'debug', }; +/** + * The type of an activity of a users presence, e.g. `PLAYING`. Here are the available types: + * * PLAYING + * * STREAMING + * * LISTENING + * * WATCHING + * @typedef {string} ActivityType + */ +exports.ActivityTypes = [ + 'PLAYING', + 'STREAMING', + 'LISTENING', + 'WATCHING', +]; + /** * The type of a websocket message event, e.g. `MESSAGE_CREATE`. Here are the available events: * * READY