mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Bridged WebSocket Events and REST Requests. Now REST Requests will respond exactly like WS Events to data
This commit is contained in:
@@ -8,6 +8,7 @@ const ClientDataStore = require('../structures/DataStore/ClientDataStore');
|
||||
const ClientManager = require('./ClientManager');
|
||||
const ClientDataResolver = require('./ClientDataResolver');
|
||||
const WebSocketManager = require('./websocket/WebSocketManager');
|
||||
const ActionsManager = require('./actions/ActionsManager');
|
||||
|
||||
class Client extends EventEmitter{
|
||||
|
||||
@@ -19,6 +20,7 @@ class Client extends EventEmitter{
|
||||
this.manager = new ClientManager(this);
|
||||
this.ws = new WebSocketManager(this);
|
||||
this.resolver = new ClientDataResolver(this);
|
||||
this.actions = new ActionsManager(this);
|
||||
}
|
||||
|
||||
login(email, password) {
|
||||
|
||||
27
src/client/actions/Action.js
Normal file
27
src/client/actions/Action.js
Normal file
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
/*
|
||||
|
||||
ABOUT ACTIONS
|
||||
|
||||
Actions are similar to WebSocket Packet Handlers, but since introducing
|
||||
the REST API methods, in order to prevent rewriting code to handle data,
|
||||
"actions" have been introduced. They're basically what Packet Handlers
|
||||
used to be but they're strictly for manipulating data and making sure
|
||||
that WebSocket events don't clash with REST methods.
|
||||
|
||||
*/
|
||||
|
||||
class GenericAction {
|
||||
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = GenericAction;
|
||||
19
src/client/actions/ActionsManager.js
Normal file
19
src/client/actions/ActionsManager.js
Normal file
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const requireAction = name => require(`./${name}`);
|
||||
|
||||
class ActionsManager {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
|
||||
this.register('MessageCreate');
|
||||
this.register('MessageDelete');
|
||||
}
|
||||
|
||||
register(name) {
|
||||
let Action = requireAction(name);
|
||||
this[name] = new Action(this.client);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ActionsManager;
|
||||
31
src/client/actions/MessageCreate.js
Normal file
31
src/client/actions/MessageCreate.js
Normal file
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Message = require('../../structures/Message');
|
||||
|
||||
class MessageCreateAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
|
||||
let client = this.client;
|
||||
let channel = client.store.get('channels', data.channel_id);
|
||||
|
||||
if (channel) {
|
||||
let message = channel._cacheMessage(new Message(channel, data, client));
|
||||
return {
|
||||
m: message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
m: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = MessageCreateAction;
|
||||
42
src/client/actions/MessageDelete.js
Normal file
42
src/client/actions/MessageDelete.js
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Message = require('../../structures/Message');
|
||||
|
||||
class MessageDeleteAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.timeouts = [];
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
let client = this.client;
|
||||
let channel = client.store.get('channels', data.channel_id);
|
||||
if (channel) {
|
||||
let message = channel.store.get('messages', data.id);
|
||||
if (message && !message._deleted) {
|
||||
message._deleted = true;
|
||||
this.scheduleForDeletion(channel, message.id);
|
||||
}
|
||||
|
||||
return {
|
||||
m: message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
m: null,
|
||||
};
|
||||
}
|
||||
|
||||
scheduleForDeletion(channel, id) {
|
||||
this.timeouts.push(
|
||||
setTimeout(() => channel.store.remove('messages', id),
|
||||
this.client.options.rest_ws_bridge_timeout)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = MessageDeleteAction;
|
||||
@@ -57,7 +57,6 @@ class RESTManager{
|
||||
let endpoint = url.replace(/\/[0-9]+/g, '/:id');
|
||||
|
||||
if (this.rateLimitedEndpoints[endpoint] && this.rateLimitedEndpoints[endpoint].timeout) {
|
||||
console.log('adding to queue');
|
||||
return new Promise((resolve, reject) => {
|
||||
this.addRequestToQueue(method, url, auth, data, file, resolve, reject);
|
||||
});
|
||||
|
||||
@@ -42,10 +42,7 @@ class RESTMethods{
|
||||
content, tts, nonce,
|
||||
})
|
||||
.then(data => {
|
||||
let message = new Message(channel, data, this.rest.client);
|
||||
channel._cacheMessage(message);
|
||||
resolve(message);
|
||||
this.rest.client.emit(Constants.Events.MESSAGE_CREATE, message);
|
||||
resolve(this.rest.client.actions.MessageCreate.handle(data).m);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
@@ -54,8 +51,11 @@ class RESTMethods{
|
||||
DeleteMessage(channel, message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL_MESSAGE(channel.id, message.id), true)
|
||||
.then(() => {
|
||||
resolve(message);
|
||||
.then(data => {
|
||||
resolve(this.rest.client.actions.MessageDelete.handle({
|
||||
id: message.id,
|
||||
channel_id: message.channel.id,
|
||||
}).m);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
|
||||
@@ -16,14 +16,12 @@ class MessageCreateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
let channel = client.store.get('channels', data.channel_id);
|
||||
|
||||
if (channel) {
|
||||
let message = new Message(channel, data, client);
|
||||
channel._cacheMessage(message);
|
||||
client.emit(Constants.Events.MESSAGE_CREATE, message);
|
||||
let response = client.actions.MessageCreate.handle(data);
|
||||
|
||||
if (response.m) {
|
||||
client.emit(Constants.Events.MESSAGE_CREATE, response.m);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -16,15 +16,12 @@ class MessageDeleteHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
let channel = client.store.get('channels', data.channel_id);
|
||||
if (channel) {
|
||||
let message = channel.store.get('messages', data.id);
|
||||
if (message) {
|
||||
channel.store.remove('messages', message.id);
|
||||
client.emit(Constants.Events.MESSAGE_DELETE, message);
|
||||
}
|
||||
}
|
||||
|
||||
let response = client.actions.MessageDelete.handle(data);
|
||||
|
||||
if (response.m) {
|
||||
client.emit(Constants.Events.MESSAGE_DELETE, response.m);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ class TextChannel extends ServerChannel {
|
||||
this.store.remove(storeKeys[0]);
|
||||
}
|
||||
|
||||
this.store.add('messages', message);
|
||||
return this.store.add('messages', message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ const DefaultOptions = exports.DefaultOptions = {
|
||||
},
|
||||
protocol_version: 4,
|
||||
max_message_cache: 200,
|
||||
rest_ws_bridge_timeout: 5000,
|
||||
};
|
||||
|
||||
const Status = exports.Status = {
|
||||
|
||||
@@ -71,7 +71,7 @@ client.on('typingStop.', (channel, user, data) => {
|
||||
client.on('message', message => {
|
||||
if (message.author.username === 'hydrabolt') {
|
||||
message.channel.sendMessage('test').then(msg => {
|
||||
msg.delete();
|
||||
msg.delete().catch(console.log);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user