mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
Change how presences are handled
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const User = require('./User');
|
||||
const Role = require('./Role');
|
||||
const Emoji = require('./Emoji');
|
||||
const Presence = require('./Presence');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
@@ -58,6 +59,14 @@ class Guild {
|
||||
}
|
||||
}
|
||||
|
||||
_setPresence(id, presence) {
|
||||
if (this.presences.get(id)) {
|
||||
this.presences.get(id).update(presence);
|
||||
return;
|
||||
}
|
||||
this.presences.set(id, new Presence(presence));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the Guild
|
||||
* @param {*} data The raw data of the guild
|
||||
@@ -100,6 +109,12 @@ class Guild {
|
||||
*/
|
||||
this.large = data.large || this.large;
|
||||
|
||||
/**
|
||||
* A collection of presences in this Guild
|
||||
* @type {Collection<string, Presence>}
|
||||
*/
|
||||
this.presences = new Collection();
|
||||
|
||||
/**
|
||||
* An array of guild features.
|
||||
* @type {Object[]}
|
||||
@@ -170,11 +185,7 @@ class Guild {
|
||||
|
||||
if (data.presences) {
|
||||
for (const presence of data.presences) {
|
||||
const user = this.client.users.get(presence.user.id);
|
||||
if (user) {
|
||||
user.status = presence.status;
|
||||
user.game = presence.game;
|
||||
}
|
||||
this._setPresence(presence.user.id, presence);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,15 @@ class GuildMember {
|
||||
this._joinDate = new Date(data.joined_at).getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* The presence of this Guild Member
|
||||
* @type {Presence}
|
||||
* @readonly
|
||||
*/
|
||||
get presence() {
|
||||
return this.guild.presences.get(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* The date this member joined the guild
|
||||
* @type {Date}
|
||||
|
||||
89
src/structures/Presence.js
Normal file
89
src/structures/Presence.js
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Represents a Game that is part of a User's presence.
|
||||
*/
|
||||
class Game {
|
||||
constructor(data) {
|
||||
/**
|
||||
* The name of the game being played
|
||||
* @type {string}
|
||||
*/
|
||||
this.name = data.name;
|
||||
/**
|
||||
* The type of the game status
|
||||
* @type {number}
|
||||
*/
|
||||
this.type = data.type;
|
||||
/**
|
||||
* If the game is being streamed, a link to the stream
|
||||
* @type {string}
|
||||
*/
|
||||
this.url = data.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the game is being streamed
|
||||
* @readonly
|
||||
*/
|
||||
get streaming() {
|
||||
return this.type === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this game is equal to another game
|
||||
* @param {Game} other the other game to compare
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(other) {
|
||||
return (
|
||||
this.name === other.name &&
|
||||
this.type === other.type &&
|
||||
this.url === other.url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Presence {
|
||||
constructor(data) {
|
||||
/**
|
||||
* The status of the presence:
|
||||
*
|
||||
* * **`online`** - user is online
|
||||
* * **`offline`** - user is offline
|
||||
* * **`idle`** - user is AFK
|
||||
* @type {string}
|
||||
*/
|
||||
this.status = data.status || 'offline';
|
||||
if (data.game) {
|
||||
/**
|
||||
* The game that the user is playing, `null` if they aren't playing a game.
|
||||
* @type {Game}
|
||||
*/
|
||||
this.game = new Game(data.game);
|
||||
} else {
|
||||
this.game = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this presence is equal to another
|
||||
* @param {Presence} other the presence to compare
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(other) {
|
||||
return (
|
||||
this.status === other.status &&
|
||||
this.game ? this.game.equals(other.game) : !other.game
|
||||
);
|
||||
}
|
||||
|
||||
update(data) {
|
||||
this.status = data.status || this.status;
|
||||
if (data.game) {
|
||||
this.game = new Game(data.game);
|
||||
} else {
|
||||
this.game = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Presence;
|
||||
@@ -1,5 +1,6 @@
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
const Constants = require('../util/Constants');
|
||||
const Presence = require('./Presence');
|
||||
|
||||
/**
|
||||
* Represents a User on Discord.
|
||||
@@ -47,34 +48,26 @@ class User {
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.bot = Boolean(data.bot);
|
||||
}
|
||||
|
||||
/**
|
||||
* The status of the user:
|
||||
*
|
||||
* * **`online`** - user is online
|
||||
* * **`offline`** - user is offline
|
||||
* * **`idle`** - user is AFK
|
||||
* @type {string}
|
||||
*/
|
||||
this.status = data.status || this.status || 'offline';
|
||||
|
||||
/**
|
||||
* Represents data about a Game
|
||||
* @property {string} name the name of the game being played.
|
||||
* @property {string} [url] the URL of the stream, if the game is being streamed.
|
||||
* @property {number} [type] if being streamed, this is `1`.
|
||||
* @typedef {object} Game
|
||||
*/
|
||||
|
||||
/**
|
||||
* The game that the user is playing, `null` if they aren't playing a game.
|
||||
* @type {Game}
|
||||
*/
|
||||
this.game = data.game;
|
||||
/**
|
||||
* The presence of this user
|
||||
* @readonly
|
||||
*/
|
||||
get presence() {
|
||||
if (this.client.presences.has(this.id)) {
|
||||
return this.client.presences.get(this.id);
|
||||
}
|
||||
for (const guild of this.client.guilds.values()) {
|
||||
if (guild.presences.has(this.id)) {
|
||||
return guild.presences.get(this.id);
|
||||
}
|
||||
}
|
||||
return new Presence();
|
||||
}
|
||||
|
||||
patch(data) {
|
||||
for (const prop of ['id', 'username', 'discriminator', 'status', 'game', 'avatar', 'bot']) {
|
||||
for (const prop of ['id', 'username', 'discriminator', 'avatar', 'bot']) {
|
||||
if (typeof data[prop] !== 'undefined') this[prop] = data[prop];
|
||||
}
|
||||
}
|
||||
@@ -150,16 +143,6 @@ class User {
|
||||
this.avatar === user.avatar &&
|
||||
this.bot === Boolean(user.bot);
|
||||
|
||||
if (equal) {
|
||||
if (user.status) equal = this.status === user.status;
|
||||
if (equal && user.game) {
|
||||
equal = this.game &&
|
||||
this.game.name === user.game.name &&
|
||||
this.game.type === user.game.type &&
|
||||
this.game.url === user.game.url;
|
||||
}
|
||||
}
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user