mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
Added ClientUser modification support
This commit is contained in:
@@ -33,6 +33,10 @@ class Client extends EventEmitter{
|
||||
}
|
||||
}
|
||||
|
||||
get user() {
|
||||
return this.store.user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Client;
|
||||
|
||||
@@ -54,6 +54,13 @@ class ClientDataResolver {
|
||||
|
||||
return guild.store.get('members', user.id);
|
||||
}
|
||||
|
||||
ResolveBase64(data) {
|
||||
if (data instanceof Buffer) {
|
||||
return 'data:image/jpg;base64,' + data.toString('base64');
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientDataResolver;
|
||||
|
||||
@@ -13,6 +13,7 @@ class ActionsManager {
|
||||
this.register('ChannelDelete');
|
||||
this.register('ChannelUpdate');
|
||||
this.register('GuildDelete');
|
||||
this.register('UserUpdate');
|
||||
}
|
||||
|
||||
register(name) {
|
||||
|
||||
44
src/client/actions/UserUpdate.js
Normal file
44
src/client/actions/UserUpdate.js
Normal file
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const CloneObject = require('../../util/CloneObject');
|
||||
const Message = require('../../structures/Message');
|
||||
|
||||
class UserUpdateAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
|
||||
let client = this.client;
|
||||
|
||||
if (client.store.user) {
|
||||
if (client.store.user.equals(data)) {
|
||||
return {
|
||||
old: client.store.user,
|
||||
updated: client.store.user,
|
||||
};
|
||||
}
|
||||
|
||||
let oldUser = CloneObject(client.store.user);
|
||||
client.store.user.setup(data);
|
||||
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
|
||||
|
||||
return {
|
||||
old: oldUser,
|
||||
updated: client.store.user,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: null,
|
||||
updated: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = UserUpdateAction;
|
||||
@@ -12,7 +12,8 @@ class RESTMethods{
|
||||
|
||||
LoginEmailPassword(email, password) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
this.rest.client.store.email = email;
|
||||
this.rest.client.store.password = password;
|
||||
this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password })
|
||||
.then(data => {
|
||||
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
|
||||
@@ -132,6 +133,25 @@ class RESTMethods{
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
UpdateCurrentUser(_data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let user = this.rest.client.store.user;
|
||||
let data = {};
|
||||
|
||||
data.username = _data.username || user.username;
|
||||
data.avatar = this.rest.client.resolver.ResolveBase64(_data.avatar) || user.avatar;
|
||||
if (!user.bot) {
|
||||
data.password = this.rest.client.store.password;
|
||||
data.email = _data.email || this.rest.client.store.email;
|
||||
data.new_password = _data.newPassword;
|
||||
}
|
||||
|
||||
this.rest.makeRequest('patch', Constants.Endpoints.ME, true, data)
|
||||
.then(data => resolve(this.rest.client.actions.UserUpdate.handle(data).updated))
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RESTMethods;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Structure = name => require(`../../../../structures/${name}`);
|
||||
const CloneObject = name => require(`../../../../util/CloneObject`);
|
||||
const Constants = require(`../../../../util/Constants`);
|
||||
|
||||
const ClientUser = Structure('ClientUser');
|
||||
const Guild = Structure('Guild');
|
||||
@@ -18,22 +19,7 @@ class UserUpdateHandler extends AbstractHandler {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
|
||||
let user = client.store.user;
|
||||
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
let oldUser = CloneObject(user);
|
||||
|
||||
user.username = data.username || user.username;
|
||||
user.id = data.id || user.id;
|
||||
user.avatar = data.avatar || user.avatar;
|
||||
user.discriminator = data.discriminator || user.discriminator;
|
||||
user.email = data.email || user.email;
|
||||
user.verified = data.verified || user.verified;
|
||||
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, user);
|
||||
let response = client.actions.UserUpdate.handle(data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user