mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Remove all data stores! Move to maps
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -2,7 +2,7 @@ const EventEmitter = require('events').EventEmitter;
|
||||
const mergeDefault = require('../util/MergeDefault');
|
||||
const Constants = require('../util/Constants');
|
||||
const RESTManager = require('./rest/RESTManager');
|
||||
const ClientDataStore = require('../structures/datastore/ClientDataStore');
|
||||
const ClientDataManager = require('./ClientDataManager');
|
||||
const ClientManager = require('./ClientManager');
|
||||
const ClientDataResolver = require('./ClientDataResolver');
|
||||
const WebSocketManager = require('./websocket/WebSocketManager');
|
||||
@@ -27,11 +27,11 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
this.rest = new RESTManager(this);
|
||||
/**
|
||||
* The data store of the Client
|
||||
* @type {ClientDataStore}
|
||||
* The data manager of the Client
|
||||
* @type {ClientDataManager}
|
||||
* @private
|
||||
*/
|
||||
this.store = new ClientDataStore(this);
|
||||
this.dataManager = new ClientDataManager(this);
|
||||
/**
|
||||
* The manager of the Client
|
||||
* @type {ClientManager}
|
||||
@@ -72,6 +72,26 @@ class Client extends EventEmitter {
|
||||
* @type {Map<String, Channel>}
|
||||
*/
|
||||
this.channels = new Map();
|
||||
/**
|
||||
* The authorization token for the logged in user/bot.
|
||||
* @type {?String}
|
||||
*/
|
||||
this.token = null;
|
||||
/**
|
||||
* The ClientUser representing the logged in Client
|
||||
* @type {?ClientUser}
|
||||
*/
|
||||
this.user = null;
|
||||
/**
|
||||
* The email, if there is one, for the logged in Client
|
||||
* @type {?String}
|
||||
*/
|
||||
this.email = null;
|
||||
/**
|
||||
* The password, if there is one, for the logged in Client
|
||||
* @type {?String}
|
||||
*/
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,14 +122,6 @@ class Client extends EventEmitter {
|
||||
return this.rest.methods.loginToken(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* The User of the logged in Client, only available after `READY` has been fired.
|
||||
* @type {ClientUser}
|
||||
*/
|
||||
get user() {
|
||||
return this.store.user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Client;
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
const AbstractDataStore = require('./AbstractDataStore');
|
||||
const Constants = require('../../util/Constants');
|
||||
const cloneObject = require('../../util/CloneObject');
|
||||
const Guild = require('../Guild');
|
||||
const User = require('../User');
|
||||
const DMChannel = require('../DMChannel');
|
||||
const TextChannel = require('../TextChannel');
|
||||
const VoiceChannel = require('../VoiceChannel');
|
||||
const GuildChannel = require('../GuildChannel');
|
||||
const Constants = require('../util/Constants');
|
||||
const cloneObject = require('../util/CloneObject');
|
||||
const Guild = require('../structures/Guild');
|
||||
const User = require('../structures/User');
|
||||
const DMChannel = require('../structures/DMChannel');
|
||||
const TextChannel = require('../structures/TextChannel');
|
||||
const VoiceChannel = require('../structures/VoiceChannel');
|
||||
const GuildChannel = require('../structures/GuildChannel');
|
||||
|
||||
class ClientDataStore extends AbstractDataStore {
|
||||
class ClientDataManager {
|
||||
constructor(client) {
|
||||
super();
|
||||
|
||||
this.client = client;
|
||||
this.token = null;
|
||||
this.session = null;
|
||||
this.user = null;
|
||||
this.email = null;
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
get pastReady() {
|
||||
@@ -102,4 +94,4 @@ class ClientDataStore extends AbstractDataStore {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientDataStore;
|
||||
module.exports = ClientDataManager;
|
||||
@@ -27,7 +27,7 @@ class ClientManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
connectToWebSocket(token, resolve, reject) {
|
||||
this.client.store.token = token;
|
||||
this.client.token = token;
|
||||
this.client.rest.methods.getGateway()
|
||||
.then(gateway => {
|
||||
this.client.ws.connect(gateway);
|
||||
|
||||
@@ -4,7 +4,7 @@ class ChannelCreateAction extends Action {
|
||||
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const channel = client.store.newChannel(data);
|
||||
const channel = client.dataManager.newChannel(data);
|
||||
|
||||
return {
|
||||
channel,
|
||||
|
||||
@@ -13,7 +13,7 @@ class ChannelDeleteAction extends Action {
|
||||
let channel = client.channels.get(data.id);
|
||||
|
||||
if (channel) {
|
||||
client.store.killChannel(channel);
|
||||
client.dataManager.killChannel(channel);
|
||||
this.deleted[channel.id] = channel;
|
||||
this.scheduleForDeletion(channel.id);
|
||||
} else if (this.deleted[data.id]) {
|
||||
|
||||
@@ -7,22 +7,22 @@ class UserUpdateAction extends Action {
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
|
||||
if (client.store.user) {
|
||||
if (client.store.user.equals(data)) {
|
||||
if (client.user) {
|
||||
if (client.user.equals(data)) {
|
||||
return {
|
||||
old: client.store.user,
|
||||
updated: client.store.user,
|
||||
old: client.user,
|
||||
updated: client.user,
|
||||
};
|
||||
}
|
||||
|
||||
const oldUser = cloneObject(client.store.user);
|
||||
client.store.user.setup(data);
|
||||
const oldUser = cloneObject(client.user);
|
||||
client.user.setup(data);
|
||||
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
|
||||
|
||||
return {
|
||||
old: oldUser,
|
||||
updated: client.store.user,
|
||||
updated: client.user,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ class APIRequest {
|
||||
}
|
||||
|
||||
getAuth() {
|
||||
if (this.rest.client.store.token && this.rest.client.store.user && this.rest.client.store.user.bot) {
|
||||
return `Bot ${this.rest.client.store.token}`;
|
||||
} else if (this.rest.client.store.token) {
|
||||
return this.rest.client.store.token;
|
||||
if (this.rest.client.token && this.rest.client.user && this.rest.client.user.bot) {
|
||||
return `Bot ${this.rest.client.token}`;
|
||||
} else if (this.rest.client.token) {
|
||||
return this.rest.client.token;
|
||||
}
|
||||
throw Constants.Errors.NO_TOKEN;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,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.client.email = email;
|
||||
this.rest.client.password = password;
|
||||
this.rest.makeRequest('post', Constants.Endpoints.login, false, { email, password })
|
||||
.then(data => {
|
||||
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
|
||||
@@ -31,8 +31,8 @@ class RESTMethods {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
|
||||
.then(res => {
|
||||
this.rest.client.store.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
|
||||
resolve(this.rest.client.store.gateway);
|
||||
this.rest.client.ws.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
|
||||
resolve(this.rest.client.ws.gateway);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
@@ -117,7 +117,7 @@ class RESTMethods {
|
||||
return resolve(dmChannel);
|
||||
}
|
||||
|
||||
return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.store.user.id), true, {
|
||||
return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.user.id), true, {
|
||||
recipient_id: recipient.id,
|
||||
})
|
||||
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
|
||||
@@ -181,14 +181,14 @@ class RESTMethods {
|
||||
|
||||
updateCurrentUser(_data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const user = this.rest.client.store.user;
|
||||
const user = this.rest.client.user;
|
||||
const 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.password = this.rest.client.password;
|
||||
data.email = _data.email || this.rest.client.email;
|
||||
data.new_password = _data.newPassword;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,12 @@ class WebSocketManager {
|
||||
* @type {?Number}
|
||||
*/
|
||||
this.sequence = -1;
|
||||
|
||||
/**
|
||||
* The gateway address for this WebSocket connection, null if not yet available.
|
||||
* @type {?String}
|
||||
*/
|
||||
this.gateway = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +97,7 @@ class WebSocketManager {
|
||||
*/
|
||||
_sendResume() {
|
||||
const payload = {
|
||||
token: this.client.store.token,
|
||||
token: this.client.token,
|
||||
session_id: this.sessionID,
|
||||
seq: this.sequence,
|
||||
};
|
||||
@@ -109,7 +115,7 @@ class WebSocketManager {
|
||||
_sendNewIdentify() {
|
||||
this.reconnecting = false;
|
||||
const payload = this.client.options.ws;
|
||||
payload.token = this.client.store.token;
|
||||
payload.token = this.client.token;
|
||||
|
||||
this.send({
|
||||
op: Constants.OPCodes.IDENTIFY,
|
||||
@@ -191,7 +197,7 @@ class WebSocketManager {
|
||||
this.ws.close();
|
||||
this.packetManager.handleQueue();
|
||||
this.client.emit(Constants.Events.RECONNECTING);
|
||||
this.connect(this.client.store.gateway);
|
||||
this.connect(this.client.ws.gateway);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class GuildCreateHandler extends AbstractHandler {
|
||||
}
|
||||
} else {
|
||||
// a new guild
|
||||
client.store.newGuild(data);
|
||||
client.dataManager.newGuild(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class PresenceUpdateHandler extends AbstractHandler {
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
|
||||
function makeUser(newUser) {
|
||||
return client.store.newUser(newUser);
|
||||
return client.dataManager.newUser(newUser);
|
||||
}
|
||||
|
||||
// step 1
|
||||
|
||||
@@ -10,14 +10,14 @@ class ReadyHandler extends AbstractHandler {
|
||||
const client = this.packetManager.client;
|
||||
|
||||
const clientUser = new ClientUser(client, data.user);
|
||||
client.store.user = clientUser;
|
||||
client.user = clientUser;
|
||||
client.users.set(clientUser.id, clientUser);
|
||||
for (const guild of data.guilds) {
|
||||
client.store.newGuild(guild);
|
||||
client.dataManager.newGuild(guild);
|
||||
}
|
||||
|
||||
for (const privateDM of data.private_channels) {
|
||||
client.store.newChannel(privateDM);
|
||||
client.dataManager.newChannel(privateDM);
|
||||
}
|
||||
|
||||
this.packetManager.ws.sessionID = data.session_id;
|
||||
|
||||
@@ -27,7 +27,7 @@ class ClientUser extends User {
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set username
|
||||
* client.store.user.setUsername('discordjs')
|
||||
* client.user.setUsername('discordjs')
|
||||
* .then(user => console.log(`My new username is ${user.username}`))
|
||||
* .catch(console.log);
|
||||
*/
|
||||
@@ -42,7 +42,7 @@ class ClientUser extends User {
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set email
|
||||
* client.store.user.setEmail('bob@gmail.com')
|
||||
* client.user.setEmail('bob@gmail.com')
|
||||
* .then(user => console.log(`My new email is ${user.email}`))
|
||||
* .catch(console.log);
|
||||
*/
|
||||
@@ -57,7 +57,7 @@ class ClientUser extends User {
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set password
|
||||
* client.store.user.setPassword('password')
|
||||
* client.user.setPassword('password')
|
||||
* .then(user => console.log('New password set!'))
|
||||
* .catch(console.log);
|
||||
*/
|
||||
@@ -70,7 +70,7 @@ class ClientUser extends User {
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set avatar
|
||||
* client.store.user.setAvatar(fs.readFileSync('./avatar.png'))
|
||||
* client.user.setAvatar(fs.readFileSync('./avatar.png'))
|
||||
* .then(user => console.log(`New avatar set!`))
|
||||
* .catch(console.log);
|
||||
*/
|
||||
|
||||
@@ -70,7 +70,7 @@ class Guild {
|
||||
|
||||
_addMember(guildUser, noEvent) {
|
||||
if (!(guildUser.user instanceof User)) {
|
||||
guildUser.user = this.client.store.newUser(guildUser.user);
|
||||
guildUser.user = this.client.dataManager.newUser(guildUser.user);
|
||||
}
|
||||
|
||||
guildUser.joined_at = guildUser.joined_at || 0;
|
||||
@@ -263,7 +263,7 @@ class Guild {
|
||||
if (data.channels) {
|
||||
this.channels.clear();
|
||||
for (const channel of data.channels) {
|
||||
this.client.store.newChannel(channel, this);
|
||||
this.client.dataManager.newChannel(channel, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class Message {
|
||||
* The author of the message
|
||||
* @type {User}
|
||||
*/
|
||||
this.author = this.client.store.newUser(data.author);
|
||||
this.author = this.client.dataManager.newUser(data.author);
|
||||
/**
|
||||
* The content of the message
|
||||
* @type {String}
|
||||
@@ -88,7 +88,7 @@ class Message {
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
} else {
|
||||
user = this.client.store.newUser(mention);
|
||||
user = this.client.dataManager.newUser(mention);
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class Message {
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
} else {
|
||||
user = this.client.store.newUser(mention);
|
||||
user = this.client.dataManager.newUser(mention);
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
class AbstractDataStore {
|
||||
constructor() {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
register(name) {
|
||||
this.data[name] = {};
|
||||
}
|
||||
|
||||
add(location, object) {
|
||||
if (this.data[location][object.id]) {
|
||||
return this.data[location][object.id];
|
||||
}
|
||||
this.data[location][object.id] = object;
|
||||
return object;
|
||||
}
|
||||
|
||||
clear(location) {
|
||||
this.data[location] = {};
|
||||
}
|
||||
|
||||
remove(location, object) {
|
||||
const id = (typeof object === 'string' || object instanceof String) ? object : object.id;
|
||||
if (this.data[location][id]) {
|
||||
delete this.data[location][id];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
get(location, value) {
|
||||
return this.data[location][value];
|
||||
}
|
||||
|
||||
getAsArray(location) {
|
||||
return Object.values(this.data[location]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AbstractDataStore;
|
||||
Reference in New Issue
Block a user