mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +01:00
Add client.cacheUser method (#548)
* Add client.cacheUser method * Fixed ESLint issues * Added existence check first
This commit is contained in:
committed by
Amish Shah
parent
e47f3dda94
commit
f944dce5c7
@@ -135,6 +135,16 @@ class Client extends EventEmitter {
|
|||||||
return this.rest.methods.loginToken(email);
|
return this.rest.methods.loginToken(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces a user to be cached.
|
||||||
|
* @param {String} id The ID of the user to cache
|
||||||
|
* @return {Promise<User>}
|
||||||
|
*/
|
||||||
|
cacheUser(id) {
|
||||||
|
if (this.users.has(id)) return Promise.resolve(this.users.get(id));
|
||||||
|
return this.rest.methods.getUser(id);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Collection, mapping Guild ID to Voice Connections.
|
* Returns a Collection, mapping Guild ID to Voice Connections.
|
||||||
* @readonly
|
* @readonly
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class ActionsManager {
|
|||||||
this.register('GuildRoleCreate');
|
this.register('GuildRoleCreate');
|
||||||
this.register('GuildRoleDelete');
|
this.register('GuildRoleDelete');
|
||||||
this.register('GuildRoleUpdate');
|
this.register('GuildRoleUpdate');
|
||||||
|
this.register('UserGet');
|
||||||
this.register('UserUpdate');
|
this.register('UserUpdate');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
src/client/actions/UserGet.js
Normal file
15
src/client/actions/UserGet.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
const Action = require('./Action');
|
||||||
|
|
||||||
|
class UserGetAction extends Action {
|
||||||
|
|
||||||
|
handle(data) {
|
||||||
|
const client = this.client;
|
||||||
|
const user = client.dataManager.newUser(data);
|
||||||
|
|
||||||
|
return {
|
||||||
|
user,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = UserGetAction;
|
||||||
@@ -193,6 +193,16 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getUser(userID) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.rest.makeRequest('get', Constants.Endpoints.user(userID), true)
|
||||||
|
.then((data) => {
|
||||||
|
resolve(this.rest.client.actions.UserGet.handle(data).user);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
updateCurrentUser(_data) {
|
updateCurrentUser(_data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const user = this.rest.client.user;
|
const user = this.rest.client.user;
|
||||||
|
|||||||
@@ -75,12 +75,15 @@ const Endpoints = exports.Endpoints = {
|
|||||||
// general endpoints
|
// general endpoints
|
||||||
login: `${API}/auth/login`,
|
login: `${API}/auth/login`,
|
||||||
logout: `${API}/auth/logout`,
|
logout: `${API}/auth/logout`,
|
||||||
|
gateway: `${API}/gateway`,
|
||||||
|
invite: (id) => `${API}/invite/${id}`,
|
||||||
|
|
||||||
|
// users
|
||||||
|
user: (userID) => `${API}/users/${userID}`,
|
||||||
|
userChannels: (userID) => `${Endpoints.user(userID)}/channels`,
|
||||||
|
avatar: (userID, avatar) => `${Endpoints.user(userID)}/avatars/${avatar}.jpg`,
|
||||||
me: `${API}/users/@me`,
|
me: `${API}/users/@me`,
|
||||||
meGuild: (guildID) => `${Endpoints.me}/guilds/${guildID}`,
|
meGuild: (guildID) => `${Endpoints.me}/guilds/${guildID}`,
|
||||||
gateway: `${API}/gateway`,
|
|
||||||
userChannels: (userID) => `${API}/users/${userID}/channels`,
|
|
||||||
avatar: (userID, avatar) => `${API}/users/${userID}/avatars/${avatar}.jpg`,
|
|
||||||
invite: (id) => `${API}/invite/${id}`,
|
|
||||||
|
|
||||||
// guilds
|
// guilds
|
||||||
guilds: `${API}/guilds`,
|
guilds: `${API}/guilds`,
|
||||||
|
|||||||
Reference in New Issue
Block a user