mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03: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:
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;
|
||||
Reference in New Issue
Block a user