mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
Rewrite presence a little bit (#1853)
* such presence many good * Update PresenceStore.js * Update index.js * Update ClientPresenceStore.js * Update Presence.js * Update ClientPresenceStore.js * Update ClientUser.js * Update Presence.js * add timestamps and party * Update Presence.js * Update PresenceStore.js * Update ClientPresenceStore.js * Update ClientPresenceStore.js
This commit is contained in:
@@ -127,7 +127,7 @@ class ClientApplication extends Base {
|
||||
*/
|
||||
iconURL({ format, size } = {}) {
|
||||
if (!this.icon) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).AppIcon(this.id, this.icon, { format, size });
|
||||
return this.client.rest.cdn.AppIcon(this.id, this.icon, { format, size });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@ const Util = require('../util/Util');
|
||||
const Guild = require('./Guild');
|
||||
const Message = require('./Message');
|
||||
const GroupDMChannel = require('./GroupDMChannel');
|
||||
const { TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents the logged in client's Discord user.
|
||||
@@ -28,7 +27,6 @@ class ClientUser extends User {
|
||||
* @type {string}
|
||||
*/
|
||||
this.email = data.email;
|
||||
this.localPresence = {};
|
||||
this._typing = new Map();
|
||||
|
||||
/**
|
||||
@@ -93,6 +91,15 @@ class ClientUser extends User {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ClientUser's presence
|
||||
* @readonly
|
||||
* @type {Presence}
|
||||
*/
|
||||
get presence() {
|
||||
return this.client.presences.clientPresence;
|
||||
}
|
||||
|
||||
edit(data, passcode) {
|
||||
if (!this.bot) {
|
||||
if (typeof passcode !== 'object') {
|
||||
@@ -180,70 +187,19 @@ class ClientUser extends User {
|
||||
* @typedef {Object} PresenceData
|
||||
* @property {PresenceStatus} [status] Status of the user
|
||||
* @property {boolean} [afk] Whether the user is AFK
|
||||
* @property {Object} [game] Game the user is playing
|
||||
* @property {string} [game.name] Name of the game
|
||||
* @property {GameType|number} [game.type] Type of the game
|
||||
* @property {string} [game.url] Twitch stream URL
|
||||
* @property {Object} [activity] activity the user is playing
|
||||
* @property {string} [activity.name] Name of the activity
|
||||
* @property {ActivityType|number} [activity.type] Type of the activity
|
||||
* @property {string} [activity.url] Stream url
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sets the full presence of the client user.
|
||||
* @param {PresenceData} data Data for the presence
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @returns {Promise<Presence>}
|
||||
*/
|
||||
setPresence(data) {
|
||||
// {"op":3,"d":{"status":"dnd","since":0,"game":null,"afk":false}}
|
||||
return new Promise(resolve => {
|
||||
let status = this.localPresence.status || this.presence.status;
|
||||
let game = this.localPresence.game;
|
||||
let afk = this.localPresence.afk || this.presence.afk;
|
||||
|
||||
if (!game && this.presence.game) {
|
||||
game = {
|
||||
name: this.presence.game.name,
|
||||
type: this.presence.game.type,
|
||||
url: this.presence.game.url,
|
||||
};
|
||||
}
|
||||
|
||||
if (data.status) {
|
||||
if (typeof data.status !== 'string') throw new TypeError('INVALID_TYPE', 'status', 'string');
|
||||
if (this.bot) {
|
||||
status = data.status;
|
||||
} else {
|
||||
this.settings.update(Constants.UserSettingsMap.status, data.status);
|
||||
status = 'invisible';
|
||||
}
|
||||
}
|
||||
|
||||
if (data.game) {
|
||||
game = data.game;
|
||||
if (typeof game.type === 'string') {
|
||||
game.type = Constants.GameTypes.indexOf(game.type);
|
||||
if (game.type === -1) throw new TypeError('INVALID_TYPE', 'type', 'GameType');
|
||||
} else if (typeof game.type !== 'number') {
|
||||
game.type = game.url ? 1 : 0;
|
||||
}
|
||||
} else if (typeof data.game !== 'undefined') {
|
||||
game = null;
|
||||
}
|
||||
|
||||
if (typeof data.afk !== 'undefined') afk = data.afk;
|
||||
afk = Boolean(afk);
|
||||
|
||||
this.localPresence = { status, game, afk };
|
||||
this.localPresence.since = 0;
|
||||
this.localPresence.game = this.localPresence.game || null;
|
||||
|
||||
this.client.ws.send({
|
||||
op: 3,
|
||||
d: this.localPresence,
|
||||
});
|
||||
|
||||
this.client._setPresence(this.id, this.localPresence);
|
||||
|
||||
resolve(this);
|
||||
});
|
||||
return this.client.presences.setClientPresence(data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,35 +214,31 @@ class ClientUser extends User {
|
||||
/**
|
||||
* Sets the status of the client user.
|
||||
* @param {PresenceStatus} status Status to change to
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @returns {Promise<Presence>}
|
||||
*/
|
||||
setStatus(status) {
|
||||
return this.setPresence({ status });
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the game the client user is playing.
|
||||
* @param {?string} game Game being played
|
||||
* @param {Object} [options] Options for setting the game
|
||||
* 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 {GameType|number} [options.type] Type of the game
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @param {ActivityType|number} [options.type] Type of the activity
|
||||
* @returns {Promise<Presence>}
|
||||
*/
|
||||
setGame(game, { url, type } = {}) {
|
||||
if (!game) return this.setPresence({ game: null });
|
||||
setActivity(name, { url, type } = {}) {
|
||||
if (!name) return this.setPresence({ activity: null });
|
||||
return this.setPresence({
|
||||
game: {
|
||||
name: game,
|
||||
type,
|
||||
url,
|
||||
},
|
||||
activity: { name, type, url },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets/removes the AFK flag for the client user.
|
||||
* @param {boolean} afk Whether or not the user is AFK
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @returns {Promise<Presence>}
|
||||
*/
|
||||
setAFK(afk) {
|
||||
return this.setPresence({ afk });
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Base = require('./Base');
|
||||
@@ -85,7 +84,7 @@ class Emoji extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get url() {
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).Emoji(this.id);
|
||||
return this.client.rest.cdn.Emoji(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@ const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Collection = require('../util/Collection');
|
||||
const MessageStore = require('../stores/MessageStore');
|
||||
const Constants = require('../util/Constants');
|
||||
|
||||
/*
|
||||
{ type: 3,
|
||||
@@ -115,7 +114,7 @@ class GroupDMChannel extends Channel {
|
||||
*/
|
||||
iconURL({ format, size } = {}) {
|
||||
if (!this.icon) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).GDMIcon(this.id, this.icon, format, size);
|
||||
return this.client.rest.cdn.GDMIcon(this.id, this.icon, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,6 @@ const Emoji = require('./Emoji');
|
||||
const Invite = require('./Invite');
|
||||
const GuildAuditLogs = require('./GuildAuditLogs');
|
||||
const Webhook = require('./Webhook');
|
||||
const { Presence } = require('./Presence');
|
||||
const GuildChannel = require('./GuildChannel');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const VoiceRegion = require('./VoiceRegion');
|
||||
@@ -18,6 +17,7 @@ const GuildMemberStore = require('../stores/GuildMemberStore');
|
||||
const RoleStore = require('../stores/RoleStore');
|
||||
const EmojiStore = require('../stores/EmojiStore');
|
||||
const GuildChannelStore = require('../stores/GuildChannelStore');
|
||||
const PresenceStore = require('../stores/PresenceStore');
|
||||
const Base = require('./Base');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
|
||||
@@ -51,9 +51,9 @@ class Guild extends Base {
|
||||
|
||||
/**
|
||||
* A collection of presences in this guild
|
||||
* @type {Collection<Snowflake, Presence>}
|
||||
* @type {PresenceStore<Snowflake, Presence>}
|
||||
*/
|
||||
this.presences = new Collection();
|
||||
this.presences = new PresenceStore(this.client);
|
||||
|
||||
if (!data) return;
|
||||
if (data.unavailable) {
|
||||
@@ -201,7 +201,7 @@ class Guild extends Base {
|
||||
|
||||
if (data.presences) {
|
||||
for (const presence of data.presences) {
|
||||
this._setPresence(presence.user.id, presence);
|
||||
this.presences.create(presence);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ class Guild extends Base {
|
||||
*/
|
||||
iconURL({ format, size } = {}) {
|
||||
if (!this.icon) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).Icon(this.id, this.icon, format, size);
|
||||
return this.client.rest.cdn.Icon(this.id, this.icon, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,7 +282,7 @@ class Guild extends Base {
|
||||
*/
|
||||
splashURL({ format, size } = {}) {
|
||||
if (!this.splash) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).Splash(this.id, this.splash, format, size);
|
||||
return this.client.rest.cdn.Splash(this.id, this.splash, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1159,14 +1159,6 @@ class Guild extends Base {
|
||||
}
|
||||
}
|
||||
|
||||
_setPresence(id, presence) {
|
||||
if (this.presences.get(id)) {
|
||||
this.presences.get(id).update(presence);
|
||||
return;
|
||||
}
|
||||
this.presences.set(id, new Presence(presence));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position of a role in this guild.
|
||||
* @param {RoleResolvable} role The role to edit, can be a role object or a role ID
|
||||
|
||||
@@ -4,33 +4,33 @@ const Constants = require('../util/Constants');
|
||||
* Represents a user's presence.
|
||||
*/
|
||||
class Presence {
|
||||
constructor(data = {}) {
|
||||
constructor(client, data = {}) {
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
this.patch(data);
|
||||
}
|
||||
|
||||
patch(data) {
|
||||
/**
|
||||
* The status of the presence:
|
||||
*
|
||||
* * **`online`** - user is online
|
||||
* * **`offline`** - user is offline or invisible
|
||||
* * **`idle`** - user is AFK
|
||||
* * **`dnd`** - user is in Do not Disturb
|
||||
* * **`dnd`** - user is in Do Not Disturb
|
||||
* @type {string}
|
||||
*/
|
||||
this.status = data.status || 'offline';
|
||||
|
||||
/**
|
||||
* The game that the user is playing
|
||||
* @type {?Game}
|
||||
*/
|
||||
this.game = data.game ? new Game(data.game) : null;
|
||||
}
|
||||
|
||||
update(data) {
|
||||
this.status = data.status || this.status;
|
||||
this.game = data.game ? new Game(data.game) : null;
|
||||
|
||||
const activity = data.game || data.activity;
|
||||
/**
|
||||
* @type {?Activity}
|
||||
*/
|
||||
this.activity = activity ? new Activity(this, activity) : null;
|
||||
}
|
||||
|
||||
_clone() {
|
||||
const clone = Object.assign(Object.create(this), this);
|
||||
if (this.game) clone.game = this.game._clone();
|
||||
if (this.activity) clone.activity = this.activity._clone();
|
||||
return clone;
|
||||
}
|
||||
|
||||
@@ -43,46 +43,91 @@ class Presence {
|
||||
return this === presence || (
|
||||
presence &&
|
||||
this.status === presence.status &&
|
||||
this.game ? this.game.equals(presence.game) : !presence.game
|
||||
this.activity ? this.activity.equals(presence.activity) : !presence.activity
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a game that is part of a user's presence.
|
||||
* Represents an activity that is part of a user's presence.
|
||||
*/
|
||||
class Game {
|
||||
constructor(data) {
|
||||
class Activity {
|
||||
constructor(presence, data) {
|
||||
Object.defineProperty(this, 'presence', { value: presence });
|
||||
|
||||
/**
|
||||
* The name of the game being played
|
||||
* The name of the activity being played
|
||||
* @type {string}
|
||||
*/
|
||||
this.name = data.name;
|
||||
|
||||
/**
|
||||
* The type of the game status
|
||||
* @type {GameType}
|
||||
* The type of the activity status
|
||||
* @type {ActivityType}
|
||||
*/
|
||||
this.type = Constants.GameTypes[data.type];
|
||||
this.type = Constants.ActivityTypes[data.type];
|
||||
|
||||
/**
|
||||
* If the game is being streamed, a link to the stream
|
||||
* If the activity is being streamed, a link to the stream
|
||||
* @type {?string}
|
||||
*/
|
||||
this.url = data.url || null;
|
||||
|
||||
/**
|
||||
* Details about the activity
|
||||
* @type {?string}
|
||||
*/
|
||||
this.details = data.details || null;
|
||||
|
||||
/**
|
||||
* State of the activity
|
||||
* @type {?string}
|
||||
*/
|
||||
this.state = data.state || null;
|
||||
|
||||
/**
|
||||
* Application ID associated with this activity
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.applicationID = data.application_id || null;
|
||||
|
||||
/**
|
||||
* Timestamps for the activity
|
||||
* @type {?Object}
|
||||
* @prop {?Date} start When the activity started
|
||||
* @prop {?Date} end When the activity will end
|
||||
*/
|
||||
this.timestamps = data.timestamps ? {
|
||||
start: data.timestamps.start ? new Date(data.timestamps.start) : null,
|
||||
end: data.timestamps.end ? new Date(data.timestamps.end) : null,
|
||||
} : null;
|
||||
|
||||
/**
|
||||
* Party of the activity
|
||||
* @type {?Object}
|
||||
* @prop {?string} id ID of the party
|
||||
* @prop {Number[]} size Size of the party as `[current, max]`
|
||||
*/
|
||||
this.party = data.party || null;
|
||||
|
||||
/**
|
||||
* Assets for rich presence
|
||||
* @type {?RichPresenceAssets}
|
||||
*/
|
||||
this.assets = data.assets ? new RichPresenceAssets(this, data.assets) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this game is equal to another game.
|
||||
* @param {Game} game The game to compare with
|
||||
* Whether this activity is equal to another activity.
|
||||
* @param {Activity} activity The activity to compare with
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(game) {
|
||||
return this === game || (
|
||||
game &&
|
||||
this.name === game.name &&
|
||||
this.type === game.type &&
|
||||
this.url === game.url
|
||||
equals(activity) {
|
||||
return this === activity || (
|
||||
activity &&
|
||||
this.name === activity.name &&
|
||||
this.type === activity.type &&
|
||||
this.url === activity.url
|
||||
);
|
||||
}
|
||||
|
||||
@@ -91,5 +136,61 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assets for a rich presence
|
||||
*/
|
||||
class RichPresenceAssets {
|
||||
constructor(activity, assets) {
|
||||
Object.defineProperty(this, 'activity', { value: activity });
|
||||
|
||||
/**
|
||||
* Hover text for large image
|
||||
* @type {?string}
|
||||
*/
|
||||
this.largeText = assets.large_text || null;
|
||||
|
||||
/**
|
||||
* Hover text for small image
|
||||
* @type {?string}
|
||||
*/
|
||||
this.smallText = assets.small_text || null;
|
||||
|
||||
/**
|
||||
* ID of large image asset
|
||||
* @type {?string}
|
||||
*/
|
||||
this.largeImage = assets.large_image || null;
|
||||
|
||||
/**
|
||||
* ID of small image asset
|
||||
* @type {?string}
|
||||
*/
|
||||
this.smallImage = assets.small_image || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} format Format of the image
|
||||
* @param {number} size Size of the iamge
|
||||
* @returns {?string} small image url
|
||||
*/
|
||||
smallImageURL({ format, size } = {}) {
|
||||
if (!this.smallImage) return null;
|
||||
return this.activity.presence.client.rest.cdn
|
||||
.AppAsset(this.activity.applicationID, this.smallImage, { format, size });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} format Format of the image
|
||||
* @param {number} size Size of the iamge
|
||||
* @returns {?string} large image url
|
||||
*/
|
||||
largeImageURL({ format, size } = {}) {
|
||||
if (!this.largeImage) return null;
|
||||
return this.activity.presence.client.rest.cdn
|
||||
.AppAsset(this.activity.applicationID, this.largeImage, { format, size });
|
||||
}
|
||||
}
|
||||
|
||||
exports.Presence = Presence;
|
||||
exports.Game = Game;
|
||||
exports.Activity = Activity;
|
||||
exports.RichPresenceAssets = RichPresenceAssets;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Constants = require('../util/Constants');
|
||||
const { Presence } = require('./Presence');
|
||||
const UserProfile = require('./UserProfile');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
@@ -108,7 +107,7 @@ class User extends Base {
|
||||
*/
|
||||
avatarURL({ format, size } = {}) {
|
||||
if (!this.avatar) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).Avatar(this.id, this.avatar, format, size);
|
||||
return this.client.rest.cdn.Avatar(this.id, this.avatar, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,7 +116,7 @@ class User extends Base {
|
||||
* @readonly
|
||||
*/
|
||||
get defaultAvatarURL() {
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).DefaultAvatar(this.discriminator % 5);
|
||||
return this.client.rest.cdn.DefaultAvatar(this.discriminator % 5);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user