mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
redo the client user presence stuff
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -112,6 +112,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
|
|
||||||
_send(data) {
|
_send(data) {
|
||||||
if (this.ws.readyState === WebSocket.OPEN) {
|
if (this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
this.emit('send', data);
|
||||||
this.ws.send(data);
|
this.ws.send(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class ClientUser extends User {
|
|||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
this.email = data.email;
|
this.email = data.email;
|
||||||
|
this.localPresence = {};
|
||||||
this._typing = new Map();
|
this._typing = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,60 +88,88 @@ class ClientUser extends User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the status and playing game of the logged in client.
|
* Set the status of the logged in user.
|
||||||
* @param {string} [status] The status, can be `online`, `idle`, or `dnd`
|
* @param {string} status can be `online`, `idle`, `invisible` or `dnd` (do not disturb)
|
||||||
* @param {string|Object} [game] The game that is being played
|
* @returns {Promise<ClientUser, Error>}
|
||||||
* @param {string} [url] If you want to display as streaming, set this as the URL to the stream (must be a
|
|
||||||
* twitch.tv URl)
|
|
||||||
* @returns {Promise<ClientUser>}
|
|
||||||
* @example
|
|
||||||
* // set status
|
|
||||||
* client.user.setStatus('dnd')
|
|
||||||
* .then(user => console.log('Changed status!'))
|
|
||||||
* .catch(console.error);
|
|
||||||
* @example
|
|
||||||
* // set game
|
|
||||||
* client.user.setStatus(null, 'Cool Game 2042')
|
|
||||||
* .then(user => console.log('Changed game!'))
|
|
||||||
* .catch(console.error);
|
|
||||||
*/
|
*/
|
||||||
setStatus(status, game = null, url = null) {
|
setStatus(status) {
|
||||||
return new Promise(resolve => {
|
return this.setPresence({ status });
|
||||||
if (status === 'online' || status === 'here' || status === 'available') {
|
}
|
||||||
this.idleStatus = null;
|
|
||||||
} else if (status === 'idle' || status === 'away') {
|
/**
|
||||||
this.idleStatus = Date.now();
|
* Set the current game of the logged in user.
|
||||||
} else {
|
* @param {string} game the game being played
|
||||||
this.idleStatus = this.idleStatus || null;
|
* @param {string} [streamingURL] an optional URL to a twitch stream, if one is available.
|
||||||
|
* @returns {Promise<ClientUser, Error>}
|
||||||
|
*/
|
||||||
|
setGame(game, streamingURL) {
|
||||||
|
return this.setPresence({ game: {
|
||||||
|
name: game,
|
||||||
|
url: streamingURL,
|
||||||
|
} });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set/remove the AFK flag for the current user.
|
||||||
|
* @param {boolean} afk whether or not the user is AFK.
|
||||||
|
* @returns {Promise<ClientUser, Error>}
|
||||||
|
*/
|
||||||
|
setAFK(afk) {
|
||||||
|
return this.setPresence({ afk });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the full presence of the current user.
|
||||||
|
* @param {Object} data the data to provide
|
||||||
|
* @returns {Promise<ClientUser, Error>}
|
||||||
|
*/
|
||||||
|
setPresence(data) {
|
||||||
|
// {"op":3,"d":{"status":"dnd","since":0,"game":null,"afk":false}}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let status = this.localPresence.status || this.presence.status;
|
||||||
|
let game = this.localPresence.game;
|
||||||
|
let afk = this.localPresence.afk || this.presence.afk;
|
||||||
|
|
||||||
|
if (!game) {
|
||||||
|
game = {
|
||||||
|
name: this.presence.game.name,
|
||||||
|
type: this.presence.game.type,
|
||||||
|
url: this.presence.game.url,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof game === 'string' && !game.length) game = null;
|
if (data.status) {
|
||||||
|
if (typeof data.status !== 'string') {
|
||||||
if (game === null) {
|
reject(new TypeError('status must be a string'));
|
||||||
this.userGame = null;
|
return;
|
||||||
} else if (!game) {
|
}
|
||||||
this.userGame = this.userGame || null;
|
status = data.status;
|
||||||
} else if (typeof game === 'string') {
|
|
||||||
this.userGame = { name: game };
|
|
||||||
} else {
|
|
||||||
this.userGame = game;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url) {
|
if (data.game) {
|
||||||
this.userGame.url = url;
|
game = data.game;
|
||||||
this.userGame.type = 1;
|
if (game.url) {
|
||||||
|
game.type = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof data.afk !== 'undefined') {
|
||||||
|
afk = data.afk;
|
||||||
|
}
|
||||||
|
|
||||||
|
afk = Boolean(afk);
|
||||||
|
|
||||||
|
this.localPresence = { status, game, afk };
|
||||||
|
|
||||||
|
this.localPresence.since = 0;
|
||||||
|
|
||||||
this.client.ws.send({
|
this.client.ws.send({
|
||||||
op: 3,
|
op: 3,
|
||||||
d: {
|
d: this.localPresence,
|
||||||
idle_since: this.idleStatus,
|
|
||||||
game: this.userGame,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.status = this.idleStatus ? 'idle' : 'online';
|
this.client._setPresence(this.id, this.localPresence);
|
||||||
this.game = this.userGame;
|
|
||||||
resolve(this);
|
resolve(this);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class Presence {
|
|||||||
* The status of the presence:
|
* The status of the presence:
|
||||||
*
|
*
|
||||||
* * **`online`** - user is online
|
* * **`online`** - user is online
|
||||||
* * **`offline`** - user is offline
|
* * **`offline`** - user is offline or invisible
|
||||||
* * **`idle`** - user is AFK
|
* * **`idle`** - user is AFK
|
||||||
* * **`dnd`** - user is in Do not Disturb
|
* * **`dnd`** - user is in Do not Disturb
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
|||||||
@@ -10,10 +10,17 @@ const { email, password, token, usertoken } = require('./auth.json');
|
|||||||
|
|
||||||
client.login(token).then(atoken => console.log('logged in with token ' + atoken)).catch(console.log);
|
client.login(token).then(atoken => console.log('logged in with token ' + atoken)).catch(console.log);
|
||||||
|
|
||||||
|
client.ws.on('send', console.log);
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log('ready!');
|
console.log('ready!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on('presenceUpdate', (o, n) => {
|
||||||
|
if (o.username.startsWith('U'))
|
||||||
|
console.log(o.username, o.presence.status, n.presence.status);
|
||||||
|
});
|
||||||
|
|
||||||
client.on('channelCreate', channel => {
|
client.on('channelCreate', channel => {
|
||||||
console.log(`made ${channel.name}`);
|
console.log(`made ${channel.name}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user