mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
add ClientPresence, remove ClientPresenceStore
This commit is contained in:
@@ -14,7 +14,7 @@ const VoiceBroadcast = require('./voice/VoiceBroadcast');
|
||||
const UserStore = require('../stores/UserStore');
|
||||
const ChannelStore = require('../stores/ChannelStore');
|
||||
const GuildStore = require('../stores/GuildStore');
|
||||
const ClientPresenceStore = require('../stores/ClientPresenceStore');
|
||||
const ClientPresence = require('../structures/ClientPresence');
|
||||
const GuildEmojiStore = require('../stores/GuildEmojiStore');
|
||||
const { Events, browser } = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
@@ -97,10 +97,11 @@ class Client extends BaseClient {
|
||||
this.channels = new ChannelStore(this);
|
||||
|
||||
/**
|
||||
* Presences that have been received for the client user, mapped by user IDs
|
||||
* @type {ClientPresenceStore<Snowflake, Presence>}
|
||||
* The presence of the Client
|
||||
* @private
|
||||
* @type {ClientPresence}
|
||||
*/
|
||||
this.presences = new ClientPresenceStore(this);
|
||||
this.presence = new ClientPresence(this);
|
||||
|
||||
Object.defineProperty(this, 'token', { writable: true });
|
||||
if (!browser && !this.token && 'CLIENT_TOKEN' in process.env) {
|
||||
|
||||
@@ -41,9 +41,9 @@ class ClientManager {
|
||||
const timeout = this.client.setTimeout(() => reject(new Error('WS_CONNECTION_TIMEOUT')), 1000 * 300);
|
||||
this.client.api.gateway.get().then(async res => {
|
||||
if (this.client.options.presence != null) { // eslint-disable-line eqeqeq
|
||||
const presence = await this.client.presences._parse(this.client.options.presence);
|
||||
const presence = await this.client.presence._parse(this.client.options.presence);
|
||||
this.client.options.ws.presence = presence;
|
||||
this.client.presences.clientPresence.patch(presence);
|
||||
this.client.presence.patch(presence);
|
||||
}
|
||||
const gateway = `${res.url}/`;
|
||||
this.client.emit(Events.DEBUG, `Using gateway ${gateway}`);
|
||||
|
||||
@@ -9,7 +9,7 @@ class ReadyHandler extends AbstractHandler {
|
||||
|
||||
client.ws.heartbeat();
|
||||
|
||||
client.presences.clientPresence.userID = data.user.id;
|
||||
client.presence.userID = data.user.id;
|
||||
if (!ClientUser) ClientUser = require('../../../../structures/ClientUser');
|
||||
const clientUser = new ClientUser(client, data.user);
|
||||
client.user = clientUser;
|
||||
|
||||
@@ -25,7 +25,6 @@ module.exports = {
|
||||
|
||||
// Stores
|
||||
ChannelStore: require('./stores/ChannelStore'),
|
||||
ClientPresenceStore: require('./stores/ClientPresenceStore'),
|
||||
GuildChannelStore: require('./stores/GuildChannelStore'),
|
||||
GuildEmojiStore: require('./stores/GuildEmojiStore'),
|
||||
GuildEmojiRoleStore: require('./stores/GuildEmojiRoleStore'),
|
||||
@@ -74,6 +73,7 @@ module.exports = {
|
||||
MessageReaction: require('./structures/MessageReaction'),
|
||||
PermissionOverwrites: require('./structures/PermissionOverwrites'),
|
||||
Presence: require('./structures/Presence').Presence,
|
||||
ClientPresence: require('./structures/ClientPresence'),
|
||||
ReactionCollector: require('./structures/ReactionCollector'),
|
||||
ReactionEmoji: require('./structures/ReactionEmoji'),
|
||||
RichPresenceAssets: require('./structures/Presence').RichPresenceAssets,
|
||||
|
||||
@@ -1,31 +1,18 @@
|
||||
const PresenceStore = require('./PresenceStore');
|
||||
const { Presence } = require('./Presence');
|
||||
const Collection = require('../util/Collection');
|
||||
const { ActivityTypes, OPCodes } = require('../util/Constants');
|
||||
const { Presence } = require('../structures/Presence');
|
||||
const { TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Stores the client presence and other presences.
|
||||
* @extends {PresenceStore}
|
||||
*/
|
||||
class ClientPresenceStore extends PresenceStore {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
this.clientPresence = new Presence(this.client, {
|
||||
status: 'online',
|
||||
afk: false,
|
||||
since: null,
|
||||
activity: null,
|
||||
user: { id: null },
|
||||
guild_id: null,
|
||||
});
|
||||
class ClientPresence extends Presence {
|
||||
constructor(client, data = {}) {
|
||||
super(client, Object.assign(data, { status: 'online', user: { id: null } }));
|
||||
}
|
||||
|
||||
async setClientPresence(presence) {
|
||||
const packet = await this._parse(presence);
|
||||
this.clientPresence.patch(packet);
|
||||
this.patch(packet);
|
||||
this.client.ws.send({ op: OPCodes.STATUS_UPDATE, d: packet });
|
||||
return this.clientPresence;
|
||||
return this;
|
||||
}
|
||||
|
||||
async _parse({ status, since, afk, activity }) { // eslint-disable-line complexity
|
||||
@@ -45,7 +32,7 @@ class ClientPresenceStore extends PresenceStore {
|
||||
const packet = {
|
||||
afk: afk != null ? afk : false, // eslint-disable-line eqeqeq
|
||||
since: since != null ? since : null, // eslint-disable-line eqeqeq
|
||||
status: status || this.clientPresence.status,
|
||||
status: status || this.status,
|
||||
game: activity ? {
|
||||
type: activity.type,
|
||||
name: activity.name,
|
||||
@@ -67,7 +54,7 @@ class ClientPresenceStore extends PresenceStore {
|
||||
};
|
||||
|
||||
if ((status || afk || since) && !activity) {
|
||||
packet.game = this.clientPresence.activity;
|
||||
packet.game = this.activity;
|
||||
}
|
||||
|
||||
if (packet.game) {
|
||||
@@ -79,4 +66,4 @@ class ClientPresenceStore extends PresenceStore {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientPresenceStore;
|
||||
module.exports = ClientPresence;
|
||||
@@ -32,7 +32,7 @@ class ClientUser extends Structures.get('User') {
|
||||
* @type {Presence}
|
||||
*/
|
||||
get presence() {
|
||||
return this.client.presences.clientPresence;
|
||||
return this.client.presence;
|
||||
}
|
||||
|
||||
edit(data) {
|
||||
@@ -97,7 +97,7 @@ class ClientUser extends Structures.get('User') {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
setPresence(data) {
|
||||
return this.client.presences.setClientPresence(data);
|
||||
return this.client.presence.setClientPresence(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -105,7 +105,6 @@ class User extends Base {
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ const structures = {
|
||||
Message: require('../structures/Message'),
|
||||
MessageReaction: require('../structures/MessageReaction'),
|
||||
Presence: require('../structures/Presence').Presence,
|
||||
ClientPresence: require('../structures/ClientPresence'),
|
||||
VoiceState: require('../structures/VoiceState'),
|
||||
Role: require('../structures/Role'),
|
||||
User: require('../structures/User'),
|
||||
|
||||
Reference in New Issue
Block a user