Add textBasedChannel.bulkDelete()

This commit is contained in:
Amish Shah
2016-08-26 22:13:23 +01:00
parent b22bda74b8
commit 642d768cdc
13 changed files with 111 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -6,6 +6,7 @@ class ActionsManager {
this.register('MessageCreate');
this.register('MessageDelete');
this.register('MessageDeleteBulk');
this.register('MessageUpdate');
this.register('ChannelCreate');
this.register('ChannelDelete');

View File

@@ -0,0 +1,33 @@
const Action = require('./Action');
const Collection = require('../../util/Collection');
const Constants = require('../../util/Constants');
class MessageDeleteBulkAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const ids = data.ids;
const messages = new Collection();
for (const id of ids) {
const message = channel.messages.get(id);
if (message) {
messages.set(message.id, message);
}
}
if (messages.size > 0) {
client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
}
return {
messages,
};
}
}
module.exports = MessageDeleteBulkAction;

View File

@@ -76,6 +76,20 @@ class RESTMethods {
});
}
bulkDeleteMessages(channel, messages) {
return new Promise((resolve, reject) => {
const options = { messages };
this.rest.makeRequest('post', `${Constants.Endpoints.channelMessages(channel.id)}/bulk_delete`, true, options)
.then(() => {
resolve(this.rest.client.actions.MessageDeleteBulk.handle({
channel_id: channel.id,
ids: messages,
}).messages);
})
.catch(reject);
});
}
updateMessage(message, content) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('patch', Constants.Endpoints.channelMessage(message.channel.id, message.id), true, {

View File

@@ -37,6 +37,7 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete');
this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate');
this.register(Constants.WSEvents.VOICE_SERVER_UPDATE, 'VoiceServerUpdate');
this.register(Constants.WSEvents.MESSAGE_DELETE_BULK, 'MessageDeleteBulk');
}
get client() {

View File

@@ -0,0 +1,21 @@
const AbstractHandler = require('./AbstractHandler');
class MessageDeleteBulkHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
client.actions.MessageDeleteBulk.handle(data);
}
}
/**
* Emitted whenever a messages are deleted in bulk
*
* @event Client#messageDeleteBulk
* @param {Collection<String, Message>} messages The deleted messages, mapped by their ID
*/
module.exports = MessageDeleteBulkHandler;

View File

@@ -54,6 +54,10 @@ class DMChannel extends Channel {
getMessages() {
return;
}
bulkDelete() {
return;
}
}
TextBasedChannel.applyToClass(DMChannel, true);

View File

@@ -131,6 +131,10 @@ class GroupDMChannel extends Channel {
getMessages() {
return;
}
bulkDelete() {
return;
}
}
TextBasedChannel.applyToClass(GroupDMChannel, true);

View File

@@ -4,6 +4,7 @@ const Collection = require('../util/Collection');
*/
class Message {
constructor(channel, data, client) {
this._type = 'message';
/**
* The channel that the message was sent in
* @type {Channel}

View File

@@ -39,6 +39,10 @@ class TextChannel extends GuildChannel {
getMessages() {
return;
}
bulkDelete() {
return;
}
}
TextBasedChannel.applyToClass(TextChannel, true);

View File

@@ -14,6 +14,22 @@ class TextBasedChannel {
*/
this.messages = new Collection();
}
/**
* Bulk delete a given Collection or Array of messages in one go. Returns the deleted messages after.
* @param {Map<String, Message>|Array<Message>} messages the messages to delete
* @returns {Collection<String, Message>}
*/
bulkDelete(messages) {
if (messages instanceof Map) {
messages = messages.array();
}
if (!(messages instanceof Array)) {
return Promise.reject('pass an array or map');
}
const messageIDs = messages.map(m => m.id);
return this.client.rest.methods.bulkDeleteMessages(this, messageIDs);
}
/**
* Send a message to this channel
* @param {String} content the content to send
@@ -72,7 +88,9 @@ class TextBasedChannel {
.then(data => {
const messages = new Collection();
for (const message of data) {
messages.set(message.id, new Message(this, message, this.client));
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
resolve(messages);
})
@@ -106,6 +124,7 @@ exports.applyToClass = (structure, full = false) => {
if (full) {
props.push('_cacheMessage');
props.push('getMessages');
props.push('bulkDelete');
}
for (const prop of props) {
applyProp(structure, prop);

View File

@@ -36,7 +36,7 @@ exports.DefaultOptions = {
},
},
protocol_version: 6,
max_message_cache: 200,
max_message_cache: 800,
rest_ws_bridge_timeout: 5000,
api_request_method: 'sequential',
shard_id: 0,
@@ -160,6 +160,7 @@ exports.Events = {
MESSAGE_UPDATE: 'messageUpdate',
RECONNECTING: 'reconnecting',
GUILD_MEMBER_SPEAKING: 'guildMemberSpeaking',
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
};
exports.WSEvents = {
@@ -189,6 +190,7 @@ exports.WSEvents = {
FRIEND_ADD: 'RELATIONSHIP_ADD',
FRIEND_REMOVE: 'RELATIONSHIP_REMOVE',
VOICE_SERVER_UPDATE: 'VOICE_SERVER_UPDATE',
MESSAGE_DELETE_BULK: 'MESSAGE_DELETE_BULK',
};
const PermissionFlags = exports.PermissionFlags = {

View File

@@ -12,6 +12,10 @@ client.on('ready', () => {
console.log('ready!');
});
client.on('messageDeleteBulk', msgs => {
msgs.array().map(m => console.log(m.content));
});
client.on('message', message => {
if (true) {
if (message.content === 'makechann') {