mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +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;
|
module.exports = Client;
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ class ClientDataResolver {
|
|||||||
|
|
||||||
return guild.store.get('members', user.id);
|
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;
|
module.exports = ClientDataResolver;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class ActionsManager {
|
|||||||
this.register('ChannelDelete');
|
this.register('ChannelDelete');
|
||||||
this.register('ChannelUpdate');
|
this.register('ChannelUpdate');
|
||||||
this.register('GuildDelete');
|
this.register('GuildDelete');
|
||||||
|
this.register('UserUpdate');
|
||||||
}
|
}
|
||||||
|
|
||||||
register(name) {
|
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) {
|
LoginEmailPassword(email, password) {
|
||||||
return new Promise((resolve, reject) => {
|
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 })
|
this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password })
|
||||||
.then(data => {
|
.then(data => {
|
||||||
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
|
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
|
||||||
@@ -132,6 +133,25 @@ class RESTMethods{
|
|||||||
.catch(reject);
|
.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;
|
module.exports = RESTMethods;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
const AbstractHandler = require('./AbstractHandler');
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
const Structure = name => require(`../../../../structures/${name}`);
|
const Structure = name => require(`../../../../structures/${name}`);
|
||||||
const CloneObject = name => require(`../../../../util/CloneObject`);
|
const CloneObject = name => require(`../../../../util/CloneObject`);
|
||||||
|
const Constants = require(`../../../../util/Constants`);
|
||||||
|
|
||||||
const ClientUser = Structure('ClientUser');
|
const ClientUser = Structure('ClientUser');
|
||||||
const Guild = Structure('Guild');
|
const Guild = Structure('Guild');
|
||||||
@@ -18,22 +19,7 @@ class UserUpdateHandler extends AbstractHandler {
|
|||||||
let data = packet.d;
|
let data = packet.d;
|
||||||
let client = this.packetManager.client;
|
let client = this.packetManager.client;
|
||||||
|
|
||||||
let user = client.store.user;
|
let response = client.actions.UserUpdate.handle(data);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,26 @@ class ClientUser extends User {
|
|||||||
this.verified = data.verified;
|
this.verified = data.verified;
|
||||||
this.email = data.email;
|
this.email = data.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setUsername(username) {
|
||||||
|
return this.client.rest.methods.UpdateCurrentUser({ username, });
|
||||||
|
}
|
||||||
|
|
||||||
|
setEmail(email) {
|
||||||
|
return this.client.rest.methods.UpdateCurrentUser({ email, });
|
||||||
|
}
|
||||||
|
|
||||||
|
setPassword(password) {
|
||||||
|
return this.client.rest.methods.UpdateCurrentUser({ password, });
|
||||||
|
}
|
||||||
|
|
||||||
|
setAvatar(avatar) {
|
||||||
|
return this.client.rest.methods.UpdateCurrentUser({ avatar, });
|
||||||
|
}
|
||||||
|
|
||||||
|
edit(data) {
|
||||||
|
return this.client.rest.methods.UpdateCurrentUser(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ClientUser;
|
module.exports = ClientUser;
|
||||||
|
|||||||
@@ -14,13 +14,35 @@ class User {
|
|||||||
this.discriminator = data.discriminator;
|
this.discriminator = data.discriminator;
|
||||||
this.avatar = data.avatar;
|
this.avatar = data.avatar;
|
||||||
this.bot = Boolean(data.bot);
|
this.bot = Boolean(data.bot);
|
||||||
this.status = data.status || 'offline';
|
this.status = data.status || this.status || 'offline';
|
||||||
this.game = data.game;
|
this.game = data.game || this.game;
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
return `<@${this.id}>`;
|
return `<@${this.id}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
equals(user) {
|
||||||
|
let base = (
|
||||||
|
this.username === user.username &&
|
||||||
|
this.id === user.id &&
|
||||||
|
this.discriminator === user.discriminator &&
|
||||||
|
this.avatar === user.avatar &&
|
||||||
|
this.bot === Boolean(user.bot)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (base) {
|
||||||
|
if (user.status) {
|
||||||
|
base = this.status === user.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.game) {
|
||||||
|
base = this.game === user.game;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return base;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = User;
|
module.exports = User;
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ class ClientDataStore extends AbstractDataStore{
|
|||||||
this.token = null;
|
this.token = null;
|
||||||
this.session = null;
|
this.session = null;
|
||||||
this.user = null;
|
this.user = null;
|
||||||
|
this.email = null;
|
||||||
|
this.password = null;
|
||||||
|
|
||||||
this.register('users');
|
this.register('users');
|
||||||
this.register('guilds');
|
this.register('guilds');
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Discord = require('../');
|
const Discord = require('../');
|
||||||
|
const request = require('superagent');
|
||||||
|
|
||||||
let client = new Discord.Client();
|
let client = new Discord.Client();
|
||||||
|
|
||||||
@@ -84,12 +85,29 @@ client.on('message', message => {
|
|||||||
message.channel.setName(message.content.substr(8)).then(chanLoop).catch(console.log);
|
message.channel.setName(message.content.substr(8)).then(chanLoop).catch(console.log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.content.startsWith('botname')) {
|
||||||
|
client.user.setUsername(message.content.substr(8)).then(nameLoop).catch(console.log);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.content.startsWith('botavatar')) {
|
||||||
|
request
|
||||||
|
.get('url')
|
||||||
|
.end((err, res) => {
|
||||||
|
client.user.setAvatar(res.body).catch(console.log)
|
||||||
|
.then(user => message.channel.sendMessage('Done!'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (message.content === 'leave') {
|
if (message.content === 'leave') {
|
||||||
message.guild.leave().then(guild => console.log('left guild', guild.name)).catch(console.log);
|
message.guild.leave().then(guild => console.log('left guild', guild.name)).catch(console.log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function nameLoop(user) {
|
||||||
|
user.setUsername(user.username + 'a').then(nameLoop).catch(console.log);
|
||||||
|
}
|
||||||
|
|
||||||
function chanLoop(channel) {
|
function chanLoop(channel) {
|
||||||
channel.setName(channel.name + 'a').then(chanLoop).catch(console.log);
|
channel.setName(channel.name + 'a').then(chanLoop).catch(console.log);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user