mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +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 mergeDefault = require('../util/MergeDefault');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const RESTManager = require('./rest/RESTManager');
|
const RESTManager = require('./rest/RESTManager');
|
||||||
const ClientDataStore = require('../structures/datastore/ClientDataStore');
|
const ClientDataManager = require('./ClientDataManager');
|
||||||
const ClientManager = require('./ClientManager');
|
const ClientManager = require('./ClientManager');
|
||||||
const ClientDataResolver = require('./ClientDataResolver');
|
const ClientDataResolver = require('./ClientDataResolver');
|
||||||
const WebSocketManager = require('./websocket/WebSocketManager');
|
const WebSocketManager = require('./websocket/WebSocketManager');
|
||||||
@@ -27,11 +27,11 @@ class Client extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
this.rest = new RESTManager(this);
|
this.rest = new RESTManager(this);
|
||||||
/**
|
/**
|
||||||
* The data store of the Client
|
* The data manager of the Client
|
||||||
* @type {ClientDataStore}
|
* @type {ClientDataManager}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.store = new ClientDataStore(this);
|
this.dataManager = new ClientDataManager(this);
|
||||||
/**
|
/**
|
||||||
* The manager of the Client
|
* The manager of the Client
|
||||||
* @type {ClientManager}
|
* @type {ClientManager}
|
||||||
@@ -72,6 +72,26 @@ class Client extends EventEmitter {
|
|||||||
* @type {Map<String, Channel>}
|
* @type {Map<String, Channel>}
|
||||||
*/
|
*/
|
||||||
this.channels = new Map();
|
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);
|
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;
|
module.exports = Client;
|
||||||
|
|||||||
@@ -1,23 +1,15 @@
|
|||||||
const AbstractDataStore = require('./AbstractDataStore');
|
const Constants = require('../util/Constants');
|
||||||
const Constants = require('../../util/Constants');
|
const cloneObject = require('../util/CloneObject');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Guild = require('../structures/Guild');
|
||||||
const Guild = require('../Guild');
|
const User = require('../structures/User');
|
||||||
const User = require('../User');
|
const DMChannel = require('../structures/DMChannel');
|
||||||
const DMChannel = require('../DMChannel');
|
const TextChannel = require('../structures/TextChannel');
|
||||||
const TextChannel = require('../TextChannel');
|
const VoiceChannel = require('../structures/VoiceChannel');
|
||||||
const VoiceChannel = require('../VoiceChannel');
|
const GuildChannel = require('../structures/GuildChannel');
|
||||||
const GuildChannel = require('../GuildChannel');
|
|
||||||
|
|
||||||
class ClientDataStore extends AbstractDataStore {
|
class ClientDataManager {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super();
|
|
||||||
|
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.token = null;
|
|
||||||
this.session = null;
|
|
||||||
this.user = null;
|
|
||||||
this.email = null;
|
|
||||||
this.password = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get pastReady() {
|
get pastReady() {
|
||||||
@@ -102,4 +94,4 @@ class ClientDataStore extends AbstractDataStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ClientDataStore;
|
module.exports = ClientDataManager;
|
||||||
@@ -27,7 +27,7 @@ class ClientManager {
|
|||||||
* @returns {null}
|
* @returns {null}
|
||||||
*/
|
*/
|
||||||
connectToWebSocket(token, resolve, reject) {
|
connectToWebSocket(token, resolve, reject) {
|
||||||
this.client.store.token = token;
|
this.client.token = token;
|
||||||
this.client.rest.methods.getGateway()
|
this.client.rest.methods.getGateway()
|
||||||
.then(gateway => {
|
.then(gateway => {
|
||||||
this.client.ws.connect(gateway);
|
this.client.ws.connect(gateway);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ class ChannelCreateAction extends Action {
|
|||||||
|
|
||||||
handle(data) {
|
handle(data) {
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
const channel = client.store.newChannel(data);
|
const channel = client.dataManager.newChannel(data);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
channel,
|
channel,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class ChannelDeleteAction extends Action {
|
|||||||
let channel = client.channels.get(data.id);
|
let channel = client.channels.get(data.id);
|
||||||
|
|
||||||
if (channel) {
|
if (channel) {
|
||||||
client.store.killChannel(channel);
|
client.dataManager.killChannel(channel);
|
||||||
this.deleted[channel.id] = channel;
|
this.deleted[channel.id] = channel;
|
||||||
this.scheduleForDeletion(channel.id);
|
this.scheduleForDeletion(channel.id);
|
||||||
} else if (this.deleted[data.id]) {
|
} else if (this.deleted[data.id]) {
|
||||||
|
|||||||
@@ -7,22 +7,22 @@ class UserUpdateAction extends Action {
|
|||||||
handle(data) {
|
handle(data) {
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
|
|
||||||
if (client.store.user) {
|
if (client.user) {
|
||||||
if (client.store.user.equals(data)) {
|
if (client.user.equals(data)) {
|
||||||
return {
|
return {
|
||||||
old: client.store.user,
|
old: client.user,
|
||||||
updated: client.store.user,
|
updated: client.user,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldUser = cloneObject(client.store.user);
|
const oldUser = cloneObject(client.user);
|
||||||
client.store.user.setup(data);
|
client.user.setup(data);
|
||||||
|
|
||||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
|
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
old: oldUser,
|
old: oldUser,
|
||||||
updated: client.store.user,
|
updated: client.user,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ class APIRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getAuth() {
|
getAuth() {
|
||||||
if (this.rest.client.store.token && this.rest.client.store.user && this.rest.client.store.user.bot) {
|
if (this.rest.client.token && this.rest.client.user && this.rest.client.user.bot) {
|
||||||
return `Bot ${this.rest.client.store.token}`;
|
return `Bot ${this.rest.client.token}`;
|
||||||
} else if (this.rest.client.store.token) {
|
} else if (this.rest.client.token) {
|
||||||
return this.rest.client.store.token;
|
return this.rest.client.token;
|
||||||
}
|
}
|
||||||
throw Constants.Errors.NO_TOKEN;
|
throw Constants.Errors.NO_TOKEN;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,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.email = email;
|
||||||
this.rest.client.store.password = password;
|
this.rest.client.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);
|
||||||
@@ -31,8 +31,8 @@ class RESTMethods {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
|
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
this.rest.client.store.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
|
this.rest.client.ws.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
|
||||||
resolve(this.rest.client.store.gateway);
|
resolve(this.rest.client.ws.gateway);
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
@@ -117,7 +117,7 @@ class RESTMethods {
|
|||||||
return resolve(dmChannel);
|
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,
|
recipient_id: recipient.id,
|
||||||
})
|
})
|
||||||
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
|
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
|
||||||
@@ -181,14 +181,14 @@ class RESTMethods {
|
|||||||
|
|
||||||
updateCurrentUser(_data) {
|
updateCurrentUser(_data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const user = this.rest.client.store.user;
|
const user = this.rest.client.user;
|
||||||
const data = {};
|
const data = {};
|
||||||
|
|
||||||
data.username = _data.username || user.username;
|
data.username = _data.username || user.username;
|
||||||
data.avatar = this.rest.client.resolver.resolveBase64(_data.avatar) || user.avatar;
|
data.avatar = this.rest.client.resolver.resolveBase64(_data.avatar) || user.avatar;
|
||||||
if (!user.bot) {
|
if (!user.bot) {
|
||||||
data.password = this.rest.client.store.password;
|
data.password = this.rest.client.password;
|
||||||
data.email = _data.email || this.rest.client.store.email;
|
data.email = _data.email || this.rest.client.email;
|
||||||
data.new_password = _data.newPassword;
|
data.new_password = _data.newPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ class WebSocketManager {
|
|||||||
* @type {?Number}
|
* @type {?Number}
|
||||||
*/
|
*/
|
||||||
this.sequence = -1;
|
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() {
|
_sendResume() {
|
||||||
const payload = {
|
const payload = {
|
||||||
token: this.client.store.token,
|
token: this.client.token,
|
||||||
session_id: this.sessionID,
|
session_id: this.sessionID,
|
||||||
seq: this.sequence,
|
seq: this.sequence,
|
||||||
};
|
};
|
||||||
@@ -109,7 +115,7 @@ class WebSocketManager {
|
|||||||
_sendNewIdentify() {
|
_sendNewIdentify() {
|
||||||
this.reconnecting = false;
|
this.reconnecting = false;
|
||||||
const payload = this.client.options.ws;
|
const payload = this.client.options.ws;
|
||||||
payload.token = this.client.store.token;
|
payload.token = this.client.token;
|
||||||
|
|
||||||
this.send({
|
this.send({
|
||||||
op: Constants.OPCodes.IDENTIFY,
|
op: Constants.OPCodes.IDENTIFY,
|
||||||
@@ -191,7 +197,7 @@ class WebSocketManager {
|
|||||||
this.ws.close();
|
this.ws.close();
|
||||||
this.packetManager.handleQueue();
|
this.packetManager.handleQueue();
|
||||||
this.client.emit(Constants.Events.RECONNECTING);
|
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 {
|
} else {
|
||||||
// a new guild
|
// 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);
|
const guild = client.guilds.get(data.guild_id);
|
||||||
|
|
||||||
function makeUser(newUser) {
|
function makeUser(newUser) {
|
||||||
return client.store.newUser(newUser);
|
return client.dataManager.newUser(newUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// step 1
|
// step 1
|
||||||
|
|||||||
@@ -10,14 +10,14 @@ class ReadyHandler extends AbstractHandler {
|
|||||||
const client = this.packetManager.client;
|
const client = this.packetManager.client;
|
||||||
|
|
||||||
const clientUser = new ClientUser(client, data.user);
|
const clientUser = new ClientUser(client, data.user);
|
||||||
client.store.user = clientUser;
|
client.user = clientUser;
|
||||||
client.users.set(clientUser.id, clientUser);
|
client.users.set(clientUser.id, clientUser);
|
||||||
for (const guild of data.guilds) {
|
for (const guild of data.guilds) {
|
||||||
client.store.newGuild(guild);
|
client.dataManager.newGuild(guild);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const privateDM of data.private_channels) {
|
for (const privateDM of data.private_channels) {
|
||||||
client.store.newChannel(privateDM);
|
client.dataManager.newChannel(privateDM);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.packetManager.ws.sessionID = data.session_id;
|
this.packetManager.ws.sessionID = data.session_id;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class ClientUser extends User {
|
|||||||
* @returns {Promise<ClientUser>}
|
* @returns {Promise<ClientUser>}
|
||||||
* @example
|
* @example
|
||||||
* // set username
|
* // set username
|
||||||
* client.store.user.setUsername('discordjs')
|
* client.user.setUsername('discordjs')
|
||||||
* .then(user => console.log(`My new username is ${user.username}`))
|
* .then(user => console.log(`My new username is ${user.username}`))
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
@@ -42,7 +42,7 @@ class ClientUser extends User {
|
|||||||
* @returns {Promise<ClientUser>}
|
* @returns {Promise<ClientUser>}
|
||||||
* @example
|
* @example
|
||||||
* // set email
|
* // 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}`))
|
* .then(user => console.log(`My new email is ${user.email}`))
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
@@ -57,7 +57,7 @@ class ClientUser extends User {
|
|||||||
* @returns {Promise<ClientUser>}
|
* @returns {Promise<ClientUser>}
|
||||||
* @example
|
* @example
|
||||||
* // set password
|
* // set password
|
||||||
* client.store.user.setPassword('password')
|
* client.user.setPassword('password')
|
||||||
* .then(user => console.log('New password set!'))
|
* .then(user => console.log('New password set!'))
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
@@ -70,7 +70,7 @@ class ClientUser extends User {
|
|||||||
* @returns {Promise<ClientUser>}
|
* @returns {Promise<ClientUser>}
|
||||||
* @example
|
* @example
|
||||||
* // set avatar
|
* // set avatar
|
||||||
* client.store.user.setAvatar(fs.readFileSync('./avatar.png'))
|
* client.user.setAvatar(fs.readFileSync('./avatar.png'))
|
||||||
* .then(user => console.log(`New avatar set!`))
|
* .then(user => console.log(`New avatar set!`))
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ class Guild {
|
|||||||
|
|
||||||
_addMember(guildUser, noEvent) {
|
_addMember(guildUser, noEvent) {
|
||||||
if (!(guildUser.user instanceof User)) {
|
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;
|
guildUser.joined_at = guildUser.joined_at || 0;
|
||||||
@@ -263,7 +263,7 @@ class Guild {
|
|||||||
if (data.channels) {
|
if (data.channels) {
|
||||||
this.channels.clear();
|
this.channels.clear();
|
||||||
for (const channel of data.channels) {
|
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
|
* The author of the message
|
||||||
* @type {User}
|
* @type {User}
|
||||||
*/
|
*/
|
||||||
this.author = this.client.store.newUser(data.author);
|
this.author = this.client.dataManager.newUser(data.author);
|
||||||
/**
|
/**
|
||||||
* The content of the message
|
* The content of the message
|
||||||
* @type {String}
|
* @type {String}
|
||||||
@@ -88,7 +88,7 @@ class Message {
|
|||||||
if (user) {
|
if (user) {
|
||||||
this.mentions.push(user);
|
this.mentions.push(user);
|
||||||
} else {
|
} else {
|
||||||
user = this.client.store.newUser(mention);
|
user = this.client.dataManager.newUser(mention);
|
||||||
this.mentions.push(user);
|
this.mentions.push(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ class Message {
|
|||||||
if (user) {
|
if (user) {
|
||||||
this.mentions.push(user);
|
this.mentions.push(user);
|
||||||
} else {
|
} else {
|
||||||
user = this.client.store.newUser(mention);
|
user = this.client.dataManager.newUser(mention);
|
||||||
this.mentions.push(user);
|
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